gpt4 book ai didi

java - SQLite数据库更新功能找不到行ID?

转载 作者:行者123 更新时间:2023-12-01 20:23:36 30 4
gpt4 key购买 nike

当用户点击使用其游戏货币时,我正在尝试更新该用户的“CashStack”。到目前为止,我发现用户的 ID 应该是 6,但是当我尝试在数据库更新函数中使用这个硬编码值时,我收到了 null 对象引用错误。谁能帮我理解为什么这个值不正确?我正在 Android Studio 中工作并使用 SQLite。

正在使用的函数:public void updateCashStack(int newCashStack){..}(见下文)、 FiveDollar.setOnClickListener(..)

如何查找 ID 号:

int lastID = 0;
for (int i = 0; i < 10; i++) {
if(userIDs[i] != 0) {
lastID = userIDs[i];
Toast.makeText(ChoosePokerStyle.this, "User ID = " + lastID, Toast.LENGTH_LONG).show( );
}
}

运行应用程序时显示的内容:
/image/8f0yl.jpg

数据库代码:

public static final String DATABASE_NAME = "rockfinn.db";
private SQLiteDatabase db;
public static final String TABLE_NAME = "user_data";
public static final String COL1 = "ID";
public static final String COL2 = "NICKNAME";
public static final String COL3 = "CASHSTACK";

public DatabaseHelper(Context context) {super(context, DATABASE_NAME, null, 1);}

@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"NICKNAME TEXT, " +
"CASHSTACK INT)";
db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}

public boolean addData(String nickname) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, nickname);
contentValues.put(COL3, 1000);

long result = db.insert(TABLE_NAME, null, contentValues);

if(result == -1) {
return false;
}
else {
return true;
}
}

public void updateCashStack(int newCashStack)
{
try
{
ContentValues values = new ContentValues();
values.put(COL3, newCashStack);
String whereClause = "id = ?";
db.update(TABLE_NAME, values, whereClause, new String[] {"6"});
//db.setTransactionSuccessful();
}
catch (Exception e)
{
Log.e("Error in transaction", "Error", e);
}
finally
{
// db.endTransaction();
}
}

public Cursor getListContents() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return data;
}

}

FiveDollar.setOnClickListener 代码:

fiveDollar.setOnClickListener(new View.OnClickListener( ) {
@Override
public void onClick(View v)
{
int five = 5;
int cashStackAsInt = Integer.parseInt(cashStack);
cashStackAsInt = cashStackAsInt - five;
userCashStackPrompt.setText(Integer.toString(cashStackAsInt));
myDB.updateCashStack(cashStackAsInt);
Toast.makeText(ChoosePokerStyle.this, "You have bet $5", Toast.LENGTH_LONG).show( );
}
}

最佳答案

您的表格中没有标题为 id 的列。它应该是ID。尝试如下:

whereClause = "ID = ?";

关于java - SQLite数据库更新功能找不到行ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58925219/

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