我正在使用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();
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!