2

我正在尝试使用 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
4

3 回答 3

6

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

于 2012-11-25T21:09:50.537 回答
1

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

于 2012-11-25T21:12:23.947 回答
0

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

于 2012-11-27T13:54:23.907 回答