Why does my databaseAdapter class update method return 0?
I'm currently trying to implement an SQLite database in an android app. It seems like the getEntry() and getAllEntries() and delete() methods in the databseAdapter class work properly. I'm having trouble with an updateEntry() method though. I'm pretty sure it's not working correctly because it's returning 0 when it should be returning 1 (I'm trying to update 1 entry only). Here is my updateEntry method.
public boolean updateEntry(long _rowIndex, Account a){
ContentValues updateValues = new ContentValues();
updateValues.put(KEY_ACCOUNTNUMBER, a.accountNumber);
updateValues.put(KEY_NAME, a.name);
updateValues.put(KEY_AMOUNT, a.amountOwed);
updateValues.put(KEY_AREA, a.areaCode);
updateValues.put(KEY_PHONE, a.phoneNumber);
String where = KEY_ID + "=" + _rowIndex;
int i = db.update(DATABASE_TABLE, updateValues, where, null);
Log.i(TAG, "rows updated returned " + i);
return i>0;
}
this is the onClick method of another class CreateAccount that calls it.
public void onClick(View arg0) {
String name = nameEditText.getText().toString();
int areaCode = Integer.parseInt(areaCodeEditText.getText().toString());
int phoneNumber = Integer.parseInt(phoneNumberEditText.getText().toString());
Account newAccount = new Account(1, name, areaCode, phoneNumber, 0);
if(b == true){
myAccountDbAdapter.updateEntry(rowIndex,newAccount);
finish();
}
else{
myAccountDbAdapter.insertEntry(newAccount);
finish();
}
}
});
I'm not really sure what could be happening here. Obviously, no rows are being updated. My updateEntry looks exaclty like the ones I've seen in tutorials. I'm pretty lost at this point but it's probably something dumb haha.
here is the entire databaseAdapter class
package com.scoracle.database;
import com.scoracle.book.Account;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHe开发者_如何学Clper;
import android.util.Log;
public class AccountDbAdapter {
public static final String DATABASE_NAME = "accountDatabase.db";
public static final String DATABASE_TABLE = "accountTable";
public static final int DATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_ACCOUNTNUMBER="accountNumber";
public static final int ACCOUNTNUMBER_COLUMN=1;
public static final String KEY_NAME="name";
public static final int NAME_COLUMN=2;
public static final String KEY_AREA="areaCode";
public static final int AREA_COLUMN=3;
public static final String KEY_PHONE="phoneNumber";
public static final int PHONE_COLUMN=4;
public static final String KEY_AMOUNT="amountOwed";
public static final int AMOUNT_COLUMN=5;
//SQL Statement to create database
private static final String DATABASE_CREATE = "create table " + DATABASE_TABLE +
" (" + KEY_ID +
" integer primary key autoincrement, " +
KEY_ACCOUNTNUMBER + " INTEGER, " +
KEY_NAME + " text not null, " +
KEY_AREA + " INTEGER, " +
KEY_PHONE + " INTEGER, " +
KEY_AMOUNT + " REAL);";
private SQLiteDatabase db;
private final Context context;
private MyDBHelper dbHelper;
private String TAG = "ADAPTER";
public AccountDbAdapter(Context _context) {
context = _context;
dbHelper = new MyDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public AccountDbAdapter open() throws SQLException{
//dbHelper = new MyDBHelper(context, DATABASE_NAME, null,DATABASE_VERSION);
try{
db = dbHelper.getWritableDatabase();
} catch(SQLException e){
db = dbHelper.getWritableDatabase();
}
return this;
}
public void close(){
db.close();
}
public long insertEntry(Account _account){
ContentValues newAccountValues = new ContentValues();
newAccountValues.put(KEY_ACCOUNTNUMBER, _account.accountNumber);
newAccountValues.put(KEY_AREA, _account.areaCode);
newAccountValues.put(KEY_NAME, _account.name);
newAccountValues.put(KEY_PHONE, _account.phoneNumber);
newAccountValues.put(KEY_AMOUNT, _account.amountOwed);
return db.insert(DATABASE_TABLE, null, newAccountValues);
}
public boolean removeEntry(long _rowIndex){
return db.delete(DATABASE_TABLE,KEY_ID+ "=" + _rowIndex, null) > 0;
}
public Cursor getAllEntries(){
return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_ACCOUNTNUMBER,KEY_NAME,KEY_AREA, KEY_PHONE,KEY_AMOUNT},
null, null, null, null, null);
}
public Account getEntry(int _rowIndex) throws SQLException{
Cursor c = db.query(DATABASE_TABLE, null, null, null, null, null, null);
if(c.moveToPosition(_rowIndex)){
Account a = new Account();
a.accountNumber = c.getInt(ACCOUNTNUMBER_COLUMN);
a.name = c.getString(NAME_COLUMN);
a.areaCode = c.getInt(AREA_COLUMN);
a.phoneNumber = c.getInt(PHONE_COLUMN);
a.amountOwed = c.getFloat(AMOUNT_COLUMN);
return a;
}else {
throw new SQLException("No entry at row " + _rowIndex);
}
}
public boolean updateEntry(long _rowIndex, Account a){
ContentValues updateValues = new ContentValues();
updateValues.put(KEY_ACCOUNTNUMBER, a.accountNumber);
updateValues.put(KEY_NAME, a.name);
updateValues.put(KEY_AMOUNT, a.amountOwed);
updateValues.put(KEY_AREA, a.areaCode);
updateValues.put(KEY_PHONE, a.phoneNumber);
String where = KEY_ID + "=" + _rowIndex;
int i = db.update(DATABASE_TABLE, updateValues, where, null);
Log.i(TAG, "rows updated returned " + i);
return i>0;
}
private static class MyDBHelper extends SQLiteOpenHelper {
private String TAG = "databasehelper";
public MyDBHelper(Context context, String name, CursorFactory factory, int version){
super(context,name,factory,version);
}
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG , "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS notes");
onCreate(db);
}
}//end db helper
}
and here is the entire class that calls the updateEntry method CreateAccount
package com.scoracle;
import com.scoracle.book.Account;
import com.scoracle.database.AccountDbAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class CreateAccount extends Activity{
private EditText nameEditText;
private EditText areaCodeEditText;
private EditText phoneNumberEditText;
private Button myButton;
private AccountDbAdapter myAccountDbAdapter;
private String TAG = "CREATE_ACCOUNT";
private int rowIndex;
private Account a;
private boolean b = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createaccountlayout);
myAccountDbAdapter = new AccountDbAdapter(this);
myAccountDbAdapter.open();
Bundle bundle = this.getIntent().getExtras();
if(bundle != null) {
rowIndex = bundle.getInt("rowIndex");
b = true;
a = myAccountDbAdapter.getEntry(rowIndex);
}
nameEditText = (EditText) findViewById(R.id.createaccountedittext01);
areaCodeEditText = (EditText) findViewById(R.id.createaccountedittext02);
phoneNumberEditText = (EditText) findViewById(R.id.createaccountedittext03);
if(b == true){
nameEditText.setText(a.name);
String s = new Integer(a.areaCode).toString();
areaCodeEditText.setText(s);
s = new Integer(a.phoneNumber).toString();
phoneNumberEditText.setText(s);
} else{}
myButton = (Button) findViewById(R.id.createaccountbutton01);
myButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
String name = nameEditText.getText().toString();
int areaCode = Integer.parseInt(areaCodeEditText.getText().toString());
int phoneNumber = Integer.parseInt(phoneNumberEditText.getText().toString());
Account newAccount = new Account(1, name, areaCode, phoneNumber, 0);
if(b == true){
myAccountDbAdapter.updateEntry(rowIndex,newAccount);
finish();
}
else{
myAccountDbAdapter.insertEntry(newAccount);
finish();
}
}
});
}
public void onPause(){
super.onPause();
myAccountDbAdapter.close();
}
}
Thanks for any help
Your where clause is wrong. It should use a question mark (?), not an actual value. The actual value should be passed as the next parameter. See this answer: update sql database with ContentValues and the update-method
精彩评论