创建表上的Android SQLite错误

时间:2012-11-25 21:05:01

标签: android sqlite

我正在尝试使用SQLiteOpenHelper创建新数据库。

@Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("CREATE TABLE SecretNumbers ("
                + "id integer primary key autoincrement, "
                + "PhoneNumber text);");

        sqLiteDatabase.execSQL("CREATE TABLE SecretWords ("
                + "id integer primary key autoincrement, "
                + "SecretWord text);");

        sqLiteDatabase.execSQL("CREATE TABLE SecretSMS ("
                + "id integer primary key autoincrement, "
                + "from text, "
                + "body text, "
                + "receivedatetime text, "
                + "isunread integer );" );



    }

在奔跑中我面临着吼叫错误。有人可以帮我吗?

11-25 22:57:23.329: INFO/Database(1231): sqlite returned: error code = 1, msg = near "from": syntax error
11-25 22:57:23.329: ERROR/Database(1231): Failure 1 (near "from": syntax error) on 0x2aaaf0 when preparing 'CREATE TABLE SecretSMS (id integer primary key autoincrement, from text, body text, receivedatetime text, isunread integer );'.
11-25 22:57:23.349: ERROR/SQLiteOpenHelper(1231): Couldn't open SecretPhoneDatabase for writing (will try read-only):
        android.database.sqlite.SQLiteException: near "from": syntax error: CREATE TABLE SecretSMS (id integer primary key autoincrement, from text, body text, receivedatetime text, isunread integer );
        at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
        at net.cgrup.SecretPhone.DataBase.DbHelper.onCreate(DbHelper.java:29)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
        at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
        at net.cgrup.SecretPhone.Broadcast.SmsReceiver.onReceive(SmsReceiver.java:31)
        at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
        at android.os.Handler.handleCallback(Handler.java:587)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:3683)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
11-25 22:57:23.369: WARN/dalvikvm(1231): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-25 22:57:23.399: ERROR/AndroidRuntime(1231): FATAL EXCEPTION: main
        java.lang.RuntimeException: Error receiving broadcast Intent { act=android.provider.Telephony.SMS_RECEIVED (has extras) } in net.cgrup.SecretPhone.Broadcast.SmsReceiver@4051d190
        at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
        at android.os.Handler.handleCallback(Handler.java:587)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:3683)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 0 to 1: /data/data/net.cgrup.SecretPhone/databases/SecretPhoneDatabase
        at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:199)
        at net.cgrup.SecretPhone.Broadcast.SmsReceiver.onReceive(SmsReceiver.java:31)
        at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
        ... 9 more

3 个答案:

答案 0 :(得分:6)

from是一个SQL关键字。您可能希望避免将其用作列名。如果您必须使用该名称,请按照此处所述进行转义:http://www.sqlite.org/lang_keywords.html

答案 1 :(得分:1)

“from”是一个保留的SQL关键字,不能将其用作列名。

答案 2 :(得分:0)

与您的问题无关,但我建议您通过“_id”更改“id”