Android的行中的SQLite数据库没有更新

Android的行中的SQLite数据库没有更新

本文介绍了Android的行中的SQLite数据库没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那里我试图在我的数据库更新的事务表中的行的应用程序。这里的表。

  //内幕交易的列名
    公共静态最后弦乐C_ID = BaseColumns._ID; //特供内部系统ID
    公共静态最后弦乐C_TYPE =类型;
    公共静态最后弦乐C_COMPANY_ID =companyid;
    公共静态最后弦乐C_PERSON_ID =人;
    公共静态最后弦乐C_NAME =名;
    公共静态最后弦乐C_TAG_ID =标签识别;
    公共静态最后弦乐C_STATUS =身份;
    公共静态最后弦乐C_TAG_SCAN_TIME =tagscantime;
    公共静态最后弦乐C_TAG_SENTSERVER_TIME =tagsentservertime;
    公共静态最后弦乐C_TRANSACTIONS_LATITUDE =transactionslatitude;
    公共静态最后弦乐C_TRANSACTIONS_LONGITUDE =transactionslongitude;

在我行插入此表中的列C_TAG_SENTSERVER_TIME设置为null。这是因为我没有在这一点上张贴的交易web服务。一旦我得到的响应从服务器返回,我想用发送到服务器的时间来更新手机的DB的行。目前,我试图让最后一排在DB,然后我试图让该行的ID,也就是传递给更新该行的方法。目前的结果

 的String []参数= {c.getString(c.getColumnIndex(LoginValidate.C_ID))};

是returnig空。这里的更新方法的其余部分。为什么上面的调用返回null?

 公共无效updateTransactionWithServerTime(DateTime的sentToServerAt){        SQLiteDatabase分贝= dbhelper.getWritableDatabase();        光标C = NULL;        C = db.query(DBHelper.TABLETRANSACTIONS,NULL,NULL,NULL,NULL,NULL,NULL);        如果(C!= NULL){
            如果(c.moveToLast()){
                的String []参数= {c.getString(c.getColumnIndex(LoginValidate.C_ID))};
                Log.e(TAGC_ID =+参数[0]);
                ContentValues​​值=新ContentValues​​();
                  values​​.put(C_TAG_SENTSERVER_TIME,sentToServerAt.getMillis());                 INT解析度= db.update(DBHelper.TABLETRANSACTIONS,价值观,LoginValidate.C_ID +=?,参数[0]);                  Log.e(TAG,做了受影响的行跨表NUM更新为+ RES);            }         }
        c.close();
        db.close();
    } // updateTransactionWithServerTime结束

 私有类DBHelper扩展SQLiteOpenHelper {        //数据库名称和版本号
        公共静态最后弦乐DB_NAME =carefreemobiledb.db;
        公共静态最终诠释DB_VERSION = 26;        //表名
        公共静态最后弦乐TABLETRANSACTIONS =交易;
        公共静态最后弦乐TABLECARER =照顾者;
        公共静态最后弦乐TABLETRANSACTIONSMAP =transactionsmap;
        公共静态最后弦乐TABLEPHONE =手机;        //公共静态最后弦乐C_ID = BaseColumns._ID; //专用ID
        //在内部
        //系统
        公共DBHelper(){
            超(背景下,DB_NAME,空,DB_VERSION);
        }        @覆盖
        公共无效的onCreate(SQLiteDatabase DB){
            字符串sqlToCreateTransactionsTable =字符串
                    .format(CREATE TABLE%S(%S INT主键,%S TEXT%S TEXT%S TEXT%S TEXT,%s的文字,+
                            %s的文字,%S TEXT%S INT%S TEXT%S文本),
                            TABLETRANSACTIONS,C_ID,C_TYPE,C_COMPANY_ID,C_PERSON_ID,
                            c_name和C_TAG_ID,C_STATUS,C_TAG_SCAN_TIME,C_TAG_SENTSERVER_TIME,
                            C_TRANSACTIONS_LATITUDE,C_TRANSACTIONS_LONGITUDE);            db.execSQL(sqlToCreateTransactionsTable);
            Log.e(TAG的OnCreate+ sqlToCreateTransactionsTable);


解决方案

您必须使用 INTEGER PRIMARY KEY 来得到一个自动增量列; INT 是远远不够的。

I have an app where i am trying to update a row in the Transactions table in my DB. Here's the table.

//table transactions column names
    public static final String C_ID = BaseColumns._ID; // special for id internally in system
    public static final String C_TYPE = "type";
    public static final String C_COMPANY_ID = "companyid";
    public static final String C_PERSON_ID = "person";
    public static final String C_NAME = "name";
    public static final String C_TAG_ID = "tagid";
    public static final String C_STATUS = "status";
    public static final String C_TAG_SCAN_TIME = "tagscantime";
    public static final String C_TAG_SENTSERVER_TIME = "tagsentservertime";
    public static final String C_TRANSACTIONS_LATITUDE = "transactionslatitude";
    public static final String C_TRANSACTIONS_LONGITUDE = "transactionslongitude";

.

When i insert a row into this table the column C_TAG_SENTSERVER_TIME is set to null. This is because i haven't at this point posted the transaction to the webservice. Once i get the response back from the server, i would like to update the row in the phone's DB with 'sent to server time'. At the moment i try to get the last row in the DB, i then try to get that row's id, that is the passed to the method that updates the row. Currently the result of

String[] param = {c.getString(c.getColumnIndex(LoginValidate.C_ID))};

is returnig null. Here's the rest of the update method. Why is the above call returning null?

public void updateTransactionWithServerTime(DateTime sentToServerAt) {

        SQLiteDatabase db = dbhelper.getWritableDatabase();

        Cursor c = null;

        c = db.query(DBHelper.TABLETRANSACTIONS, null, null, null, null, null, null);

        if (c != null ) {
            if  (c.moveToLast()) {
                String[] param = {c.getString(c.getColumnIndex(LoginValidate.C_ID))};
                Log.e(TAG, "C_ID = " + param[0]);
                ContentValues values = new ContentValues();
                  values.put(C_TAG_SENTSERVER_TIME, sentToServerAt.getMillis());

                 int res =  db.update(DBHelper.TABLETRANSACTIONS, values, LoginValidate.C_ID+"=?", param[0]);

                  Log.e(TAG, "done the update on trans table num of rows affected is " + res);

            }

         }
        c.close();
        db.close();
    }//end of updateTransactionWithServerTime

[edit1]

private class DBHelper extends SQLiteOpenHelper {

        // database name and version number
        public static final String DB_NAME = "carefreemobiledb.db";
        public static final int DB_VERSION = 26;

        //table names
        public static final String TABLETRANSACTIONS = "transactions";
        public static final String TABLECARER = "carer";
        public static final String TABLETRANSACTIONSMAP = "transactionsmap";
        public static final String TABLEPHONE = "phone";

        // public static final String C_ID = BaseColumns._ID; // special for id
        // internally in
        // system


        public DBHelper() {
            super(context, DB_NAME, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            String sqlToCreateTransactionsTable = String
                    .format("create table %s ( %s INT primary key, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT," +
                            " %s TEXT, %s TEXT, %s INT, %s TEXT, %s TEXT )",
                            TABLETRANSACTIONS, C_ID, C_TYPE, C_COMPANY_ID, C_PERSON_ID,
                            C_NAME, C_TAG_ID, C_STATUS, C_TAG_SCAN_TIME, C_TAG_SENTSERVER_TIME,
                            C_TRANSACTIONS_LATITUDE, C_TRANSACTIONS_LONGITUDE);

            db.execSQL(sqlToCreateTransactionsTable);
            Log.e(TAG, "oncreate " + sqlToCreateTransactionsTable);
解决方案

You must use INTEGER PRIMARY KEY to get an autoincrementing column; INT is not enough.

这篇关于Android的行中的SQLite数据库没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:34