我目前正在尝试在Android应用中实现SQLite数据库。看起来databseAdapter类中的getEntry()和getAllEntries()以及delete()方法正常工作。我遇到了updateEntry()方法的问题。我很确定它没有正常工作,因为当它应该返回1时它返回0(我只想更新1个条目)。这是我的updateEntry方法。
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;
}
这是另一个调用它的类CreateAccount的onClick方法。
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();
}
}
});
我不确定这里会发生什么。显然,没有更新行。我的updateEntry看起来很像我在教程中看到的那些。我在这一点上很迷茫,但这可能是愚蠢的哈哈。
这是整个databaseAdapter类
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.SQLiteOpenHelper;
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
}
这是调用updateEntry方法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();
}
}
感谢您的帮助
答案 0 :(得分:1)
你的where子句是错误的。它应该使用问号(?),而不是实际值。实际值应作为下一个参数传递。请参阅此答案:update sql database with ContentValues and the update-method
答案 1 :(得分:0)
查看本教程:http://knightswhocode.com/wordpress/?p=14。它为您提供了一组有用的CRUD数据库方法