我有一个列表视图,其中填充了通过SQLite在数据库中输入的名称。当我单击其中一个名称时,应用程序崩溃。
请,如果您能找到问题,我将不胜感激。谢谢!
我添加了从列表中单击名称时出现的logcat部分。 COL0 =“ ID”,COL1 =“名称”。
OnClick方法:
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.
答案 0 :(得分:0)
字符串值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()
这样,您不必担心单引号,因为这将由方法<log4net debug="false">
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:\temp\DotNetEngine.log" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="2" />
<param name="MaximumFileSize" value="500KB" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %level %logger - %message%exception%newline" />
</layout>
</appender>
<root>
<level value="debug" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
处理。