gpt4 book ai didi

Android Sqlite 如果不存在则插入

转载 作者:太空宇宙 更新时间:2023-11-03 12:10:21 25 4
gpt4 key购买 nike

在我的 android 应用程序中,我需要将数据保存到数据库中。这些值是用户名和 DOB 详细信息。所以在保存细节之前我需要检查数据是否存在。在名称中,如果名字、姓氏和出生日期相同,则无需插入数据。如果有人知道,我将在下面提供我的代码,请帮忙。

                    EditText et1 = (EditText) findViewById (R.id.editText1);
EditText et2 = (EditText) findViewById (R.id.editText2);
EditText et3 = (EditText) findViewById (R.id.editText3);
EditText et4 = (EditText) findViewById (R.id.editText7);
EditText et5 = (EditText) findViewById (R.id.editText8);
EditText et6 = (EditText) findViewById (R.id.editText9);
EditText et7 = (EditText) findViewById (R.id.editText10);
EditText et8 = (EditText) findViewById (R.id.editText11);




String fname = et1.getText().toString();

String mname = et2.getText().toString();

String lname = et3.getText().toString();

String bday = et4.getText().toString();

String bmonth = et5.getText().toString();

String byear = et6.getText().toString();

String mno = et7.getText().toString();

String pem = et8.getText().toString();

db.execSQL("INSERT INTO Data2 VALUES('"+fname+"', '"+mname+"','"+lname+"','"+bday+"', '"+bmonth+"','"+byear+"', '"+mno+"', '"+pem+"');");

Toast.makeText(NumEntry.this, "Your details are successfully saved.", Toast.LENGTH_LONG).show();

最佳答案

检查行是否已存在于数据库中,并根据此更新或插入您的数据:

 public Long saveUser(Some values) {
long rowId;
String sql = "SELECT * FROM Data2 WHERE COLUMN_1 = ? AND COLUMN_2 = ?";
Cursor cursor = getSqLiteDatabase().rawQuery(sql, new String[]{"column_value_1" ,"column_value_2"});
if (cursor == null || !cursor.moveToFirst()) {
//Insert new
rowId = getSqLiteDatabase().insert(Data2, null, yourCV);
} else {
//Update
String clause = "COLUMN_1 = ? AND COLUMN_2 = ?";
String args[] = {"column_value_1" ,"column_value_2"};
rowId = getSqLiteDatabase().update(User.DB_KEYS.TABLE.toString(), UserSqlStorage.saveUserCV(user), clause, args);
}
closeCursor(cursor);
return rowId;
}

关于Android Sqlite 如果不存在则插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24573648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com