android - 检查值是否存在 sqlite 错误插入

标签 android

我有一个方法可以插入数据并检查代码是否存在

如果存在则不创建插入,如果不存在则创建插入

但是当我尝试运行时,数据无法插入到数据库中

这是我的插入代码

public void processAddA(DialogWrapper wrapper)
               {
           ContentValues values = new ContentValues(2);

            values.put(DatabaseHelper.CODE, wrapper.getCode());
            values.put(DatabaseHelper.ALAMAT, wrapper.getAlamat());
            values.put(DatabaseHelper.BATAS, wrapper.getBatas());
            values.put(DatabaseHelper.LAT, wrapper.getLat());
            values.put(DatabaseHelper.LON, wrapper.getLon());
            values.put(DatabaseHelper.LUAS, wrapper.getLuas());
            values.put(DatabaseHelper.TANGGAL_AWAL, wrapper.getTglA());
            values.put(DatabaseHelper.TANGGAL_AKHIR, wrapper.getTglB());
            values.put(DatabaseHelper.USER_ID, wrapper.getUser());
            values.put(DatabaseHelper.SENT, "0");

                  open();
                  //check if value exist or not.
                  Cursor c = database.rawQuery("SELECT * FROM petak_tetap where code='"
                            + wrapper.getCode() + "'", null);
                  if(c == null)
                  {
                  //doesn't exists therefore insert record.
                      database.insert("petak_tetap", DatabaseHelper.ALAMAT,
                                values);
                  }
                  close();
               }

如何解决?

最佳答案

使用这个条件它会起作用

if (c!=null && c.getCount()==0)

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

相关文章:

android - Flutter:在 channel plugins.flutter.io/path_provider 上找不到方法 getApplicationDocumentsDirectory 的实现

java - 拍照失败 java.lang.runtimeexception 无法以 Intent 启动服务

android - "AndroidManifest.xml does not declare a Java package"错误

java - 如何在android中使用TextWatcher进行查询?

javascript - 如何使用phonegap和javascript播放本地媒体文件?

android - 设置添加背景颜色以滑动图像

android - USB调试如何获取数据库文件?

android - cordova "release"在 SSL 方面的行为与 "debug"不同

android - 如何在android中将布局的宽度和高度设置为百分比?

java - 安卓。 socket 。连接失败