从数据库中获取数据时遇到问题。在LogCat中,抱怨没有这样的列时间实例。这是我添加到数据库中的新列。只有提取有问题,但createNewList没有该列的问题。可能是什么问题呢?我发布了我的整个数据库类。
public class TrackerDBAdapter {
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";
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 = 1;
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 + "," +
" UNIQUE (" + KEY_IDNUM +"));";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
onCreate(db);
}
}
public TrackerDBAdapter(Context ctx) {
this.mCtx = ctx;
}
public TrackerDBAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}
public long createNewList(String idnum, String simnum,
String description, String model, String time) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_IDNUM, idnum);
initialValues.put(KEY_SIMCARD, simnum);
initialValues.put(KEY_DESCRIPTION, description);
initialValues.put(KEY_MODEL, model);
initialValues.put(KEY_TIMEINSTANCE, time);
return mDb.insert(SQLITE_TABLE, null, initialValues);
}
public boolean deleteAllTrackerInTheList() {
int doneDelete = 0;
doneDelete = mDb.delete(SQLITE_TABLE, null , null);
Log.w(TAG, Integer.toString(doneDelete));
return doneDelete > 0;
}
@SuppressLint("NewApi")
public Cursor fetchListBySimcardNum(String inputText) throws SQLException {
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_IDNUM, KEY_SIMCARD, KEY_DESCRIPTION, KEY_MODEL, KEY_TIMEINSTANCE},
null, null, null, null, null);
}
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_IDNUM, KEY_SIMCARD, KEY_DESCRIPTION, KEY_MODEL, KEY_TIMEINSTANCE},
KEY_SIMCARD + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
@SuppressLint("NewApi")
public Cursor fetchListByIDNum(String inputText) throws SQLException {
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_IDNUM, KEY_SIMCARD, KEY_DESCRIPTION, KEY_MODEL, KEY_TIMEINSTANCE},
null, null, null, null, null);
}
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_IDNUM, KEY_SIMCARD, KEY_DESCRIPTION, KEY_MODEL, KEY_TIMEINSTANCE},
KEY_IDNUM + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchAllTrackerInTheList() {
Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_IDNUM, KEY_SIMCARD, KEY_DESCRIPTION, KEY_MODEL, KEY_TIMEINSTANCE},
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public int getLastInsertedID() {
int id = 0;
Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_IDNUM, KEY_SIMCARD, KEY_DESCRIPTION, KEY_MODEL, KEY_TIMEINSTANCE},
null, null, null, null, null);
if (mCursor.moveToLast()) {
id = mCursor.getInt(0);
}
return id;
}
public void insertSomeCountries() {
createNewList("AFG","Afghanistan","Asia","Southern", "Central Asia");
}
}
答案 0 :(得分:0)
数据库未更新。 尝试删除您的应用并重新安装。
或者您可以更改数据库的版本:
private static final int DATABASE_VERSION = 2;