android - 从 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();
DatabaseHelpe
r类
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;
}
我期待我的产品名称。我如何解决它?
解决方案
你不应该MSG
在单引号中使用。您的查询应该是:
select * from tablename where ...
String sql = "select * from "+MSG+" where product='"+name+"'";
tablename
不能用单引号。
推荐阅读
- reactjs - FormatJS / React-intl 中的 if/else
- javascript - 每次出现在字符串中时,用新值替换部分字符串,JavaScript
- angular - 如何在 Angular 中保持 FormGroup 的状态
- javascript - 为什么在 asp.net mvc 中加载模型很慢?
- java - 在 Spring Boot 中使用环境变量
- actions-on-google - 具有相同命令的设备
- javascript - CSS 样式不适用于 React 标题和段落
- linux - 如何获得 CPU 指令序列的准确性能配置文件?
- python - 扩展 ast.AST
- azure - CosmosDB 重音(变音符号)不敏感查询