我尝试在数据库中创建2个表,但是我得到了下一个sintax错误:
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)
第一个错误似乎出现在下一行代码中:
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);
这是我的DatabaseHandler类:
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);
}
}
ActivityEntity的属性和构造函数如下所示。它为所有属性设置了/ get方法
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;
}
ActivityDetails的属性和构造函数如下所示。它为所有属性设置了/ get方法
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 :(得分:2)
表名中有空格。删除空格,或将表名放在双引号中:
private static final String TABLE_ACTIVITIES_DETAILS = "ActivitiesDetails";
或
private static final String TABLE_ACTIVITIES_DETAILS = "\"Activities Details\"";