sqlite查询错误,使应用程序崩溃



我有一个列表视图,我填充了通过SQLite在数据库中输入的名称。当我单击其中一个名称时,应用程序崩溃了。

请,如果您能找到问题,我会很感激。谢谢你!

我添加了我单击列表名称时出现的logcat的一部分。col0 =" id"和col1 =" name"。

on Click方法:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String name = adapterView.getItemAtPosition(i).toString();
                Log.e(TAG, name);
                Log.e(TAG, "" + i);
                Cursor player = dbHelper.getItemID(name);  //this is line 51 -> see logcat
                int itemID = -1;
                while(player.moveToNext())
                    itemID = player.getInt(0);
                if(itemID > -1) {
                    Intent intent = new Intent(PlayersList.this, PlayerCharacters.class);
                    intent.putExtra("id", itemID);
                    intent.putExtra("name", name);
                    startActivity(intent);
                } else Toast.makeText(PlayersList.this, "No ID associated with that name.", Toast.LENGTH_SHORT).show();
            }
        });
    }

数据库类中的getItemid((方法:

public Cursor getItemID(String name) {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT " + COL0 + " FROM " + TABLE_NAME + " WHERE " + COL1 + " = " + name;
        Cursor data = db.rawQuery(query, null);  //this is line 62 -> see logcat
        return data;
    }

单击项目时收到的错误日志(从列表中的名称(:

D/AbsListView: onTouchUp() mTouchMode : 0
E/PlayersListActivity: Eu
    3
E/SQLiteLog: (1) no such column: Eu
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.characters, PID: 30728
    android.database.sqlite.SQLiteException: no such column: Eu (code 1): , while compiling: SELECT ID FROM PlayersTable WHERE Name = Eu
    #################################################################
    Error Code : 1 (SQLITE_ERROR)
    Caused By : SQL(query) error or missing database.
        (no such column: Eu (code 1): , while compiling: SELECT ID FROM PlayersTable WHERE Name = Eu)
    #################################################################
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1096)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:661)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1746)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1685)
        at com.example.characters.DatabaseHelper.getItemID(DatabaseHelper.java:62)  //---->here
        at com.example.characters.PlayersList$1.onItemClick(PlayersList.java:51)   //----->here
        at android.widget.AdapterView.performItemClick(AdapterView.java:350)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1683)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:4094)
        at android.widget.AbsListView$10.run(AbsListView.java:6583)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Application terminated.

字符串值 name必须包含在单引号中:

String query = "SELECT " + COL0 + " FROM " + TABLE_NAME + " WHERE " + COL1 + " = '" + name + "'";

没有这些单个引号,字符串值name被视为列名,并且由于表格中不存在这样的列,因此该应用程序崩溃了,例如:

no such column: Eu

但是,将参数传递到查询的安全和建议的方法是:

String query = "SELECT " + COL0 + " FROM " + TABLE_NAME + " WHERE " + COL1 + " = ?";
Cursor data = db.rawQuery(query, new String[] {name});

这样,您不必担心单引号,因为这将由方法rawQuery()来处理。

相关内容

  • 没有找到相关文章

最新更新