android sqlite检查是否插入新值

android sqlite检查是否插入新值,第1张

概述我正在使用sqlite.我成功创建了数据库和表.我还编写了可以在我的表中插入新值的代码.我的代码工作正常,但现在我想显示例如:如果插入新值,则显示toast消息,否则在toast或其他内容中显示错误消息.这是我对表源代码的插入:publicvoidInsertToPhysicalPersonTable(StringFirstName

我正在使用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检查是否插入新值全部内容,希望文章能够帮你解决android sqlite检查是否插入新值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1096772.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存