尝试将传递到函数中的数据与数据库中的数据进行比较时,Where子句不起作用

时间:2012-12-31 07:37:21

标签: android sqlite android-sqlite

我正在尝试比较两个值;一个在数据库中,另一个在数据库类中传递给具有查询的方法。当我尝试运行时,应用程序崩溃了。

发送数据的代码:

projectdatabase db = new projectdatabase(this); 
    String gotid = db.getProjectName(id);

执行查询的数据库类中的方法代码:

public String getProjectName(String id) {                   

    String[] columns = new String[] {PROJECT_NAME,KEY_ROWID};
    Cursor c = projectDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + id, null, null, null, null);
    String result = "";

    int iID = c.getColumnIndex(PROJECT_ID);

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        result = result + c.getString(iID);
    }

    return result;
}

我的where子句有什么问题吗?

编辑:

在Arash回答之后实施的代码:

public String getProjectName(String id) {                   

    String[] columns = new String[] {PROJECT_NAME,KEY_ROWID};
    Cursor c = projectDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + id, null, null, null, null);
    String result = "";

    if(c.moveToFirst())
    {
       int iName = c.getColumnIndex(PROJECT_NAME);

       for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            result = result + c.getString(iName);
        }
    }

    return result;
}

Log Cat:

12-31 15:25:15.193: W/dalvikvm(2134): threadid=1: thread exiting with uncaught   
exception (group=0x409c01f8)
12-31 15:25:15.257: I/dalvikvm(2134): threadid=3: reacting to signal 3
12-31 15:25:15.355: E/AndroidRuntime(2134): FATAL EXCEPTION: main
12-31 15:25:15.355: E/AndroidRuntime(2134): java.lang.RuntimeException: Unable to start    
activity ComponentInfo{com.kk.project/com.kk.project.ViewProjectDetails}:  
java.lang.NullPointerException
12-31 15:25:15.355: E/AndroidRuntime(2134):     at  
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at 
android.app.ActivityThread.access$600(ActivityThread.java:123)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at 
android.os.Handler.dispatchMessage(Handler.java:99)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at   
android.os.Looper.loop(Looper.java:137)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at 
android.app.ActivityThread.main(ActivityThread.java:4424)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at  
java.lang.reflect.Method.invokeNative(Native Method)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at 
java.lang.reflect.Method.invoke(Method.java:511)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at  
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at  
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at  
dalvik.system.NativeStart.main(Native Method)
12-31 15:25:15.355: E/AndroidRuntime(2134): Caused by: java.lang.NullPointerException
12-31 15:25:15.355: E/AndroidRuntime(2134):     at  
com.kk.project.projectdatabase.getProjectName(projectdatabase.java:123)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at   
com.kk.project.ViewProjectDetails.onCreate(ViewProjectDetails.java:19)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at   
android.app.Activity.performCreate(Activity.java:4465)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at   
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-31 15:25:15.355: E/AndroidRuntime(2134):     at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

12-31 15:25:15.355:E / AndroidRuntime(2134):... 11更多

1 个答案:

答案 0 :(得分:0)

在使用游标列以避免NPE之前,请务必检查光标中是否有行:

而不是:

int iID = c.getColumnIndex(PROJECT_ID);

使用:

if(c.MoveToFirst())
{
   int iID = c.getColumnIndex(PROJECT_ID);
}

编辑:光标选择列中没有“PROJECT_ID”列

第二次编辑:

使用Like This获取光标的内容:

String T = c.getString(c.getColumnIndex(KEY_Your_STRING));
Int I = c.getInt(c.getColumnIndex(KEY_Your_INTEGER))

并且您已将Project_Name指定为整数值!