gpt4 book ai didi

android sqlite 检查是否插入了新值

转载 作者:IT王子 更新时间:2023-10-29 06:21:00 24 4
gpt4 key购买 nike

我正在使用 sqlite。我成功地创建了数据库和表。我还编写了可以在我的表中插入新值的代码。我的代码运行完美,但现在我想显示例如:如果插入新值,则显示 toast 消息,否则在 toast 或其他内容中显示错误消息。这是我插入到表的源代码:

public void InsertToPhysicalPersonTable(String FirstName, String LastName,
String FullName, String FatherName) {
try {
ContentValues newValues = new ContentValues();
newValues.put("FirstName", FirstName);
newValues.put("LastName", LastName);
newValues.put("FullName", FullName);
newValues.put("FatherName", FatherName);



db.insert(AddNewPhysicalPerson, null, newValues);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Toast.makeText(myContext, "Something wrong", Toast.LENGTH_SHORT).show();
}

}

我这样调用我的函数:

loginDataBaseAdapter.InsertToPhysicalPersonTable("FirstName",
"LastName",
"FullName",
"FatherName"
);

如果有人知道解决方案,请帮助我。谢谢

最佳答案

insert() 方法返回新插入行的行 ID,如果发生错误则返回 -1。

改变

db.insert(AddNewPhysicalPerson, null, newValues);

像这样

long rowInserted = db.insert(AddNewPhysicalPerson, null, newValues);
if(rowInserted != -1)
Toast.makeText(myContext, "New row added, row id: " + rowInserted, Toast.LENGTH_SHORT).show();
else
Toast.makeText(myContext, "Something wrong", Toast.LENGTH_SHORT).show();

关于android sqlite 检查是否插入了新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27863609/

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