简体   繁体   English

SQLitedatabase 更新 Function 找不到行 ID?

[英]SQLitedatabase Update Function Not Finding Row ID?

I'm trying to update a user's "CashStack" when that user clicks to use their in game currency.当用户点击使用他们的游戏货币时,我正在尝试更新用户的“CashStack”。 So far I've found that the user's ID should be 6 but when I try to use this hard coded value in my database update function I'm getting a null object reference error.到目前为止,我发现用户的 ID 应该是 6,但是当我尝试在我的数据库更新 function 中使用这个硬编码值时,我得到了null ZA8CFDE6331BD59EB2AC96F8911C4B66 参考错误。 Can anyone help me understand why this value is incorrect?谁能帮我理解为什么这个值不正确? I'm working in Android Studio and using SQLite.我在 Android Studio 工作并使用 SQLite。

Functions being used: public void updateCashStack(int newCashStack){..} (see below), fiveDollar.setOnClickListener(..)正在使用的函数: public void updateCashStack(int newCashStack){..} (见下文)、 fiveDollar.setOnClickListener(..)

How I'm finding ID number:我如何找到身份证号码:

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( );
    }
}

What shows when I run app:当我运行应用程序时显示什么:
https://i.stack.imgur.com/8f0yl.jpg

Database Code:数据库代码:

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 code: 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( );
    }
}

In your table there is no column titled id .在您的表中没有标题为id的列。 It should be ID .它应该是ID Try like below:尝试如下:

whereClause = "ID = ?";

The syntax in SQL is not case sensitive, but in your database define column String COL1 = "ID"; SQL 中的语法不区分大小写,但在您的数据库中定义列String COL1 = "ID"; it don't match with String whereClause = "id =?";它与String whereClause = "id =?";不匹配

You can use String whereClause = COL1 + " =?";您可以使用String whereClause = COL1 + " =?"; or String whereClause = "ID =?";String whereClause = "ID =?";

Hope this helps!希望这可以帮助!

I'm getting a null object reference error.我收到 null object 参考错误。

The most likely cause is that an in-scope instantiated instance of myDB doesn't exist.最可能的原因是 myDB 的范围内实例化实例不存在。

You can check this by placing a breakpoint on the line您可以通过在该行上放置一个断点来检查这一点

db.update(TABLE_NAME, values, whereClause, new String[] {"6"}); 

in DatabaseHelper.java .DatabaseHelper.java中。

Then run in debug mode (eg click green bug)).然后在调试模式下运行(例如单击绿色错误))。 When debug window appears inspect the variables.当调试 window 出现时检查变量。

At a guess db is null.猜测db是 null。 If so you need to ensure that myDB has been instantiated and that the call is within the scope of myDB (eg declare myDB at the class level and instantiate it in the onCreate method of an activity or the equivalent for a fragment).如果是这样,您需要确保myDB已被实例化并且调用在myDB的 scope 内(例如,在 class 级别声明myDB并在活动的onCreate方法或片段的等效方法中实例化它)。

You may wish to refer to Debug your app for more info on Debugging.您可能希望参考调试您的应用程序以获取有关调试的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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