Android 在 Sqlite 中按 Id 选择行?

发布于 2024-11-04 00:26:48 字数 1064 浏览 0 评论 0原文

我想在android sqlite中按id获取特定行并编写了以下代码,但它不返回任何记录。我编写了一个 getAllRecords() 方法,它返回数据库中的所有记录。

谁能解释我犯的错误?

public Bank getBankById(int bankId)
    {

        Cursor cursor=null;
        Bank bnk = null;
        cursor =  this.db.rawQuery("select * from " + BanksTable.NAME + " where " + BanksTable.COL_ID + "=" + bankId  , null);
         if (cursor != null)
            {
             if (cursor.moveToFirst())
                {
             int id = cursor.getInt(cursor.getColumnIndex(BanksTable.COL_ID));
             String name = cursor.getString(cursor.getColumnIndex(BanksTable.COL_NAME));
             String url = cursor.getString(cursor.getColumnIndex(BanksTable.COL_IMAGE_URL));
             byte[] image = cursor.getBlob(cursor.getColumnIndex(BanksTable.COL_IMAGE));
             bnk=new Bank();
             bnk.setId(id);
             bnk.setImageURL(url);
             bnk.setName(name);
             bnk.setImageByteArray(image);
                }
             cursor.close();
            }
        return bnk;

    }

I want to get a specific row by id in android sqlite and have written the following code, but it does not return any records. I have written a getAllRecords() method which returns all records from database.

Can anyone explain mistake I am making?

public Bank getBankById(int bankId)
    {

        Cursor cursor=null;
        Bank bnk = null;
        cursor =  this.db.rawQuery("select * from " + BanksTable.NAME + " where " + BanksTable.COL_ID + "=" + bankId  , null);
         if (cursor != null)
            {
             if (cursor.moveToFirst())
                {
             int id = cursor.getInt(cursor.getColumnIndex(BanksTable.COL_ID));
             String name = cursor.getString(cursor.getColumnIndex(BanksTable.COL_NAME));
             String url = cursor.getString(cursor.getColumnIndex(BanksTable.COL_IMAGE_URL));
             byte[] image = cursor.getBlob(cursor.getColumnIndex(BanksTable.COL_IMAGE));
             bnk=new Bank();
             bnk.setId(id);
             bnk.setImageURL(url);
             bnk.setName(name);
             bnk.setImageByteArray(image);
                }
             cursor.close();
            }
        return bnk;

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵你的手,一向走下去 2024-11-11 00:26:49

我通过将参数更改为名称结束了这个问题。
这很奇怪,但我仍然不知道为什么它不起作用。

我得到像 0,1,2,3,4 这样的银行 ID,也许 SQLite 不允许它或内部影响它,但我仍然不确定。

cursor =  this.db.rawQuery("select * from " + BanksTable.NAME + " where " + BanksTable.COL_NAME + "='" + bankName + "'" , null);

I ended up this problem by changing parameter to name.
Its weird but I still don't know why it was not working.

I was getting bank id like 0,1,2,3,4 maybe it is not allowed in SQLite or internally affecting it but still I am not sure.

cursor =  this.db.rawQuery("select * from " + BanksTable.NAME + " where " + BanksTable.COL_NAME + "='" + bankName + "'" , null);
请叫√我孤独 2024-11-11 00:26:49

这是我从 sqlite db 获取 custom 对象的方法

    fun getTaskById(itemId: Int): ModelTask {
    val food= ModelTask()

    val database = readableDatabase
    val kolonlar = arrayOf(DBContract.TaskEntry.COLUMN_T_ID,
            DBContract.TaskEntry.COLUMN_T_TITLE,
            DBContract.TaskEntry.COLUMN_T_DESC,
            DBContract.TaskEntry.COLUMN_T_TIME,
            DBContract.TaskEntry.COLUMN_T_PRIORITY,
            DBContract.TaskEntry.COLUMN_T_DATE,
            DBContract.TaskEntry.COLUMN_T_DONE)
    val whereClause =  DBContract.TaskEntry.COLUMN_T_ID + " = ? "
    val whereArgs = arrayOf(itemId.toString())
    val cursor = database.query( DBContract.TaskEntry.TABLE_NAME_TASK, kolonlar, whereClause, whereArgs,
            null, null,  DBContract.TaskEntry.COLUMN_T_ID + " DESC")

    while (cursor.moveToNext()) {

        food.id = cursor.getInt(cursor.getColumnIndex(DBContract.TaskEntry.COLUMN_T_ID))
        food.title = cursor.getString(cursor.getColumnIndex(DBContract.TaskEntry.COLUMN_T_TITLE))
        food.description = cursor.getString(cursor.getColumnIndex(DBContract.TaskEntry.COLUMN_T_DESC))
        food.time = cursor.getString(cursor.getColumnIndex(DBContract.TaskEntry.COLUMN_T_TIME))
        food.priority = cursor.getString(cursor.getColumnIndex(DBContract.TaskEntry.COLUMN_T_PRIORITY))
        food.date = cursor.getString(cursor.getColumnIndex(DBContract.TaskEntry.COLUMN_T_DATE))
        food.done = cursor.getInt(cursor.getColumnIndex(DBContract.TaskEntry.COLUMN_T_DONE))
     }
    database.close()
    cursor.close()
    return foodList
}

Here is my approach to get custem object from sqlite db

    fun getTaskById(itemId: Int): ModelTask {
    val food= ModelTask()

    val database = readableDatabase
    val kolonlar = arrayOf(DBContract.TaskEntry.COLUMN_T_ID,
            DBContract.TaskEntry.COLUMN_T_TITLE,
            DBContract.TaskEntry.COLUMN_T_DESC,
            DBContract.TaskEntry.COLUMN_T_TIME,
            DBContract.TaskEntry.COLUMN_T_PRIORITY,
            DBContract.TaskEntry.COLUMN_T_DATE,
            DBContract.TaskEntry.COLUMN_T_DONE)
    val whereClause =  DBContract.TaskEntry.COLUMN_T_ID + " = ? "
    val whereArgs = arrayOf(itemId.toString())
    val cursor = database.query( DBContract.TaskEntry.TABLE_NAME_TASK, kolonlar, whereClause, whereArgs,
            null, null,  DBContract.TaskEntry.COLUMN_T_ID + " DESC")

    while (cursor.moveToNext()) {

        food.id = cursor.getInt(cursor.getColumnIndex(DBContract.TaskEntry.COLUMN_T_ID))
        food.title = cursor.getString(cursor.getColumnIndex(DBContract.TaskEntry.COLUMN_T_TITLE))
        food.description = cursor.getString(cursor.getColumnIndex(DBContract.TaskEntry.COLUMN_T_DESC))
        food.time = cursor.getString(cursor.getColumnIndex(DBContract.TaskEntry.COLUMN_T_TIME))
        food.priority = cursor.getString(cursor.getColumnIndex(DBContract.TaskEntry.COLUMN_T_PRIORITY))
        food.date = cursor.getString(cursor.getColumnIndex(DBContract.TaskEntry.COLUMN_T_DATE))
        food.done = cursor.getInt(cursor.getColumnIndex(DBContract.TaskEntry.COLUMN_T_DONE))
     }
    database.close()
    cursor.close()
    return foodList
}
妥活 2024-11-11 00:26:49

我通过在创建表时向 id 添加“自动增量”属性解决了这个问题

db.execSQL("CREATE TABLE IF NOT EXISTS " + BanksTable.NAME +" ( " +BanksTable.COL_ID + " 
integer PRIMARY KEY **autoincrement**, "+ BanksTable.COL_NAME + " varchar, " + 
BanksTable.COL_IMAGE_URL + " varchar," + BanksTable.COL_IMAGE + " blob);");

I solved this issue by adding "autoincrement" property to id, while creating the table

db.execSQL("CREATE TABLE IF NOT EXISTS " + BanksTable.NAME +" ( " +BanksTable.COL_ID + " 
integer PRIMARY KEY **autoincrement**, "+ BanksTable.COL_NAME + " varchar, " + 
BanksTable.COL_IMAGE_URL + " varchar," + BanksTable.COL_IMAGE + " blob);");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文