gpt4 book ai didi

android - 获取 SQLiteDatabase 中的特定行不起作用

转载 作者:行者123 更新时间:2023-11-28 23:27:23 25 4
gpt4 key购买 nike

我是 SQLiteDatabase 的新开发人员和新手,遇到这个小问题.. 我需要一点帮助我正在创建一个简单的 to_do 应用程序,它有目录,该应用程序可以创建类别并将它们显示在 ListView 中,通过单击刚刚创建的类别项目,我可以添加新的 to_do 任务并使用此类别名称在新的 ListView 中显示它们

这是我的数据库:

public class DatabaseOpenHelper extends SQLiteOpenHelper {
public static final String ITEM_NAME_TABLE="items_table";
public static final String TYPE_NAME_TABLE="types_table";
public static final String ITEM_NAME="name";
public static final String ITEM_PRICE="price";
public static final String ITEM_IMAGE_NAME="img_name";
public static final String ITEM_TYPE="item_type";
public static final String ID="_id";
}

我想按点击的类别查看项目,所以我写了这个方法:

public Cursor getlAllGifts(String category){
return database.query(DatabaseOpenHelper.ITEM_NAME_TABLE, null, DatabaseOpenHelper.ITEM_TYPE+"="+ category, null, null, null, null);
}

我从先前的 Intent 中传递了类别,但是当调用这个方法时我得到了这个错误

08-03 06:59:59.120: E/SQLiteLog(2696): (1) no such column: personal
08-03 06:59:59.120: W/dalvikvm(2696): threadid=12: thread exiting with uncaught exception (group=0xa4bfd648)
08-03 06:59:59.120: E/AndroidRuntime(2696): FATAL EXCEPTION: AsyncTask #2
08-03 06:59:59.120: E/AndroidRuntime(2696): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-03 06:59:59.120: E/AndroidRuntime(2696): at java.lang.Thread.run(Thread.java:841)
08-03 06:59:59.120: E/AndroidRuntime(2696): Caused by: android.database.sqlite.SQLiteException: no such column: personal (code 1): , while compiling: SELECT * FROM items_table WHERE item_type=personal
08-03 06:59:59.120: E/AndroidRuntime(2696): Caused by: android.database.sqlite.SQLiteException: no such column: personal (code 1): , while compiling: SELECT * FROM items_table WHERE item_type=personal
08-03 06:59:59.120: E/AndroidRuntime(2696): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

我有个人类别,但我不知道如何按特定类别(item_type)获取行,请帮助我

最佳答案

你能试试下面的查询吗?

如果可行,那么,我会用更多详细信息更新答案:

public Cursor getlAllGifts(String category){
return database.query(DatabaseOpenHelper.ITEM_NAME_TABLE, null, DatabaseOpenHelper.ITEM_TYPE + " LIKE ? ", new String[]{category}, null, null, null, null);
}

背景

如果您检查 DOCS ,您可以看到该方法 query() 接受以下参数:

query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) 

您的代码是:

database.query(DatabaseOpenHelper.ITEM_NAME_TABLE, null, DatabaseOpenHelper.ITEM_TYPE+"="+ category, null, null, null, null)

因此,您在正确的位置设置了WHERE 语句。但是,你做得不对。然后,您的查询被转换为:

SELECT * FROM items_table WHERE item_type=personal

然后,SQLite 将 personal 作为列处理(而不是作为您的 where 条件)

让 Android 来解决问题

事实上,您可以使用 query() 的第四个参数 (selectionArgs[]) 然后,Android 将创建 where条款给你......你只需发送参数/参数。

这样,在第 3 个参数中,您添加了 ?,在第 4 个参数中,您添加了 android 应该插入的参数来代替 ?

因此,在我的示例中,我添加了:

  • 第三个参数:DatabaseOpenHelper.ITEM_TYPE + "LIKE ? "
  • 第四个参数:new String[]{category}

然后 Android 将用您的参数 category 替换 ?

关于android - 获取 SQLiteDatabase 中的特定行不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38743996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com