更新时间:2023-02-08 17:26:06
您必须使用 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.