android sqlite检查是否插入了新值 [英] android sqlite check if inserted new value
本文介绍了android sqlite检查是否插入了新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在与sqlite合作。我成功创建了数据库和表。我还编写了可以在表中插入新值的代码。我的代码运行良好,但是现在我想显示例如:吐司消息(如果插入了新值),否则在吐司中显示错误消息或其他内容。
这是我插入表的源代码:
I'm working with sqlite. I successfully created database and table. I also wrote code which can insert new values in my table. My code is working perfect, but now I want to show for example: toast message if inserted new value, else show error message in toast or something else. This is a my insert to table source code:
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();
}
}
我这样调用我的函数:
I called my function like this:
loginDataBaseAdapter.InsertToPhysicalPersonTable("FirstName",
"LastName",
"FullName",
"FatherName"
);
如果有人知道解决方案,请帮助我。
谢谢
If anyone knows the solution, please help me. Thanks
推荐答案
insert()
方法返回行新插入的行的ID;如果发生错误,则为-1。
insert()
method returns the row ID of the newly inserted row, or -1 if an error occurred.
更改
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检查是否插入了新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文