【发布时间】:2018-10-03 21:37:42
【问题描述】:
我有这些方法
public void insert(ContentValues values){
try {
database = dbhelper.getWritableDatabase();
database.insertOrThrow(dbhelper.TABLE_NAME, null, values);
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
//releasing the resources.
database.close();
dbhelper.close();
}
}
public Cursor query(){
database=dbhelper.getReadableDatabase();
Cursor cur=database.query(DbHelper.TABLE_NAME, null, null, null, null, null, null);
return cur;
}
一个插入数据,另一个从我的数据库中查询数据。
现在我在数据库中插入一些数据
values = new ContentValues();
values.put("email",email);
values.put("pass",password);
values.put("contact",contact);
DetailsDb detailsDb = new DetailsDb(this);
Cursor cursor = detailsDb.query();
if(cursor.getCount() > 0){
Toast.makeText(this,"Ooops user exists",Toast.LENGTH_SHORT).show();
}else{
detailsDb.insert(values);
Toast.makeText(this,"You are registered",Toast.LENGTH_SHORT).show();
}
基本上,我是说。如果数据库中有一行(cursor.getCount > 0),则用户存在。如果没有,则插入新用户。但是,如果我再次输入同一个用户,我会收到“您已注册”消息。所以那里出了点问题。有什么想法吗?
谢谢,
西奥。
【问题讨论】:
-
因为查询函数没有检查数据是否存在