0

Currently Im am implementing SQLiteDatabase into my android application. So i wanted to create a table called TABLE_LOGIN and store all the data of the login user but i met with a error saying 1 of the column doesnt exist. I check and i did put the column when the table is created Below is my code:

// 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);
}

This is my 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

EDIT INSERT USER DATA CODE:

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
    }
4

2 回答 2

0

我无法阅读整个日志猫,因为我无法在手机的浏览器中滚动它,但我认为问题出在列 ID 上。我认为设置为主键的列会自动设置为不为空,对吗?我看不到您在插入命令中为该列赋值。

如果这个建议是正确的,那么将列设置为 autoincrememt 会有所帮助。如果您想在列表视图中使用该表,请将名称更改为 _id。

于 2012-08-26T19:25:23.533 回答
0

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

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

编辑:我认为您使用的字符串连接有一个小问题。

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)";
于 2012-08-26T17:53:30.117 回答