1

Have a Database with various table. I need to update a String value in table1.

I have a string in activity1.And passed the String value to DBhelper from Activity1.Like this.

private void goTokey() {

        check="premium";
        dbHelper=new DBHelper(this);
        dbHelper.sample(check);
}

I got a value in DBHelper and I need to update that value in database in table1.

DBHelper.class

public  void sample( String prms) {
        Log.d("DBHELPER SUCCESS", prms);

        try{
            SQLiteDatabase db1 = this.getWritableDatabase(); 
            db1.execSQL("update table1 SET status = '"+prms+"' WHERE id = 1 ");
        }catch(Exception e){
            System.out.println("GET SAMPLE VALUE"+e);
        }
    }

For reference of DBHelper.class view here

What's wrong with my syntax?

I got exception like this from some where..

02-28 12:09:45.604: I/System.out(4975): GET SAMPLE VALUEandroid.database.sqlite.SQLiteException: table report already exists (code 1): , while compiling: create table report(level TEXT, topic TEXT,  start TEXT, end TEXT, date TEXT)

How to achieve this to update the value into database?

1 Answers1

0

Change from this

public  void sample( String prms) {
        Log.d("DBHELPER SUCCESS", prms);

        try{
            SQLiteDatabase db1 = this.getWritableDatabase(); 
            db1.execSQL("update table1 SET status = '"+prms+"' WHERE id = 1 ");
        }catch(Exception e){
            System.out.println("GET SAMPLE VALUE"+e);
        }
    }

to this

public  void sample( String prms) {
        Log.d("DBHELPER SUCCESS", prms);
        try
        {
         ContentValues content =new ContentValues();
            content.put(STATUS, prms);
            long l= myDataBase.update(APP_TABLE, content,null, null);
            System.out.print("HELLO NEW INSER CODE----"+l);
        } catch(Exception e){
            //System.out.println("NO UPDATE IN APPSTATUS");
        }
    }
Make it Simple
  • 1,832
  • 5
  • 32
  • 57