SQLiteDatabase Android出错

时间:2012-08-26 17:39:01

标签: android sqlite

目前我正在将SQLiteDatabase实现到我的Android应用程序中。所以我想创建一个名为TABLE_LOGIN的表并存储登录用户的所有数据,但我遇到一个错误,表示列中的1个不存在。我检查,我确实在创建表时放了列 以下是我的代码:

// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_CNAME = "cname";
private static final String KEY_EMAIL = "email";
private static final String KEY_UID = "uid";
private static final String KEY_CREATED_AT = "created_at";

public void onCreate(SQLiteDatabase db) {
    String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
            + KEY_ID + " INTEGER PRIMARY KEY," 
            + KEY_NAME + " TEXT,"
            + KEY_CNAME + " TEXT,"
            + KEY_EMAIL + " TEXT UNIQUE,"
            + KEY_UID + " TEXT,"
            + KEY_CREATED_AT + " TEXT" + ")";
    db.execSQL(CREATE_LOGIN_TABLE);
}

这是我的logcat:

08-27 01:29:57.336: E/JSON(531): {"tag":"login","success":1,"error":0,"uid":"5033b1eeedde88.60881196","user":{"name":"a","cname":"a","email":"a@gmail.com","created_at":"2012-08-22 00:06:06","updated_at":null}}
08-27 01:29:57.486: E/Database(531): Error inserting uid=5033b1eeedde88.60881196 created_at=2012-08-22 00:06:06 email=a@gmail.com cname=a name=a
08-27 01:29:57.486: E/Database(531): android.database.sqlite.SQLiteException: table login has no column named cname: , while compiling: INSERT INTO login(uid, created_at, email, cname, name) VALUES(?, ?, ?, ?, ?);
08-27 01:29:57.486: E/Database(531):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
08-27 01:29:57.486: E/Database(531):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
08-27 01:29:57.486: E/Database(531):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
08-27 01:29:57.486: E/Database(531):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
08-27 01:29:57.486: E/Database(531):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
08-27 01:29:57.486: E/Database(531):    at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
08-27 01:29:57.486: E/Database(531):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
08-27 01:29:57.486: E/Database(531):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
08-27 01:29:57.486: E/Database(531):    at com.stts.sparetimetradingsystem.library.DatabaseHandlerEmployer.addUser(DatabaseHandlerEmployer.java:72)
08-27 01:29:57.486: E/Database(531):    at com.stts.sparetimetradingsystem.employer.LoginEmployerActivity$LoginEmployer$1.run(LoginEmployerActivity.java:129)
08-27 01:29:57.486: E/Database(531):    at android.os.Handler.handleCallback(Handler.java:587)
08-27 01:29:57.486: E/Database(531):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-27 01:29:57.486: E/Database(531):    at android.os.Looper.loop(Looper.java:123)
08-27 01:29:57.486: E/Database(531):    at android.app.ActivityThread.main(ActivityThread.java:4627)
08-27 01:29:57.486: E/Database(531):    at java.lang.reflect.Method.invokeNative(Native Method)
08-27 01:29:57.486: E/Database(531):    at java.lang.reflect.Method.invoke(Method.java:521)
08-27 01:29:57.486: E/Database(531):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-27 01:29:57.486: E/Database(531):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-27 01:29:57.486: E/Database(531):    at dalvik.system.NativeStart.main(Native Method)
08-27 01:29:58.736: E/AndroidRuntime(531): FATAL EXCEPTION: main
08-27 01:29:58.736: E/AndroidRuntime(531): java.lang.RuntimeException: Unable to destroy activity {com.stts.sparetimetradingsystem/com.stts.sparetimetradingsystem.employer.HomepageEmployerActivity}: java.lang.NullPointerException
08-27 01:29:58.736: E/AndroidRuntime(531):  at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3655)
08-27 01:29:58.736: E/AndroidRuntime(531):  at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673)
08-27 01:29:58.736: E/AndroidRuntime(531):  at android.app.ActivityThread.access$2900(ActivityThread.java:125)
08-27 01:29:58.736: E/AndroidRuntime(531):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
08-27 01:29:58.736: E/AndroidRuntime(531):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 01:29:58.736: E/AndroidRuntime(531):  at android.os.Looper.loop(Looper.java:123)
08-27 01:29:58.736: E/AndroidRuntime(531):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-27 01:29:58.736: E/AndroidRuntime(531):  at java.lang.reflect.Method.invokeNative(Native Method)
-27 01:29:58.736: E/AndroidRuntime(531):    at java.lang.reflect.Method.invoke(Method.java:521)
08-27 01:29:58.736: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-27 01:29:58.736: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-27 01:29:58.736: E/AndroidRuntime(531):  at dalvik.system.NativeStart.main(Native Method)
08-27 01:29:58.736: E/AndroidRuntime(531): Caused by: java.lang.NullPointerException
08-27 01:29:58.736: E/AndroidRuntime(531):  at com.stts.sparetimetradingsystem.employer.HomepageEmployerActivity.onDestroy(HomepageEmployerActivity.java:315)
08-27 01:29:58.736: E/AndroidRuntime(531):  at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642)
08-27 01:29:58.736: E/AndroidRuntime(531):  ... 11 more

编辑 插入用户数据代码:

public void addUser(String name, String cname, String email, String uid, String created_at) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, name); // Name
        values.put(KEY_CNAME, cname); // CName
        values.put(KEY_EMAIL, email); // Email
        values.put(KEY_UID, uid); // UID
        values.put(KEY_CREATED_AT, created_at); // Created At

        // Inserting Row
        db.insert(TABLE_LOGIN, null, values);
        db.close(); // Closing database connection
    }

2 个答案:

答案 0 :(得分:0)

转到设置 - &gt; 管理应用 - &gt; 您的应用 - &gt; 清除所有应用程序数据

可能在您在表格中添加此列之前,它正在使用存储在手机中的旧表格。

编辑 :我认为您使用的字符串连接存在轻微问题。

String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
      + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_NAME+" TEXT, "+KEY_CNAME+" TEXT, "+KEY_EMAIL+" TEXT UNIQUE, "+KEY_UID+" TEXT, "+KEY_CREATED_AT+" TEXT)";

答案 1 :(得分:0)

我无法阅读整个日志猫,因为我无法在手机的浏览器中滚动它,但我认为问题是列ID。 我认为列设置为主键的列是自动设置为不为空吗?我没有看到你在insert命令中给这个列赋值。

如果此建议正确,则有助于将列设置为autoincrememt。如果您想在列表视图中使用该表,请将名称更改为_id。