我使用此代码将User对象保存到SQLite中,但是我收到此错误。什么问题和解决方案?
日志
11-12 13:48:18.647: E/AndroidRuntime(2409): Caused by: android.database.sqlite.SQLiteException: no such column: firstname (code 1): ,
while compiling: SELECT id, firstname, lastname, phonenumber, groupname FROM Users WHERE id = ?
代码
MySQLiteHelper sql=new MySQLiteHelper(MainActivity.this, "users");
for(int i=0;i<5;i++)
sql.addUser(new User());
for(int i=1;i<=5;i++)
Log.i(i+"", sql.getUser(i).toString());
/////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///////////////////////////
public class MySQLiteHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 2;
// Database Name
private static String TABLE_NAME = "Users";
public static String DB_NAME;
public static String DB_PATH;
private Context myContext;
SQLiteDatabase db1;
public MySQLiteHelper(Context context,String Databasename) {
super(context, Databasename, null, DATABASE_VERSION);
myContext=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
// SQL statement to create User table
String CREATE_User_TABLE = "CREATE TABLE " + TABLE_NAME + " (id " +
"INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT,lastname TEXT,phonenumber TEXT,groupname TEXT)";
// create Users table
db.execSQL(CREATE_User_TABLE);
}
// Users table name
// Users Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_FIRSTNAME = "firstname";
private static final String KEY_LASTNAME = "lastname";
private static final String KEY_PHONENUMBER = "phonenumber";
private static final String KEY_GROUPNAME = "groupname";
private static final String[] COLUMNS = {KEY_ID,KEY_FIRSTNAME,KEY_LASTNAME,KEY_PHONENUMBER,KEY_GROUPNAME};
public void addUser(User user){
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(KEY_FIRSTNAME, user.getFirstName());
values.put(KEY_LASTNAME, user.getLastName());
values.put(KEY_PHONENUMBER, user.getPhoneNumber());
values.put(KEY_GROUPNAME, user.getGroupName());
// 3. insert
db.insert(TABLE_NAME, // table
null, //nullColumnHack
values); // key/value -> keys = column names/ values = column values
// 4. close
db.close();
}
public User getUser(int id){
// 1. get reference to readable DB
SQLiteDatabase db = null ;
db = this.getReadableDatabase();
// 2. build query
Cursor cursor =
db.query(TABLE_NAME, // a. table
COLUMNS, // b. column names
" id = ?", // c. selections
new String[] { String.valueOf(id) }, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit
// 3. if we got results get the first one
if (cursor != null)
cursor.moveToFirst();
System.gc();
// 4. build User object
User user=new User();
user.setId(Integer.parseInt(cursor.getString(0)));
user.setFirstname(cursor.getString(1));
user.setLastname(cursor.getString(2));
user.setPhoneNumber(cursor.getString(3));
user.setGroupName(cursor.getString(4));
cursor.close();
return user;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older Users table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// create fresh Users table
this.onCreate(db);
}
@Override
public synchronized void close() {
if(db1!=null)db1.close();
//super.close();
}
}
答案 0 :(得分:0)
我怀疑你的问题是你之前没有得到更新的表的迭代。我看到您使用的是版本2.您需要在onUpgrade
函数中提供更新说明,否则请吹掉数据库并重试。如果没有任何重要信息,我建议清除程序存储器,其中包括所有数据库,并查看它是否有效。