2

我刚开始开发一个 android 应用程序,我遇到了这个错误,我不知道我在哪里做错了。我想插入用户的登录详细信息,以便在用户成功登录时获取详细信息。

这是我的日志:

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

这是我的 SQLiteDatabase 代码包 com.stts.sparetimetradingsystem.library;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandlerEmployer extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "stts";

// Login table name
private static final String TABLE_LOGIN = "login";

// 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 DatabaseHandlerEmployer(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase dbe) {
    // ORIGINAL 
    /*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" + ")";

    dbe.execSQL(CREATE_LOGIN_TABLE);*/

    // TESTING HERE
    dbe.execSQL("CREATE TABLE " + TABLE_LOGIN + "(" 
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
            + KEY_NAME + " TEXT NOT NULL,"
            + KEY_CNAME + " TEXT,"
            + KEY_EMAIL + " TEXT UNIQUE NOT NULL,"
            + KEY_UID + " TEXT NOT NULL,"
            + KEY_CREATED_AT + " TEXT NOT NULL);"
            );      
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase dbe, int oldVersion, int newVersion) {
    // Drop older table if existed
    dbe.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);

    // Create tables again
    onCreate(dbe);
}

/**
 * Storing user details in database
 * */
//public void addUser(String name, String email, String uid, String created_at) {
public void addUser(String name, String cname, String email, String uid, String created_at) {
    SQLiteDatabase dbe = 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
    dbe.insert(TABLE_LOGIN, null, values);
    dbe.close(); // Closing database connection
}

/**
 * Getting user data from database
 * */
/*public HashMap<String, String> getUserDetails(){
    HashMap<String,String> user = new HashMap<String,String>();
    String selectQuery = "SELECT  * FROM " + TABLE_LOGIN;

    SQLiteDatabase dbe = this.getReadableDatabase();
    Cursor cursor = dbe.rawQuery(selectQuery, null);
    // Move to first row
    cursor.moveToFirst();
    if(cursor.getCount() > 0){
        user.put("name", cursor.getString(1));
        user.put("cname", cursor.getString(2));
        user.put("email", cursor.getString(3));
        user.put("uid", cursor.getString(4));
        user.put("created_at", cursor.getString(5));
    }
    cursor.close();
    dbe.close();
    // return user
    return user;
}*/
public String getCName()
{
    String[] col = new String[] {KEY_NAME};
    SQLiteDatabase dbe = this.getReadableDatabase();
    Cursor c = dbe.query(TABLE_LOGIN, col, null, null, null, null, null);
    String res = "";
    int iCname = c.getColumnIndex(KEY_NAME);
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
    {
        res = res + c.getString(iCname);
    }
    c.close();
    dbe.close();
    return res;
}

public String getEmail()
{
    String[] col = new String[] {KEY_EMAIL};
    SQLiteDatabase dbe = this.getReadableDatabase();
    Cursor c = dbe.query(TABLE_LOGIN, col, null, null, null, null, null);
    String res = "";
    int iEmail = c.getColumnIndex(KEY_EMAIL);
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
    {
        res = res + c.getString(iEmail);
    }
    c.close();
    dbe.close();
    return res;
}

/**
 * Getting user login status
 * return true if rows are there in table
 * */
public int getRowCount() {
    String countQuery = "SELECT  * FROM " + TABLE_LOGIN;
    SQLiteDatabase dbe = this.getReadableDatabase();
    Cursor cursor = dbe.rawQuery(countQuery, null);
    int rowCount = cursor.getCount();
    dbe.close();
    cursor.close();

    // return row count
    return rowCount;
}

/**
 * Re crate database
 * Delete all tables and create them again
 * */
public void resetTables(){
    SQLiteDatabase dbe = this.getWritableDatabase();
    // Delete All Rows
    dbe.delete(TABLE_LOGIN, null, null);
    dbe.close();
}
}

我想知道导致错误的问题在哪里。请帮忙!谢谢!

4

1 回答 1

2

当您更改架构时,例如添加NOT NULL约束或可能添加 column KEY_CNAME,您需要通知您的 OpenHelper 这些更改。它不会自动找到它们。最简单的方法是升级数据库:

private static final int DATABASE_VERSION = 2;

您正试图插入"5040ef44839a09.93014710"INTEGER PRIMARY KEY列。通常 SQLite 只使用数据类型亲和性,但 SQLite 对主键使用严格的数据类型匹配。

这将引发异常消息:"Error: datatype mismatch"

于 2012-08-31T17:21:22.570 回答