Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;

Java
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
Posted
Updated 19-Aug-22 7:23am
Comments
Richard MacCutchan 21-Jul-16 10:24am    
What result? You need to explain where your code seems to be failing, what results you expect to see, and what results you actually see.

You also need to stop using string concatenation to create your SQL statements. Use proper parameterised queries as described in the documentation.
wajib rehman 21-Jul-16 10:48am    
actually my question is about to check if the exist record of the user is found and show message that record is already exist

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[^]
 
Share this answer
 
public boolean contains(String value) {
try {

sqLiteDatabase = this.getReadableDatabase();
cursor=sqLiteDatabase.rawQuery("SELECT * FROM " + TABLE_Name + " WHERE " + COLUMN_Name + " = ?;", new String[] {host});
if (cursor.moveToFirst()) {
Log.e("Record Already Exists", "Table is:"+TABLE_Name +" ColumnName:"+COLUMN_Name );

return true;
}
else
{
Log.e("New Record ", "Table is:" +TABLE_Name +" ColumnName:"+COLUMN_Name +" Column Value:"+VIDEOID_WV);
// Inserting record
return false;
}
}
catch (SQLException sqlException) {
sqlException.printStackTrace();
Log.e("TAG","sqlEx: "+ sqlException.toString());
}
return false;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900