java - 使用内容值的更新方法

标签 java android database android-studio null

helper

public boolean mMessagesSent(String ID,int Data) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_ID, ID);
    contentValues.put(KEY_MESSAGES_SENT, Data);
    db.update(TABLE_USER_DATA, contentValues, null, null);
    return true;
}     

Activity

mainData.mTotalMessages("MyData", +1);
        mainData.mTotalMessagesSent("MyData",+1);
        mainData.mMessages(MessageRecieverId,+1);
        mainData.mMessagesSent(MessageRecieverId,+1);

这是更新数据的正确方法吗...我想将数据的 int 值增加 1,所以我输入了+1,但当我检索数据时该值仍然为空

第一个答案后的代码

public boolean mMessagesSent(String ID,int Data) {
    MainData mainData = new MainData(getApplicationContext());
    SQLiteDatabase db = mainData.getWritableDatabase();
    String newId = ID;
    int newData = Data;
    MainData helper = new MainData(this); //Change the name to your Helper Class name
    Cursor data = helper.getData();
    while (data.moveToNext()) {
        newId = data.getString(data.getColumnIndex("Data"));
        newData = data.getInt(data.getColumnIndex("TotalMessagesSent"));
    }
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_DATA, newId);
    contentValues.put(KEY_MESSAGES_SENT, (newData + 1)); //Change the value of newData(which is actually your old value) by incrementing
    db.update(TABLE_USER_DATA, contentValues, null, null);
    return true;
}

正在获取

final MainData myDBHlpr = new MainData(getActivity());

    Cursor csr = myDBHlpr.getAllQuestions(getActivity());
    while (csr.moveToNext()) {

        mTotalMessagesSent.setText(csr.getString(1));
        mTotalMessagesRecieved.setText(csr.getString(csr.getColumnIndex("TotalMessagesRecieved")));
        mTotalMessages.setText(csr.getString(csr.getColumnIndex("TotalMessages")));

    }

        csr.close();

最佳答案

Is this the correct method to update data... I want to increase the int value of data by 1 so i have put +1 but still the value is empty when i retrieve data

如果您打算每次调用函数mMessagesSent()时将数据的现有值增加一次

您可以考虑首先从数据库中获取值(如果存在),然后递增该值。在Helper中创建一个函数如下所示

public Cursor getData(){

    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_NAME;

    return db.rawQuery(query, null);
}

这将返回一个游标到数据库,以读取数据库中的值。 不要将函数mMessagesSent放在Helper类中,而是将其放在Activity中。现在,在您的 mMessagesSent 函数中,调用上述函数getData(),如图所示

public void mMessagesSent() {
SQLiteDatabase db = this.getWritableDatabase();
String newId = "default value whatever you have specified";//These two lines may be removed and put outside. If you already have it, then 
int newData = 0;                                           //not required to declare here at all, just replace with those variable names
HelperClassName helper = new HelperClassName(this); //Change the name to your Helper Class name
Cursor data = helper.getData();
while(data.moveToNext()){
    newId = data.getString(0);
    newData = data.getInt(1);
}
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_ID, ID);
contentValues.put(KEY_MESSAGES_SENT, (newData+1)); //Change the value of newData(which is actually your old value) by incrementing
long returnVariable = db.update(TABLE_USER_DATA, contentValues, null, null);
if(returnVariable == -1){
    //-1 means there was an error updating the values
}
else{
//the return value if successful will be number of rows affected by the update
}
} 

希望你能得到答案。

关于java - 使用内容值的更新方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53354501/

相关文章:

java - Spring Boot super JAR : cannot be resolved to absolute file path because it does not reside in the file system

java - <jsp :useBean> error: Bean cannot be resolved to a type

Android 默认按钮监听器?

sql-server - SQL 服务器 : pulling and updating local data

c# - 尝试使用代码将一行插入数据库,然后在单击按钮后显示该行(使用 CommandText)

c# - 如何通过 SSH 从 C# 连接到 mysql

java - 如何使一个 JPanel 在另一个 JPanel 中扩展到最大宽度

java - 创建 Artifact 叠加

java - 为 Android 应用程序存储货币兑换数据

Android BLE - 如何分块读取大特征值(使用偏移量)?