我尝试仅在电话号码(号码)不存在的情况下插入数据库中的某些值(消息正文和日期),但我的代码不起作用。你能提一些建议吗?
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("INSERT INTO version2 (number, body, date) values ("+number+","+body+","+ date+") SELECT "+number+" WHERE NOT EXISTS "
+ "(SELECT 1 FROM version2 WHERE number = "+number+")", null);
if (cursor.moveToFirst()) {
do {
} while (cursor.moveToNext());
}
}
答案 0 :(得分:0)
尝试此查询。
在插入记录时,似乎您的选择查询不正确。
INSERT INTO version2 (number, body, date) values ("+number+","+body+","+ date+") WHERE number IS NOT NULL or number <> '';
答案 1 :(得分:0)
您需要创建一个检查重复电话号码的方法。
private boolean isPhoneExists ( long number )
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select phone from version2 where number = " + number, null );
if (cursor.getCount() > 0)
return true;
else
return false;
}
//现在你的插入声明
if ( !isPhoneExists ( "987654321" ) )
{
Cursor cursor = db.rawQuery("INSERT INTO version2 (number, body, date) values ("+number+","+body+","+ date+")
....
}