无法获取android数据库中的数据库行值?

时间:2013-09-06 11:07:20

标签: android database sqlite

我有一个关于员工表单的数据库,当我传递员工电子邮件ID时,我需要获取员工详细信息。我无法获取详细信息,并且我引用了此链接Get the single row with cursor和此{{3} }。

我的代码

数据库代码

public List<String>  getContact(String eid) {

List<String> labels = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor= db.query(TABLE_EMPLOY, new String[] {KEY_EMAIL,"email"}, "email=?",new String[]{eid}, null, null, null);
if (cursor != null){
    do{
    cursor.moveToFirst();
    labels.add(cursor.getString(1));
    } while (cursor.moveToNext());
}cursor.close();
db.close();

// returning lables
return labels;

}

活动代码

    dataBase db = new dataBase(this);
    List<String> studentInfo = db.getContact(eid);
    for (String cn : studentInfo){
        Toast.makeText(getApplicationContext(),cn.toString(), Toast.LENGTH_LONG).show();
    }

错误日志

09-06 16:56:27.330: E/AndroidRuntime(21699): FATAL EXCEPTION: main
09-06 16:56:27.330: E/AndroidRuntime(21699): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
09-06 16:56:27.330: E/AndroidRuntime(21699):    at android.app.LoadedApk.makeApplication(LoadedApk.java:482)
09-06 16:56:27.330: E/AndroidRuntime(21699):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3938)
09-06 16:56:27.330: E/AndroidRuntime(21699):    at android.app.ActivityThread.access$1300(ActivityThread.java:123)
09-06 16:56:27.330: E/AndroidRuntime(21699):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
09-06 16:56:27.330: E/AndroidRuntime(21699):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 16:56:27.330: E/AndroidRuntime(21699):    at android.os.Looper.loop(Looper.java:137)
09-06 16:56:27.330: E/AndroidRuntime(21699):    at android.app.ActivityThread.main(ActivityThread.java:4424)
09-06 16:56:27.330: E/AndroidRuntime(21699):    at java.lang.reflect.Method.invokeNative(Native Method)
09-06 16:56:27.330: E/AndroidRuntime(21699):    at java.lang.reflect.Method.invoke(Method.java:511)
09-06 16:56:27.330: E/AndroidRuntime(21699):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-06 16:56:27.330: E/AndroidRuntime(21699):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-06 16:56:27.330: E/AndroidRuntime(21699):    at dalvik.system.NativeStart.main(Native Method)
09-06 16:56:27.330: E/AndroidRuntime(21699): Caused by: java.lang.NullPointerException
09-06 16:56:27.330: E/AndroidRuntime(21699):    at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:362)
09-06 16:56:27.330: E/AndroidRuntime(21699):    at android.app.LoadedApk.getClassLoader(LoadedApk.java:305)
09-06 16:56:27.330: E/AndroidRuntime(21699):    at android.app.LoadedApk.makeApplication(LoadedApk.java:474)
09-06 16:56:27.330: E/AndroidRuntime(21699):    ... 11 more

修改 当我传递电子邮件ID时,我需要获取ID,姓名,昵称,用户名,密码,手机号

1 个答案:

答案 0 :(得分:0)

确保在第一次调用数据库时已创建数据库。 e.g

public Database(Context context) { 
   super(context, DATABASE_NAME, null, DATABASE_VERSION); 
}

@Override
public void onCreate(SQLiteDatabase db) {
     String CREATE_EMPLOY_TABLE = "CREATE TABLE " + TABLE_EMPLOY + "("
             + KEY_ID + " INTEGER PRIMARY KEY," 
             + KEY_NAME + " TEXT,"
             + KEY_EMAIL + " TEXT"
             + ")";
      db.execSQL(CREATE_EMPLOY_TABLE);
}

此时验证您的查询

Cursor cursor = db.query(TABLE_EMPLOY, new String[] { 
           KEY_ID,
           KEY_NAME,
           KEY_EMAIL
}, KEY_EMAIL + " = ?", new String[] { String.valueOf(eid) }, null, null, null, null);