0

I try to create 2 table in a database, but I get next sintax errors:

    FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapplication/com.myapplication.MyActivity}: android.database.sqlite.SQLiteException: near "Details": syntax error (code 1): , while compiling: CREATE TABLE Activities Details(id INTEGER FOREIGN KEY REFERENCES Activities(id), heartRate INTEGER, heartBeatNo INTEGER, Distance INTEGER, Speed INTEGER, Strides INTEGER,CurrentDateTime TEXT)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.database.sqlite.SQLiteException: near "Details": syntax error (code 1): , while compiling: CREATE TABLE Activities Details(id INTEGER FOREIGN KEY REFERENCES Activities(id), heartRate INTEGER, heartBeatNo INTEGER, Distance INTEGER, Speed INTEGER, Strides INTEGER,CurrentDateTime TEXT)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
            at com.myapplication.DatabaseHandler.onCreate(DatabaseHandler.java:74)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
            at com.myapplication.DatabaseHandler.addActivity(DatabaseHandler.java:98)
            at com.myapplication.MyActivity.onCreate(MyActivity.java:24)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
07-29 05:53:49.484      947-947/com.myapplication D/Insert:﹕ Inserting . .
07-29 05:53:49.584      947-947/com.myapplication E/SQLiteLog﹕ (1) near "Details": syntax error
07-29 05:53:49.614      947-947/com.myapplication D/AndroidRuntime﹕ Shutting down VM
07-29 05:53:49.624      947-947/com.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41465700)
07-29 05:53:49.644      947-947/com.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapplication/com.myapplication.MyActivity}: android.database.sqlite.SQLiteException: near "Details": syntax error (code 1): , while compiling: CREATE TABLE Activities Details(id INTEGER FOREIGN KEY REFERENCES Activities(id), heartRate INTEGER, heartBeatNo INTEGER, Distance INTEGER, Speed INTEGER, Strides INTEGER,CurrentDateTime TEXT)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.database.sqlite.SQLiteException: near "Details": syntax error (code 1): , while compiling: CREATE TABLE Activities Details(id INTEGER FOREIGN KEY REFERENCES Activities(id), heartRate INTEGER, heartBeatNo INTEGER, Distance INTEGER, Speed INTEGER, Strides INTEGER,CurrentDateTime TEXT)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
            at com.myapplication.DatabaseHandler.onCreate(DatabaseHandler.java:74)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
            at com.myapplication.DatabaseHandler.addActivity(DatabaseHandler.java:98)
            at com.myapplication.MyActivity.onCreate(MyActivity.java:24)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

The first error seems to be in next line of code:

String CREATE_ACTIVITIES_DETAILS_TABLE = "CREATE TABLE " +
            TABLE_ACTIVITIES_DETAILS + "(" +
            ACTIVITY_ID + " INTEGER FOREIGN KEY REFERENCES " + 
            TABLE_ACTIVITIES + "(" + KEY_ID + "), " +
            KEY_HEARTRATE + " INTEGER, "+
            KEY_HEARTBEATNO + " INTEGER, "+
            KEY_DISTANCE + " INTEGER, " +
            KEY_SPEED + " INTEGER, " +
            KEY_STRIDES + " INTEGER," +
            KEY_CURRENTDATETIME + " TEXT" +
            ")";
    db.execSQL(CREATE_ACTIVITIES_DETAILS_TABLE);

Here is my DatabaseHandler class:

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 2;

// Database Name
private static final String DATABASE_NAME = "YastDB.com.yast.db";

// Activities table name
private static final String TABLE_ACTIVITIES = "Activities";
private static final String TABLE_ACTIVITIES_DETAILS = "ActivitiesDetails";

// Activities Table Col names
private static final String KEY_ID = "id";
private static final String KEY_ACTIVITYTYPE = "ActivityType";
private static final String KEY_STARTDATETIME = "StartDateTime";
private static final String KEY_ENDDATETIME = "EndDateTime";


//Activities details table col names
private static final String ACTIVITY_ID = "id";
private static final String KEY_HEARTRATE = "heartRate";
private static final String KEY_HEARTBEATNO = "heartBeatNo";
private static final String KEY_DISTANCE = "Distance";
private static final String KEY_SPEED = "Speed";
private static final String KEY_STRIDES = "Strides";
public static final String  KEY_CURRENTDATETIME = "CurrentDateTime";

private String[] PROJECTION = new String[]{ KEY_ID,
        KEY_ACTIVITYTYPE, KEY_STARTDATETIME,KEY_ENDDATETIME };

private String[] PROJECTIONDETAILS = new String[]{ ACTIVITY_ID,
        KEY_HEARTRATE, KEY_HEARTBEATNO, KEY_DISTANCE,
        KEY_SPEED,KEY_STRIDES,KEY_CURRENTDATETIME};

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_ACTIVITIES_TABLE = "CREATE TABLE " + TABLE_ACTIVITIES + "("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            KEY_ACTIVITYTYPE + " INTEGER,"+
            KEY_STARTDATETIME + " TEXT," +
            KEY_ENDDATETIME + " TEXT" +
            ")";
    db.execSQL(CREATE_ACTIVITIES_TABLE);

    String CREATE_ACTIVITIES_DETAILS_TABLE = "CREATE TABLE " + TABLE_ACTIVITIES_DETAILS + "(" +
            ACTIVITY_ID + " INTEGER FOREIGHN KEY REFERENCES " + TABLE_ACTIVITIES + "(" + KEY_ID + "), " +
            KEY_HEARTRATE + " INTEGER, "+
            KEY_HEARTBEATNO + " INTEGER, "+
            KEY_DISTANCE + " INTEGER, " +
            KEY_SPEED + " INTEGER, " +
            KEY_STRIDES + " INTEGER," +
            KEY_CURRENTDATETIME + " TEXT" +
            ")";
    db.execSQL(CREATE_ACTIVITIES_DETAILS_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACTIVITIES);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACTIVITIES_DETAILS);

    // Create tables again
    onCreate(db);
 }

}

The ActivityEntity's attributes and constructors are below. It has set/get methods for all theirs attributes

public class ActivityEntity {

//private variables

private int id;
private int activityType;
private String startDateTime;
private String endDateTime;

// Empty constructor
public ActivityEntity(){

}
// constructor
public ActivityEntity(int id, int activityType, String startDateTime, String endDateTime){
    this.id = id;
    this.activityType = activityType;
    this.startDateTime = startDateTime;
    this.endDateTime = endDateTime;
}

public ActivityEntity(int activityType,  String startDateTime, String endDateTime){
    this.activityType = activityType;
    this.startDateTime = startDateTime;
    this.endDateTime = endDateTime;
}

The ActivityDetails's attributes and constructors are below. It has set/get methods for all theirs attributes

 public class ActivityDetalis {

    //private variables

   private  int activityId;
   private int heartRate;
   private int heartBeatNo;
   private int distance;
   private int speed;
   private int strides;
   private String currentDateTime;
    // Empty constructor
    public ActivityDetalis() {
    }


    public ActivityDetalis(int activityId, int heartRate, int heartBeatNo, int distance, int speed, int strides, String currentDateTime) {
        this.activityId = activityId;
        this.heartRate = heartRate;
        this.heartBeatNo = heartBeatNo;
        this.distance = distance;
        this.speed = speed;
        this.strides = strides;
        this.currentDateTime = currentDateTime;
    }

    public ActivityDetalis(int heartRate, int heartBeatNo, int distance, int speed, int strides, String currentDateTime) {
        this.heartRate = heartRate;
        this.heartBeatNo = heartBeatNo;
        this.distance = distance;
        this.speed = speed;
        this.strides = strides;
        this.currentDateTime = currentDateTime;
    }
0

1 Answer 1

2

You have a space in your table name. Either remove the space, or put the table name in double quotes:

private static final String TABLE_ACTIVITIES_DETAILS = "ActivitiesDetails";

or

private static final String TABLE_ACTIVITIES_DETAILS = "\"Activities Details\"";
Sign up to request clarification or add additional context in comments.

1 Comment

Either you didn't really change the code that was run, or the error message is not really the same. Please update the question if you're still seeing a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.