0

基本上,我想验证用户名是否尚未写入数据库。由于我很新,我不知道该怎么做并尝试过,但我收到了这个代码的错误。我真的不知道是否有更好的方法来做到这一点,如果有,请告诉我,谢谢。

   private long addEntry(String strUsename, String strName,
        String strFirstName, String strPassword) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_USERNAME, strUsename);
    cv.put(KEY_FIRSTNAME, strFirstName);
    cv.put(KEY_NAME, strName);      
    cv.put(KEY_PASSWORD, strPassword);
    return accountDB.insert(DATABASE_TABLE, null, cv);
}

public String Verification_add(String ver_username, String strName,
        String strFirstName, String strPassword){
    String result = ""; 
    String[] columns = {KEY_ROWID, KEY_USERNAME, KEY_NAME, KEY_FIRSTNAME, KEY_PASSWORD}; 
    Cursor c = accountDB.query(DATABASE_TABLE, columns, KEY_USERNAME + "=" + ver_username, null, null, null, null);
    if (!c.moveToFirst()){ // IF IT'S FALSE, that mean the username is not taken, ritgh ? 
        addEntry(ver_username, strName, strFirstName, strPassword);
        c.close(); 
        return result = "Account[...] code: 1 !"; 
    }else{
        c.close(); 
        return result = "Username already [...] code:2 "; 
    } 

}

这是 logcat 报告:

02-24 23:09:32.201: E/Verification_add(1389): MY ERROR !! 
02-24 23:09:32.201: E/Verification_add(1389): java.lang.NullPointerException
02-24 23:09:32.201: E/Verification_add(1389):   at com.example.communication.DataBaseRegistration.Verification_add(DataBaseRegistration.java:79)
02-24 23:09:32.201: E/Verification_add(1389):   at com.example.communication.Registration.onClick(Registration.java:42)
02-24 23:09:32.201: E/Verification_add(1389):   at android.view.View.performClick(View.java:4202)
02-24 23:09:32.201: E/Verification_add(1389):   at android.view.View$PerformClick.run(View.java:17340)
02-24 23:09:32.201: E/Verification_add(1389):   at android.os.Handler.handleCallback(Handler.java:725)
02-24 23:09:32.201: E/Verification_add(1389):   at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 23:09:32.201: E/Verification_add(1389):   at android.os.Looper.loop(Looper.java:137)
02-24 23:09:32.201: E/Verification_add(1389):   at android.app.ActivityThread.main(ActivityThread.java:5039)
02-24 23:09:32.201: E/Verification_add(1389):   at java.lang.reflect.Method.invokeNative(Native Method)
02-24 23:09:32.201: E/Verification_add(1389):   at java.lang.reflect.Method.invoke(Method.java:511)
02-24 23:09:32.201: E/Verification_add(1389):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-24 23:09:32.201: E/Verification_add(1389):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-24 23:09:32.201: E/Verification_add(1389):   at dalvik.system.NativeStart.main(Native Method)

谢谢 !

4

1 回答 1

0

我猜你正在做这个检查,所以你不会添加相同的名字两次或更多:使用这个方法:PS:updateRecord(String name)如果你只是想知道名字是否在数据库中,你可以打电话。

公共长插入记录(){

    ContentValues cv = new ContentValues();
    //put values

    if(!updateRecord(cv, name))
        return db.insert(P_TABLE, null, cv);
    else return 0;
}

private boolean updateRecord(ContentValues values, String name) {
    return db.update(P_TABLE, values, KEY_NAME + "=" + name, null) > 0;
}
于 2013-08-16T14:25:37.903 回答