【发布时间】:2016-04-28 05:01:16
【问题描述】:
我想检查 SQLite 中的数据是否已经存在可以更新或插入。我正在检查下面提到的代码。
代码:
public long addmenus(String navigationdrawer,String optionname)
{
SQLiteDatabase menus=this.getWritableDatabase();
try {
ContentValues values=new ContentValues();
values.put(HEADER_NAME,navigationdrawer);
values.put(CHILD_NAME,optionname);
// menus.insert(TABLE_NAME,null,values);
// String owner=optionname;
Cursor cursor = menus.rawQuery("select * from TABLE_NAME where CHILD_NAME ='"+ optionname +"'", null);
if(cursor.getCount()<1)
{
//execute insert query here
long rows = menus.insert(TABLE_NAME, null, values);
return rows;
// return rows inserted.
}
else
{
//Perform the update query
String strFilter = "CHILD_NAME" + optionname;
long updaterow=menus.update(TABLE_NAME,values,strFilter,null);
return updaterow;
// return rows updated.
}
// menus.close();
} catch (Exception e) {
return -1;
}
finally {
if (menus != null)
menus.close();
}
}
我的活动:
我将整个 json 数据转换为字符串对象,然后插入到 SQLite 中。
String productpage=jsonObject.toString();
db.addmenus(productpage,"Navigationmenus");
但它不起作用。它无法插入sqlite。
任何人解决这个问题很高兴感谢。 提前致谢
【问题讨论】:
-
您遇到什么错误?此外,您似乎在使用
TABLE_NAME和CHILD_NAME作为字符串,而不是它们看起来的常量。 -
表名是我的表名,有两列子名和标题名。我必须将整个 json 数据转换为字符串插入到列标题名称中,我将给出一个选项名称,例如。一个 json 的菜单,然后是另一个 json 的第二个产品选项
-
您将 JSON 存储到 Sqlite 中?您可能希望改为使用 Realm 作为数据库
-
我不知道 Realm 如何使用它
标签: android android-layout android-fragments android-sqlite android-studio-2.0