0

我有一个应用程序,我试图在我的数据库中的 Transactions 表中更新一行。这是桌子。

//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";

.

当我在此表中插入一行时,列 C_TAG_SENTSERVER_TIME 设置为空。这是因为我此时还没有将交易发布到网络服务。一旦我从服务器得到响应,我想用“发送到服务器时间”来更新手机数据库中的行。目前我尝试获取数据库中的最后一行,然后尝试获取该行的 id,即传递给更新该行的方法。目前的结果

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

是返回空。这是更新方法的其余部分。为什么上面的调用返回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

[编辑1]

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

3 回答 3

0

您必须使用INTEGER PRIMARY KEY来获取自动增量列;INT是不足够的。

于 2012-10-18T15:59:25.510 回答
0
String [] str new String[c.getCount()];
int i = 0;
            while (c.moveToNext()) {
                str[i] = c.getString(c.getColumnIndex(LoginValidate.C_ID));
                i++;
            }
于 2012-10-18T13:38:03.220 回答
0

假设表实际上包含该列,则从光标读取 ID 的代码看起来是正确的。

无论如何,读取表的所有行的所有字段只是为了获取一个值是低效的,并且没有ORDER BY,并不能保证最新记录是最后一条。

我建议使用此查询来读取最后一个 ID:

cursor = db.rawQuery("SELECT max(" + LoginValidate.C_ID + ")" +
                     " FROM " + DBHelper.TABLETRANSACTIONS, null);

但是,您可以将其作为子查询直接插入更新查询:

int res = db.update(DBHelper.TABLETRANSACTIONS, values,
                    LoginValidate.C_ID + " = " +
                     "(SELECT max("+LoginValidate.C_ID+")" +
                     " FROM " + DBHelper.TABLETRANSACTIONS + ")", null);
于 2012-10-18T13:56:05.087 回答