Android在Sqlite中按ID选择行?

时间:2011-04-28 10:25:19

标签: android sqlite

我想在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;

    }

3 个答案:

答案 0 :(得分:10)

我通过将参数更改为name来结束此问题。 它很奇怪,但我仍然不知道它为什么不起作用。

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

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

答案 1 :(得分:0)

我通过在创建表

时向id添加“autoincrement”属性来解决了这个问题
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);");

答案 2 :(得分:0)

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

<?php
$id = $_GET['id'];
$name = $_SESSION['u_uname'];
$file = $_FILES['fileToUpload']['name'];
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$message = mysqli_real_escape_string($conn,$_POST['message']);
$daterep = date('Y-m-d H:i:s');

$qry = "INSERT INTO forum_answer (topicid,name,message,daterep,file) VALUES 
('$id','$name','$message','$daterep','$target_file')";
$log = mysqli_query($conn,$qry);

move_uploaded_file($_FILES['fileToUpload']['tmp_name'],$target_dir.$file);
?>