简体   繁体   中英

updating record in android programming

I am writing a program to display the details of students' record in android. I am using a existing database and getting the data from it. Here I need to update the status as confirmed. I have created a confirmation button which on pressing will update the status as confirmed. Here on pressing the button my code shows no error but the value is not updated in the database. For information I am using sqlitebrowser as my database.

b1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    ContentValues values = new ContentValues();
    //contentValues.put(DatabaseHelper.STATUS,"CONFIRMED");
    StudentDetails = SQLiteDatabase.openDatabase("", null, SQLiteDatabase.OPEN_READWRITE);
    //status.setText("CONFIRMED");
     //mDbHelper.close();
    values.put("Status", "Confrimed");

    StudentDetails.insert("Student","", values);
    Toast.makeText(getApplicationContext(), "Status upadated", Toast.LENGTH_LONG).show();
    StudentDetails.close();
    }
}

I noticed you've supplied "" as the argument to path for openDatabase() . Unless you're calling this on a member that will open the appropriate database, I believe this wouldn't open a valid database, and thus I wouldn't be surprised if a row didn't get written to the table you were trying to modify. Perhaps try supplying a database file name instead of a blank string and see if that works.

Also, if you're not implementing SQLiteOpenHelper in your own class as outlined in Saving Data in SQL Databases , I would suggest trying that instead of accessing the database in another way, if it isn't too much trouble. I've just made an app using this method, and I have no problems.

Nonetheless, I would assign a long to the output value of the insert function, and set a debug point or write its value of the console. If the value is -1, the method didn't assign a row to the table (possibly because of database constraints - perhaps check that you're satisfying them in terms of the values supplied in the Bundle ).

The value returned from insert will be -1 if an error occurred, as outlined in the docs for insert(String table, String nullColumnHack, ContentValues values) ).

您应该使用StudentDetails.update(“ Student”,“”,values);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM