代码之家  ›  专栏  ›  技术社区  ›  Aviumaa

我的应用程序在创建Sqlite DB时崩溃[关闭]

  •  -4
  • Aviumaa  · 技术社区  · 6 年前

    我试图在我的android应用程序中使用数据库,但当我开始使用DB helper/Contract的活动时,它崩溃了。我尝试了一些查询验证器,似乎没有发现任何错误。 下面我使用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) 
    
    3 回复  |  直到 6 年前
        1
  •  7
  •   MikeT    6 年前

    你的问题是 AUTO_INCREMENT 不是关键字正确的关键字是 AUTOINCREMENT

    但是,您可能希望省略 自动递增 而且只要使用 INTEGER PRIMARY KEY 阅读后:-

    AUTOINCREMENT关键字会带来额外的CPU、内存、磁盘空间和 磁盘I/O开销,如果不严格需要,应避免。是的 通常不需要。

    SQLite Autoincrement

        2
  •  3
  •   dev    6 年前

    无需添加AUTO\u INCREMENT;主键将自动递增。删除自动增量

    或使用自动增量 请参见: Android SQLite auto increment

        3
  •  1
  •   Kanwarpreet Singh    6 年前

    您的问题是AUTO\u 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)";