我在一個應用程序上工作並且創建了一個數據庫,但是當我輸入數據時,logcat發出一個錯誤,提示「列密碼不存在」。爲什麼是這樣?爲什麼它說列不存在爲android sqlite數據庫?
這裏是我的數據庫代碼(一部分):
public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_NUMBER = 1;
private static final String DATABASE_NAME = "Snipeshot";
private static final String TABLE_USERS = "users";
private static final String KEY_USERNAME ="username ";
private static final String KEY_PASSWORD = "password ";
public DBHandler(Context ctx){
super(ctx, DATABASE_NAME, null, DATABASE_NUMBER);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_USER_TABLE = "CREATE TABLE " + DATABASE_NAME + "(" + KEY_USERNAME + "TEXT, " + KEY_PASSWORD
+ "TEXT" + ");";
db.execSQL(CREATE_USER_TABLE);
}
這裏是輸入數據到數據庫的代碼:私有類寄存器擴展
AsyncTask<Object,Void,Boolean>{
String f,l,d,em,un,pw,cpw;
boolean success = true;
protected void onPreExecute(){
f = fn.getText().toString();
l = ln.getText().toString();
d = dob.getText().toString();
em = e.getText().toString();
un = u.getText().toString();
pw = p.getText().toString();
cpw = cp.getText().toString();
}
@Override
protected Boolean doInBackground(Object... arg0) {
// TODO Auto-generated method stub
if(f == null || f.equalsIgnoreCase("") ){
success = false;
return false;
}
else if(l == null || l.equalsIgnoreCase("")){
success = false;
return false;
}
else if(d == null || d.equalsIgnoreCase(""))
{
success = false;
return false;
}
else if(em == null || em.equalsIgnoreCase("")){
success = false;
return false;
}
else if(un == null || un.equalsIgnoreCase("")){
success = false;
return false;
}
else if(pw == null || pw.equalsIgnoreCase("")){
success = false;
return false;
}
else if(!(cpw.equals(pw))){
success = false;
return false;
}
if(success){
db.addUSer(new User(un,pw));
}
return true;
}
protected void onPostExecute(Boolean results){
results = true;
ctx.startActivity(new Intent(ctx,SignIn.class));
}
這裏是addUSer code:
public void addUSer(User user) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_USERNAME, user.getuserName()); //
values.put(KEY_PASSWORD, user.getPassword()); //
// Inserting Row
db.insert(TABLE_USERS, null, values);
db.close(); // Closing database connection
}
也許是因爲你的密碼欄被命名爲「密碼」,而不是「密碼」? – panini
同意@panini,更好的檢查'db.addUSer'。 –
重要的是列名後面有空格嗎? KEY_USERNAME =「用戶名」; – cbrulak