gpt4 book ai didi

java - 为什么我的数据库Adapter类更新方法返回0?

转载 作者:行者123 更新时间:2023-12-01 15:56:09 33 4
gpt4 key购买 nike

我目前正在尝试在 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();
}
}

感谢您的帮助

最佳答案

你的where子句是错误的。它应该使用问号 (?),而不是实际值。实际值应作为下一个参数传递。请参阅这个答案:update sql database with ContentValues and the update-method

关于java - 为什么我的数据库Adapter类更新方法返回0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5011095/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com