我想设定一个条件,如果名字和姓氏在数据库中,则分数将被更新,否则数据将作为新用户正常插入。
我的代码:
public void data(View view)
{
scor=String.valueOf(qc1.scor);
db.execSQL("UPDATE Student SET email='"+scor+"' WHERE fname ='"+exo.fname+"' AND lname ='"+exo.lname+"';");
Toast toast = Toast.makeText(getApplicationContext(), "data updated", Toast.LENGTH_LONG);
toast.show();
}
最佳答案
像这样的事情应该可以解决问题:
Cursor cursor = db.rawQuery("select 1 from Student where fname=? AND lname =?",
new String[] { exo.fname, exo.lname });
if(cursor.getCount() > 0) {
// this means we found at least one entry that matches the name
// update database here
} else {
// otherwise that user doesn't exist yet
// insert into database here
}
关于java - 如何使用sqlite在java中创建条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24226526/