我正在尝试在 Android 的 SQLite 表中使用 KEY_ROWID 从列中获取值。但我得到了错误的价值。例如,当我尝试从第六行获取值时,我从第二行获取了值。然后对于第五行,我得到了第一行的值。对于第四行,我得到了空值。我的数据库中总共有六行。有什么问题?我的数据库是
public static final String KEY_ROWID = "_id";
public static final String KEY_IDNUM = "_idnum";
public static final String KEY_SIMCARD = "_simcard";
public static final String KEY_DESCRIPTION = "_description";
public static final String KEY_MODEL = "_model";
public static final String KEY_TIMEINSTANCE = "timeinstance";
public static final String KEY_PASSWORD = "password";
public static final String KEY_LOCATIONAUTOUPDATE = "locationupdate";
public static final String KEY_OVERSPEED = "overspeed";
public static final String KEY_MOVEMENT = "movement";
public static final String KEY_GEOFENCE = "geofence";
public static final String KEY_TIMEZONE = "timezone";
public static final String KEY_AUTHORIZE = "authorize";
public static final String KEY_IMAGE = "image";
public static final String KEY_ACTIVATE = "activate";
public static final String KEY_NUMOFUSE = "number_of_use";
private static final String TAG = "TrackerDBAdapter";
private static final String DATABASE_NAME = "DBTrackerList";
private static final String SQLITE_TABLE = "TBTrackerList";
private static final int DATABASE_VERSION = 5;
private final Context mCtx;
private static final String DATABASE_CREATE =
"CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
KEY_ROWID + " integer PRIMARY KEY autoincrement," +
KEY_IDNUM + "," +
KEY_SIMCARD + "," +
KEY_DESCRIPTION + "," +
KEY_MODEL + "," +
KEY_TIMEINSTANCE + "," +
KEY_PASSWORD + "," +
KEY_LOCATIONAUTOUPDATE + "," +
KEY_OVERSPEED + "," +
KEY_MOVEMENT + "," +
KEY_GEOFENCE + "," +
KEY_TIMEZONE + "," +
KEY_AUTHORIZE + "," +
KEY_IMAGE + "," +
KEY_ACTIVATE + "," +
KEY_NUMOFUSE + "," +
" UNIQUE (" + KEY_ROWID +"));";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
我正在尝试使用 rowid(KEY_ROWID) 获取 idnum(KEY_IDNUM),如下所示。
public String fetchIDnumByRowid(long rowid) throws SQLException{
Cursor mCursor = null;
String retstring = null;
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_IDNUM, KEY_SIMCARD, KEY_DESCRIPTION, KEY_MODEL, KEY_TIMEINSTANCE, KEY_PASSWORD,
KEY_LOCATIONAUTOUPDATE, KEY_OVERSPEED, KEY_MOVEMENT, KEY_GEOFENCE, KEY_TIMEZONE, KEY_AUTHORIZE, KEY_IMAGE, KEY_ACTIVATE, KEY_NUMOFUSE},
KEY_ROWID + "=" + rowid, null, null, null, null, null);
if(mCursor != null){
mCursor.moveToFirst();
retstring = mCursor.getString(1);
}
return retstring;
}
EDIT1:当我获取所有行并逐行检查这些 idnum 时,一切都是正确的。只是当我如上所示获取时,我遇到了问题。