SQLiteException:没有这样的列

时间:2014-06-28 19:16:03

标签: android android-sqlite

这就是我想要做的事情

如果某人输入“Gggfggvgvvvvvvvvvvvv”或该名称不在数据库中的任何名称,那么我试图按照“用户名不存在”的方式向用户返回消息。

这是我的问题。当用户单击“getDetails”按钮时,我收到一条令人讨厌的错误消息(下面的错误消息)。

这是我认为可行的,但它不起作用。因为如果列不存在,getColumnIndex等于-1,我想我会将index1与-1进行比较。如果该比较为真,我会向我的用户Toast一条消息,但我收到一条错误消息。

我的问题是,我该如何解决这个问题?如何让我的语法在没有我的应用程序崩溃的情况下向用户提供消息?

public void getDetails(View view){
   String theName = name.getText().toString();
   insertHelper.checkToSeeIfUserNameExist(theName);
}

public void checkToSeeIfUserNameExist(String theUserName){
    SQLiteDatabase db = helper.getWritableDatabase();

    String[] selectColoumnsFromDatabase = {
        InsertDatabaseHelper.NAME   
    };

    String selectionArgs= theUserName;
    Cursor cursor = db.query(
            InsertDatabaseHelper.TABLE_NAME, 
            selectColoumnsFromDatabase, 
            selectionArgs,  
            null, null, null, null
            );
    while(cursor.moveToNext()){
        int index1 = cursor.getColumnIndex(InsertDatabaseHelper.NAME);

        if(index1 == -1){
            SQLMessage.message(helper.context, "Username Does Not Exist");
        } else {
            SQLMessage.message(helper.context, "Something Else Is Happening");
        }

    }
}


06-28 15:04:20.189: E/AndroidRuntime(2999): FATAL EXCEPTION: main
06-28 15:04:20.189: E/AndroidRuntime(2999): java.lang.IllegalStateException: Could not execute method of the activity
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.view.View$1.onClick(View.java:2144)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.view.View.performClick(View.java:2485)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.view.View$PerformClick.run(View.java:9081)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.os.Handler.handleCallback(Handler.java:587)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.os.Looper.loop(Looper.java:130)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.app.ActivityThread.main(ActivityThread.java:3683)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at java.lang.reflect.Method.invokeNative(Native Method)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at java.lang.reflect.Method.invoke(Method.java:507)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:870)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at dalvik.system.NativeStart.main(Native Method)
06-28 15:04:20.189: E/AndroidRuntime(2999): Caused by: java.lang.reflect.InvocationTargetException
06-28 15:04:20.189: E/AndroidRuntime(2999):     at java.lang.reflect.Method.invokeNative(Native Method)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at java.lang.reflect.Method.invoke(Method.java:507)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.view.View$1.onClick(View.java:2139)
06-28 15:04:20.189: E/AndroidRuntime(2999):     ... 11 more
06-28 15:04:20.189: E/AndroidRuntime(2999): Caused by: android.database.sqlite.SQLiteException: no such column: Gggfggvgvvvvvvvvvvv: , while compiling: SELECT Name FROM SHANETABLE WHERE Gggfggvgvvvvvvvvvvv
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1235)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1189)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1271)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at com.example.multapps.InsertDatabaseAdapter.checkToSeeIfUserNameExist(InsertDatabaseAdapter.java:152)
06-28 15:04:20.189: E/AndroidRuntime(2999):     at com.example.multapps.InsertIntoDatabase.getDetails(InsertIntoDatabase.java:61)
06-28 15:04:20.189: E/AndroidRuntime(2999):     ... 14 more

3 个答案:

答案 0 :(得分:1)

这是您的错误:

while compiling: SELECT Name FROM SHANETABLE WHERE Gggfggvgvvvvvvvvvvv

您的查询应如下所示:

SELECT Name FROM SHANETABLE WHERE Name = 'Gggfggvgvvvvvvvvvvv'

然后,如果光标有 0行,则表格中不存在该名称。

答案 1 :(得分:1)

正确获取光标:

Cursor cursor = db.query(InsertDatabaseHelper.TABLE_NAME,                  // table
                         new String[] { InsertDatabaseHelper.NAME },       // columns
                         InsertDatabaseHelper.NAME + " = ? ",              // selection
                         new String[] { theUserName },                     // selectionArgs
                         null, null, null);                                // unused

您只需致电moveToNext(),即表示您没有正确检查光标。 如果没有返回匹配的行,它将返回false,因此您的代码不会运行。 通过以下方法检查Cursor中的内容是否更好:

try {
    if (null != cursor && cursor.moveToFirst()) { //moveToFirst returns false if cursor is empty!
        Toast.makeText(YourActivity.this, "Username exists!", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(YourActivity.this, "Username doesn't exist!",Toast.LENGTH_LONG).show();
    }
} finally { // ensures that your cursor is closed no matter what!
    if (null != cursor) {
        cursor.close();
    }
}

您还可以通过cursor.getCount()

检查光标中的行数

答案 2 :(得分:0)

您通常没有给定用户名的列。通常,您有一个名为&#34; username&#34;每行都有一个值。您的架构非常奇怪,或者您将列本身与列中的值混淆。