好的,我有一个问题,当我尝试在主代码中添加用户名时,它写为:
E / SQLiteLog:(1)表用户名没有名为KEY_USERNAME的列 E / SQLiteDatabase:插入KEY_USERNAME = tyuiopoiuyt KEY_PASSWORD = uytrtyuio时出错 android.database.sqlite.SQLiteException:表用户名没有名为KEY_USERNAME(代码1):的列,而在编译时:INSERT INTO Usernames(KEY_USERNAME,KEY_PASSWORD)VALUES(?,?)
我现在将同时添加数据库和Username类,希望能对您有所帮助。
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "UsernameManager";
private static final String TABLE_USERNAMES = "Usernames";
private static final String KEY_ID = "id";
private static final String KEY_USERNAME = "username";
private static final String KEY_PASSWORD = "password";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_USERNAME_TABLE = String.format("CREATE TABLE %s (%s INTEGER PRIMARY KEY, %s TEXT, %s TEXT)", TABLE_USERNAMES, KEY_ID, KEY_USERNAME, KEY_PASSWORD);
db.execSQL(CREATE_USERNAME_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERNAMES);
onCreate(db);
}
void addUsername(Usernames username){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("KEY_PASSWORD", username.getPassword());
cv.put("KEY_USERNAME", username.getUsername());
db.insert(TABLE_USERNAMES, null, cv);
db.close();
}
Usernames getUsername(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_USERNAMES, new String[] {KEY_ID, KEY_USERNAME, KEY_PASSWORD}, KEY_ID + "=?",
new String[]{String.valueOf(id)}, null,null,null,null);
if(cursor != null){
cursor.moveToFirst();
}
Usernames username = new Usernames(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2));
return username;
}
public List<Usernames> getAllUsernames(){
List<Usernames> usernamesList = new ArrayList<>();
String selectQuery = String.format("SELECT * FROM %s", TABLE_USERNAMES);
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do {
Usernames username = new Usernames();
username.setId(Integer.parseInt(cursor.getString(0)));
username.setUsername(cursor.getString(1));
username.setPassword(cursor.getString(2));
usernamesList.add(username);
} while ( cursor.moveToNext());
}
return usernamesList;
}
public int updateUsername(Usernames username){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("KEY_USERNAME", username.getUsername());
cv.put("KEY_PASSWORD", username.getPassword());
return db.update(TABLE_USERNAMES, cv, KEY_ID + "=?",
new String[]{String.valueOf(username.getId())});
}
public void deleteUsername(Usernames username){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_USERNAMES, KEY_ID + "=?",
new String[]{String.valueOf(username.getId())});
db.close();
}
public int getUsernameCount(){
String countQuery = String.format("SELECT * FROM %s", TABLE_USERNAMES);
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery , null );
cursor.close();
return cursor.getCount();
}
}
这是用户名类
public class Usernames {
int id;
String username;
String password;
public Usernames(){
}
public Usernames(String username, String password){
this.username = username;
this.password = password;
}
public Usernames(int id, String username, String password){
this.id = id;
this.username = username;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}