如何检查记录已存在于android数据库中 [英] How to check the record is already exist in android database

查看:73
本文介绍了如何检查记录已存在于android数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码我错了



数据库代码:





what i wrong in the following code

Database code:


    public class DatabaseOperation extends SQLiteOpenHelper {


    public static final String DATABASE_NAME = "Student.db";
    public static final String TABLE_NAME = "student_table";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "PASS";
    public static final String COL_4 = "CONTACT";
    public static final String COL_5 = "NIC";
    public static final String COL_6= "CONFIRM";

    public DatabaseOperation(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PASS TEXT,CONFIRM TEXT,NIC INTEGER,CONTACT INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }
    public  boolean insertData(String name,String pass,String confrim,String contact,String nic)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(COL_2,name);
        cv.put(COL_3,pass);
        cv.put(COL_6,confrim);
        cv.put(COL_4,contact);
        cv.put(COL_5,nic);
       long result= db.insert(TABLE_NAME,null,cv);
        if(result==-1)
            return false;
        else
            return true;

    }
    public Cursor getAllData()
    {
        SQLiteDatabase db=this.getWritableDatabase();
        Cursor res=db.rawQuery("select * from "+TABLE_NAME,null);
        return res;
    }
    public boolean updateData(String id,String name,String pass,String confrim,String contact,String nic)
    {
      SQLiteDatabase db=this.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(COL_1,id);
        cv.put(COL_2,name);
        cv.put(COL_3,pass);
        cv.put(COL_6,confrim);
        cv.put(COL_4,contact);
        cv.put(COL_5,nic);
        db.update(TABLE_NAME,cv, "ID= ?",new String[]{id});
        return  true;

    }
    public Integer deletData(String id)
    {
        SQLiteDatabase db=this.getWritableDatabase();
      return  db.delete(TABLE_NAME, "ID = ?",new String[] {id});
    }
    public boolean checkIfRecordExist(String TABLE_NAME,String COL_2,String chek)
    {
        try
        {
            SQLiteDatabase db=this.getReadableDatabase();
            Cursor cursor=db.rawQuery("SELECT "+COL_2+" FROM "+TABLE_NAME+" WHERE "+COL_2+"='"+COL_2+"'",null);
            if (cursor.moveToFirst())
            {
                db.close();
                Log.d("Record  Already Exists", "Table is:"+TABLE_NAME+" ColumnName:"+COL_2);
                return true;//record Exists

            }
            Log.d("New Record  ", "Table is:"+TABLE_NAME+" ColumnName:"+COL_2+" Column Value:"+COL_2);
            db.close();
        }
        catch(Exception errorException)
        {
            Log.d("Exception occured", "Exception occured "+errorException);
           // db.close();
        }
        return false;
    }
}




and this is the activity class;




registr.setOnClickListener(new View.OnClickListener() {
            @Override
              public void onClick(View view) {

                boolean recordExists= myDb.checkIfRecordExist(DatabaseOperation.TABLE_NAME ,DatabaseOperation.COL_2 ,name.getText().toString());
                if(recordExists)
                {
                    Toast.makeText(getBaseContext(), "exist", Toast.LENGTH_SHORT).show();
                }

                boolean isInserted = myDb.insertData(name.getText().toString(), pass.getText().toString(),
                        confirm.getText().toString(), cont.getText().toString(), nic.getText().toString());


                if (isInserted == true)
                    Toast.makeText(getBaseContext(), "INSERTED", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(getBaseContext(), "not inserted", Toast.LENGTH_SHORT).show();



              }
           });





我尝试了什么:



我尝试了更多,但没有得到。我添加了这个类,但没有显示任何事情,但它没有向我显示任何错误,但无法显示结果



What I have tried:

I tried more but did'nt get.I added this class but did'nt show any thing althoug it did'nt show me any error but could'nt show the result

推荐答案

在您的代码上使用您希望阻止添加包含表中已存在的值的行。如果是这种情况,我建议使用唯一约束或密钥 [ ^ ]而不是试图自己检查存在。



查看 SQLite查询语言:CREATE TABLE中的唯一约束定义 [ ^ ]
BAsed on your code you want to prevent adding a row that contains a value which already exists in the table. If that is the case, I would suggest using a Unique constraint or key[^] instead of trying to check the existence yourself.

Have a look at unique constraint definition in SQLite Query Language: CREATE TABLE[^]


这篇关于如何检查记录已存在于android数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆