我正在尝试在我的Android应用程序中使用数据库但是当我启动将使用数据库帮助程序/合同的活动时它会崩溃。我已经尝试了一些查询验证器,我似乎没有发现任何错误。 以下我用来使用我的dbhelper但第二行会导致崩溃。
DBhelper dbHelper = new DBhelper(this);
mDatabase = dbHelper.getWritableDatabase();
我的dbcontract的代码是:
public final class DBcontract {
private DBcontract(){}
public static class DB_table1 implements BaseColumns {
public static final String TABLE_Name = "Timer_table";
public static final String COL_1 = "id";
public static final String COL_2 = "Title";
public static final String COL_3 = "Startvalue";
public static final String COL_4 = "Endvalue";
public static final String COL_5 = "Timer";
}
}
并且dbhelper是:
public class DBhelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "archeage.db";
//Create table query
public static final String CREATE_TABLE =
"" + "CREATE TABLE " + TABLE_Name + " ( "
+ DBcontract.DB_table1.COL_1 + " INTEGER PRIMARY KEY AUTO_INCREMENT, "
+ DBcontract.DB_table1.COL_2 + " TEXT NOT NULL, "
+ DBcontract.DB_table1.COL_3 + " INTEGER NOT NULL, "
+ DBcontract.DB_table1.COL_4 + " INTEGER NOT NULL, "
+ DBcontract.DB_table1.COL_5 + " INTEGER NOT NULL"
+ ");";
//delete table query
public static final String DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_Name;
public DBhelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DELETE_TABLE);
onCreate(db);
}
}
编辑logcat错误:
04-09 13:27:51.233 28939-28939/com.example.xang.archeage E/SQLiteLog: (1) near "AUTO_INCREMENT": syntax error
04-09 13:27:51.236 28939-28939/com.example.xang.archeage E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.xang.archeage, PID: 28939
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xang.archeage/com.example.xang.archeage.Timerpopup}: android.database.sqlite.SQLiteException: near "AUTO_INCREMENT": syntax error (code 1): , while compiling: CREATE TABLE Timer_table ( id INTEGER PRIMARY KEY AUTO_INCREMENT, Title TEXT NOT NULL, Startvalue INTEGER NOT NULL, Endvalue INTEGER NOT NULL, Timer INTEGER NOT NULL);
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2779)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2844)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6361)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: android.database.sqlite.SQLiteException: near "AUTO_INCREMENT": syntax error (code 1): , while compiling: CREATE TABLE Timer_table ( id INTEGER PRIMARY KEY AUTO_INCREMENT, Title TEXT NOT NULL, Startvalue INTEGER NOT NULL, Endvalue INTEGER NOT NULL, Timer INTEGER NOT NULL);
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)
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:1699)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1630)
at com.example.xang.archeage.DBhelper.onCreate(DBhelper.java:41)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.xang.archeage.Timerpopup.onCreate(Timerpopup.java:27)
at android.app.Activity.performCreate(Activity.java:6675)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2732)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2844)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6361)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
答案 0 :(得分:7)
您的问题是 {"editor.fontFamily": "SourceCodePro-Regular"}
不是关键字,正确的关键字是 AUTO_INCREMENT
。
但是,您可能希望省略AUTOINCREMENT
,并在阅读后使用AUTOINCREMENT
: -
AUTOINCREMENT关键字会占用额外的CPU,内存,磁盘空间和 磁盘I / O开销,如果不是严格需要,应该避免。它是 通常不需要。
答案 1 :(得分:3)
无需添加AUTO_INCREMENT; PRIMARY KEY将自动递增。删除AUTO_INCREMENT
或使用AUTOINCREMENT 看到这个:Android SQLite auto increment
答案 2 :(得分:1)
您的问题是AUTO_INCREMENT,使您的查询不那么复杂。
String CREATE_EMPLOYEE_TABLE = "CREATE TABLE Employee (Emp_Id INTEGER PRIMARY KEY AUTOINCREMENT, Emp_Name TEXT, Emp_Phone TEXT, Emp_Email TEXT, Emp_Designation TEXT, Emp_Password TEXT)";