得到错误:引起:android.database.sqlite.SQLiteException:near" of"

时间:2012-12-17 02:52:47

标签: android runtime-error

我收到此错误: “引起:android.database.sqlite.SQLiteException:near”of“:语法错误:CREATE TABLE Notes列表(_IDINTEGER PRIMARY KEY AUTOINCREMENT,notes_nameTEXT NOT NULL notes_contentTEXT NOT NULL notes_timeTEXT NOT NULL);”

我正在使用此代码:

 db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
                        KEY_ROWID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        KEY_NAME + "TEXT NOT NULL " + 
                        KEY_CONTENT + "TEXT NOT NULL " +
                        KEY_TIME + "TEXT NOT NULL);"

                        );

这是我的logcat:

  12-17 08:58:15.998: I/Database(515): sqlite returned: error code = 1, msg = near "of": syntax error
    12-17 08:58:15.998: E/Database(515): Failure 1 (near "of": syntax error) on 0x22ddf8 when preparing 'CREATE TABLE List of Notes (_idINTEGER PRIMARY KEY AUTOINCREMENT, notes_nameTEXT NOT NULL, notes_contentTEXT NOT NULL, notes_timeTEXT NOT NULL);'.
    12-17 08:58:16.060: D/AndroidRuntime(515): Shutting down VM
    12-17 08:58:16.068: W/dalvikvm(515): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
    12-17 08:58:16.088: E/AndroidRuntime(515): FATAL EXCEPTION: main
    12-17 08:58:16.088: E/AndroidRuntime(515): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.compilationofnotes.pragya/com.compilationofnotes.pragya.NotesView}: android.database.sqlite.SQLiteException: near "of": syntax error: CREATE TABLE List of Notes (_idINTEGER PRIMARY KEY AUTOINCREMENT, notes_nameTEXT NOT NULL, notes_contentTEXT NOT NULL, notes_timeTEXT NOT NULL);
    12-17 08:58:16.088: E/AndroidRuntime(515):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at android.os.Handler.dispatchMessage(Handler.java:99)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at android.os.Looper.loop(Looper.java:123)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at android.app.ActivityThread.main(ActivityThread.java:4627)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at java.lang.reflect.Method.invokeNative(Native Method)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at java.lang.reflect.Method.invoke(Method.java:521)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at dalvik.system.NativeStart.main(Native Method)
    12-17 08:58:16.088: E/AndroidRuntime(515): Caused by: android.database.sqlite.SQLiteException: near "of": syntax error: CREATE TABLE List of Notes (_idINTEGER PRIMARY KEY AUTOINCREMENT, notes_nameTEXT NOT NULL, notes_contentTEXT NOT NULL, notes_timeTEXT NOT NULL);
    12-17 08:58:16.088: E/AndroidRuntime(515):  at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1727)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at com.compilationofnotes.pragya.SqlDatabase$DbHelper.onCreate(SqlDatabase.java:41)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at com.compilationofnotes.pragya.SqlDatabase.open(SqlDatabase.java:62)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at com.compilationofnotes.pragya.NotesView.onCreate(NotesView.java:23)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    12-17 08:58:16.088: E/AndroidRuntime(515):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    12-17 08:58:16.088: E/AndroidRuntime(515):  ... 11 more
    12-17 08:58:18.848: I/Process(515): Sending signal. PID: 515 SIG: 9

这是我的新logcat:

12-17 09:27:25.535: D/AndroidRuntime(357): Shutting down VM
12-17 09:27:25.535: W/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-17 09:27:25.578: E/AndroidRuntime(357): FATAL EXCEPTION: main
12-17 09:27:25.578: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.compilationofnotes.pragya/com.compilationofnotes.pragya.NewNote}: java.lang.NullPointerException
12-17 09:27:25.578: E/AndroidRuntime(357):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-17 09:27:25.578: E/AndroidRuntime(357):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-17 09:27:25.578: E/AndroidRuntime(357):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-17 09:27:25.578: E/AndroidRuntime(357):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-17 09:27:25.578: E/AndroidRuntime(357):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 09:27:25.578: E/AndroidRuntime(357):  at android.os.Looper.loop(Looper.java:123)
12-17 09:27:25.578: E/AndroidRuntime(357):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-17 09:27:25.578: E/AndroidRuntime(357):  at java.lang.reflect.Method.invokeNative(Native Method)
12-17 09:27:25.578: E/AndroidRuntime(357):  at java.lang.reflect.Method.invoke(Method.java:521)
12-17 09:27:25.578: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-17 09:27:25.578: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-17 09:27:25.578: E/AndroidRuntime(357):  at dalvik.system.NativeStart.main(Native Method)
12-17 09:27:25.578: E/AndroidRuntime(357): Caused by: java.lang.NullPointerException
12-17 09:27:25.578: E/AndroidRuntime(357):  at com.compilationofnotes.pragya.NewNote.onCreate(NewNote.java:28)
12-17 09:27:25.578: E/AndroidRuntime(357):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-17 09:27:25.578: E/AndroidRuntime(357):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-17 09:27:25.578: E/AndroidRuntime(357):  ... 11 more

请帮忙。提前谢谢!

3 个答案:

答案 0 :(得分:3)

将您的创建表格查询更改为:

  String CREATE_DATABASE_TABLE = "CREATE TABLE " + DATABASE_TABLE + "("
            + KEY_ROWID + " INTEGER PRIMARY KEY," +
              KEY_NAME  + " TEXT NOT NULL,"
            + KEY_CONTENT + " TEXT NOT NULL," 
            + KEY_TIME + " TEXT NOT NULL"
             + ")";

 db.execSQL(CREATE_DATABASE_TABLE);

您没有在create table statement

中的每一列之后添加“,”

答案 1 :(得分:3)

除了另一个答案中的好建议之外。

CREATE TABLE List of Notes (

表名必须是一个单词。考虑ListOfNamesList_of_Names

答案 2 :(得分:0)

在代码中使用逗号分隔

   db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
                    KEY_ROWID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    KEY_NAME + "TEXT NOT NULL, " + 
                    KEY_CONTENT + "TEXT NOT NULL, " +
                    KEY_TIME + "TEXT NOT NULL);"

                    );