我有一个应用程序,我试图更新我的数据库中的事务表中的行。这是表格。
//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列设置为null。这是因为我还没有将交易发布到webservice。一旦我从服务器获得响应,我想用“发送到服务器时间”更新手机数据库中的行。目前我尝试获取数据库中的最后一行,然后我尝试获取该行的id,即传递给更新行的方法。目前是
的结果String[] param = {c.getString(c.getColumnIndex(LoginValidate.C_ID))};
是returnig null。这是更新方法的其余部分。为什么上面的调用返回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);
答案 0 :(得分:0)
String [] str new String[c.getCount()];
int i = 0;
while (c.moveToNext()) {
str[i] = c.getString(c.getColumnIndex(LoginValidate.C_ID));
i++;
}
答案 1 :(得分: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);
答案 2 :(得分:0)
您必须使用INTEGER PRIMARY KEY
来获取自动增量列; INT
还不够。