1

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

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));
}
4

2 回答 2

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();
            }
        }

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

于 2012-06-18T03:29:05.533 回答
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);
     }
于 2012-06-18T04:38:11.697 回答