Android显示数据库记录列表(sqlite)

时间:2013-01-28 01:26:19

标签: android database sqlite

我想在我的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中显示的用户名列表

2 个答案:

答案 0 :(得分: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});

答案 1 :(得分: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});