我正在使用 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。
Change
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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)