我有一个应用程序,我试图在我的数据库中的 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);