使用内容值的更新方法 [英] updating method using content values

查看:74
本文介绍了使用内容值的更新方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

助手

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

活动

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

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

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

在第一个答案后进行编码

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

FETCH

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

推荐答案

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

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

If you meant to increase the existing value of data by once each time you call the function mMessagesSent()

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

You can consider to first take the value from the database if it exists, and increment the value. Create a function in Helper as shown below

public Cursor getData(){

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

    return db.rawQuery(query, null);
}

这将向数据库返回一个游标,以读取数据库中的值. 请勿将mMessagesSent函数放置在Helper类中,而应将其放置在Activity中 .现在,在您的 mMessagesSent 函数中,如图所示调用上述函数 getData()

This will return a Cursor to the Database, to read the values in the database. Do not put the function mMessagesSent in Helper class, put it in the Activity. Now in your mMessagesSent function, call the above function getData() as shown

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
}
} 

希望您能得到答案.

这篇关于使用内容值的更新方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆