检查数据库android中的重复数据

时间:2012-06-18 02:58:25

标签: android database sqlite

我有应用程序与数据库
我想在进入数据库之前检查数据库中的数据 如果数据与数据库相等则无法进入,如果数据不相等则可以进入
这是我的一些代码:

public void saveData(){
    long tim = System.currentTimeMillis();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
    String curTime = df.format(tim);
    Cursor c = helper.getNoOrder();
    c.moveToFirst();
    if(c.getCount() != 0){
        System.out.println("klik");
        if(noOrder.getText().toString().equals(c.getString(0))){
            Toast.makeText(this, "No Order Sudah Terdaftar", Toast.LENGTH_LONG).show();
        }else{
            helper.insertBeli123(noOrder.getText().toString(), item.getText().toString(), 
                    custName.getText().toString(), custTelp.getText().toString(),
                    qty.getText().toString(),temp.getText().toString(),temp.getText().toString(),
                    temp.getText().toString(), curTime);
            Toast.makeText(this, "Data berhasil disimpan", Toast.LENGTH_LONG).show();
            finish();
        }
    }else{
        helper.insertBeli123(noOrder.getText().toString(), item.getText().toString(), 
                custName.getText().toString(), custTelp.getText().toString(),
                qty.getText().toString(),temp.getText().toString(),temp.getText().toString(),
                temp.getText().toString(), curTime);
        Toast.makeText(this, "Data berhasil disimpan", Toast.LENGTH_LONG).show();
        finish();
    }
    //startActivity(new Intent(this,Direktori.class));
}

在我的DBhelper中这样:

public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE tanda_terima (_id integer primary key AUTOINCREMENT,no_order text " +
            ",item text,cust_id text,cust_name text,cust_telp text,quantity text,order_nik text" +
            ",pengirim text,penerima text,gambar text,waktu_ambil datetime,flag integer)");
    db.execSQL("CREATE TABLE image (_id integer primary key AUTOINCREMENT,no_order text ,image text,tgl_buat datetime,flag integer)");
    db.execSQL("CREATE TABLE user (user text primary key,no_imei integer,password text)");
    db.execSQL("CREATE TABLE log_sync_tanda (no_order text primary key,status text ,waktu_sync datetime)");
    db.execSQL("CREATE TABLE log_sync_image (image text primary key,kdstore text,status text ,waktu_sync datetime)");
}
 public void insertBeli123(String no_order,String item,String cust_name
        ,String cust_telp,String quantity,String pengirim,String penerima,
        String gambar,String waktu){

    ContentValues cv = new ContentValues();
    cv.put("no_order", no_order);
    cv.put("item", item);
    cv.put("cust_name", cust_name);
    cv.put("cust_telp", cust_telp);
    cv.put("quantity", quantity);
    cv.put("pengirim", pengirim);
    cv.put("penerima", penerima);
    cv.put("gambar", gambar);
    cv.put("waktu_ambil", waktu);

    getWritableDatabase().insert("tanda_terima", "name", cv);
}
    public Cursor getNoOrder(){
    return(getReadableDatabase().rawQuery("SELECT no_order FROM tanda_terima", null));
}

2 个答案:

答案 0 :(得分:0)

检查源代码。它可以帮助您检查重复的数据。

String sql = "select * from "
                + TABLE_NAME + " WHERE placeId = '" + placeId + "'"; 
        Cursor cursorinfo = null; 
        try {
            database = databaseHelper.getReadableDatabase();
            cursorinfo = database.rawQuery(sql, null);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        if (!cursorinfo.moveToFirst()) {
                        database = databaseHelper.getWritableDatabase();
            try {
                database.insert(TABLE_NAME, "", values/// Here ContentValues);
            } finally {
                database.close();
                cursorinfo.close();
            }
        }

我认为它对你有帮助。  感谢

答案 1 :(得分:0)

首先,您需要从游标

中的数据库中获取整个数据
 void insertInDatabase()
 {
    Cursor cursor=fetchDataFromDatabase();
    if(!cursor.getCount==0)
    {
         while(cursor.moveToNext())
          {
               if(!(cursor.getString(1).equals("Harneet"))
               {
                     dbAdapter.insert("Harneet");
                     return;
               }
          }
          System.println("Record ALready exists in Database");
    }
    else
    {
              System.println("Empty Database");
              dbAdapter.insert("Harneet");
    }
  }

    /*Assume that we have two fields in database, one is Primary key and other is Name field*/

在DbHelper中写下这个:

在数据库中插入值

     void insert(String name)
     { 
           ContentValues cv=new ContentValues();
           cv.put("NAME",name);
           getWritableDatabase.insert(TABLE_NAME,null,cv);
     }

获取光标值

     public Cursor fetchDataFromDatabase()
     {
          String col[]={_id,name};
         getReadableDatabase.query(TABLE_NAME,col,null,null,null);
     }