ahmed aslam
ahmed aslam

Reputation: 11

CREATE TABLE ERROR SQLite Android

I am facing a error in my Route_has_Stops TABLE

private static final String CREATE_TABLE_ROUTE_HAS_STOPS="CREATE TABLE " + TABLE_ROUTE_HAS_STOPS + "("
             + KEY_ROUTE_HAS_STOP_ID + " INTEGER PRIMARY KEY,"
             + KEY_ROUTE_ID + " INTEGER,"
             + "FOREIGN KEY ("+KEY_ROUTE_ID+") REFERENCES "+TABLE_Route+" ("+KEY_ROUTE_ID+")"
             + KEY_STOP_ID + " INTEGER,"
             + "FOREIGN KEY ("+KEY_STOP_ID+") REFERENCES "+TABLE_Stops+" ("+KEY_STOP_ID+"))";

Error

android.database.sqlite.SQLiteException: near "Stop_id": syntax error (code 1): , while compiling: CREATE TABLE Route_has_Stops(ID INTEGER PRIMARY KEY,Route_id INTEGER,FOREIGN KEY (Route_id) REFERENCES Route (Route_id)Stop_id INTEGER,FOREIGN KEY (Stop_id) REFERENCES Stops (Stop_id))

Upvotes: 0

Views: 94

Answers (4)

ahmed aslam
ahmed aslam

Reputation: 11

i solved my issue after declaring a Stop and Route key constraints first then declared a Foreign key of them..

 private static final String CREATE_TABLE_ROUTE_AS_STOP="CREATE TABLE "+TABLE_ROUTE_AS_STOP+"("
                 + KEY_ROUTE_HAS_STOP_ID + " INTEGER PRIMARY KEY,"
                 + KEY_ROUTE_ID + " INTEGER," + KEY_STOP_ID + " INTEGER,"
                 + "FOREIGN KEY ("+KEY_ROUTE_ID+") REFERENCES "+TABLE_Route+" ("+KEY_ROUTE_ID+"),"
                 + "FOREIGN KEY ("+KEY_STOP_ID+") REFERENCES "+TABLE_Stops+" ("+KEY_STOP_ID+"))";

Upvotes: 0

Android Dev
Android Dev

Reputation: 433

This way iam Creating my Sqlite database table, Working Fine

DatabaseHelper.java

package com.example.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DataBaseHelper_forsignUp extends SQLiteOpenHelper {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_FNAME = "firstname";
    public static final String KEY_LNAME = "lastname";
    public static final String KEY_EMAIL = "email";
    public static final String KEY_PHONENUMBER = "Phonenumber";
    public static final String KEY_PASSWORD = "Password";
    public static final String KEY_CONFPASSWORD = "ConfPassword";
    public static final String KEY_UPDATESTATUS = "Status";
    public static final String KEY_DATEOFBIRTH="Dateofbirth";
    public static final String KEY_LOCATION="Location";
    public static final String KEY_ADDRESS="Address";

    private static  String DATABASE_NAME = "Signup";
    public static String TABLE_NAME = "SIGNUP";




    public DataBaseHelper_forsignUp(Context context, String name,
            CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, version);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String createQuery = "CREATE TABLE " + TABLE_NAME + "("
                + "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                + " firstname TEXT NOT NULL," 
                + " lastname TEXT NOT NULL, "
                + " email TEXT NOT NULL," 
                + " Phonenumber TEXT NOT NULL,"
                + " Password TEXT NOT NULL," 
                + " Dateofbirth TEXT ,"
            //  + " Gender TEXT ,"
                + " Location TEXT ,"
                + " Address TEXT ,"
                + " Status TEXT NOT NULL);";


        db.execSQL(createQuery);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
        onCreate(db);
    }

}

Upvotes: -1

laalto
laalto

Reputation: 152807

You cannot mix table constraints such as FOREIGN KEY and column specifications. Specify your columns first, and put the FOREIGN KEY table constraints last in the CREATE TABLE.

Upvotes: 2

Mureinik
Mureinik

Reputation: 311143

You are missing a comma before stop_id:

private static final String CREATE_TABLE_ROUTE_HAS_STOPS="CREATE TABLE " + TABLE_ROUTE_HAS_STOPS + "("
             + KEY_ROUTE_HAS_STOP_ID + " INTEGER PRIMARY KEY,"
             + KEY_ROUTE_ID + " INTEGER,"
             + "FOREIGN KEY ("+KEY_ROUTE_ID + ") REFERENCES " 
             + TABLE_Route+" ("+KEY_ROUTE_ID+")," // This comma was missing
             + KEY_STOP_ID + " INTEGER,"
             + "FOREIGN KEY ("+KEY_STOP_ID+") REFERENCES "+TABLE_Stops+" ("
             + KEY_STOP_ID+"))";

Upvotes: 1

Related Questions