首页 > 解决方案 > 从 SQLite 数据库检索数据时出错

问题描述

我正在尝试在我的手机上获取 SQLite 数据库值。数据在数据库中可用,但此游标类显示在数据库中不可用的值。我该如何解决这个问题?

这是databasehelper调用函数

String imgname=img_name.getText().toString();
Cursor cursor = db.pname(imgname);

if(cursor.getCount()== 0)
{
    Toast.makeText(getApplicationContext(), "Values are not available", Toast.LENGTH_SHORT).show();
}
else
{
    while (cursor.moveToFirst())
    {
        Toast.makeText(getApplicationContext(), "name"+cursor.getColumnIndex("name"), Toast.LENGTH_SHORT).show();
    }
}
cursor.close();

DatabaseHelper类

public Cursor pname(String imgname) {
    String name = imgname.toString();
    String sql = "select * from '"+MSG+"' where product='"+name+"'";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(sql, null);
    return cursor;
}

我期待我的产品名称。我如何解决它?

标签: android

解决方案


你不应该MSG在单引号中使用。您的查询应该是:

select * from tablename where ...

String sql = "select * from "+MSG+" where product='"+name+"'";

tablename不能用单引号。


推荐阅读