0

我想在我的 android 应用程序中显示名称为“Bernadette”的项目列表。我的代码如下所示:

public List<WebsiteRecords> getAllWebsite() {
    List<WebsiteRecords> websites = new ArrayList<WebsiteRecords>();
    Cursor cursor = database.rawQuery("SELECT * FROM " + MySQLiteHelper.TABLE_URL + "WHERE user=?", new String[]{MySQLiteHelper.COLUMN_USER});
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        WebsiteRecords website = cursorToWebsite(cursor);
        websites.add(website);
        cursor.moveToNext();
    }
    cursor.close();
    return websites;
}

private WebsiteRecords cursorToWebsite(Cursor cursor) {
    WebsiteRecords website = new WebsiteRecords();
    website.setId(cursor.getInt(0));
    website.setUser(cursor.getString(1));
    website.setSitename(cursor.getString(2));
    website.setUsername(cursor.getString(3));
    website.setPassword(cursor.getString(4));
    return website;
}

但是当我运行它时,它说应用程序已经停止工作并且我的 logcat 说:

01-28 09:07:26.922: E/AndroidRuntime(835): FATAL EXCEPTION: main
01-28 09:07:26.922: E/AndroidRuntime(835): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bernadette.remembermypassword/com.bernadette.remembermypassword.PasswordActivity}: android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: SELECT * FROM websitesWHERE user=?
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.os.Looper.loop(Looper.java:137)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-28 09:07:26.922: E/AndroidRuntime(835):  at java.lang.reflect.Method.invokeNative(Native Method)
01-28 09:07:26.922: E/AndroidRuntime(835):  at java.lang.reflect.Method.invoke(Method.java:511)
01-28 09:07:26.922: E/AndroidRuntime(835):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-28 09:07:26.922: E/AndroidRuntime(835):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-28 09:07:26.922: E/AndroidRuntime(835):  at dalvik.system.NativeStart.main(Native Method)
01-28 09:07:26.922: E/AndroidRuntime(835): Caused by: android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: SELECT * FROM websitesWHERE user=?
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
01-28 09:07:26.922: E/AndroidRuntime(835):  at com.bernadette.remembermypassword.DataSource.getAllWebsite(DataSource.java:62)
01-28 09:07:26.922: E/AndroidRuntime(835):  at com.bernadette.remembermypassword.PasswordActivity.show_list_layout(PasswordActivity.java:172)
01-28 09:07:26.922: E/AndroidRuntime(835):  at com.bernadette.remembermypassword.PasswordActivity.onCreate(PasswordActivity.java:42)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.app.Activity.performCreate(Activity.java:5008)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-28 09:07:26.922: E/AndroidRuntime(835):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-28 09:07:26.922: E/AndroidRuntime(835):  ... 11 more

你能帮我解决这个问题吗?我有一个名为 users 的数据库,其中包含 _id、user、site、username、password 列。当我登录我的应用程序时,我想显示具有相同“用户”名称的项目列表。我该怎么办?

我还想知道我是否输入了正确的代码集,因为当我继续运行它时,它仍然显示数据库中的所有记录,而不仅仅是显示我在 Toast 中显示的用户名的列表

4

2 回答 2

1
Cursor cursor = database.rawQuery("SELECT * FROM " + MySQLiteHelper.TABLE_URL + "WHERE user=?", new String[]{MySQLiteHelper.COLUMN_USER});

您需要在 之前的另一个空格WHERE,将行更改为

Cursor cursor = database.rawQuery("SELECT * FROM " + MySQLiteHelper.TABLE_URL + " WHERE user=?", new String[]{MySQLiteHelper.COLUMN_USER});
于 2013-01-28T01:30:16.757 回答
0

你的问题在

" WHERE user=?", new String[]{MySQLiteHelper.COLUMN_USER});

改变“?”的值 直接地。

采用

 database.rawQuery("SELECT * FROM " + MySQLiteHelper.TABLE_URL);

代替

database.rawQuery("SELECT * FROM " + MySQLiteHelper.TABLE_URL + "WHERE user=?", new String[]{MySQLiteHelper.COLUMN_USER});
于 2013-01-28T01:30:26.257 回答