Reputation: 900
I amp working on an app and I created a database but when I input the data, logcat gives an error saying "column passwords does not exist". Why is this?
Here is my code for the database(part of it):
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);
}
and here is the code that inputs data into the database:private class register extends
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));
}
Here is the 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
}
Upvotes: 0
Views: 2004
Reputation: 25267
From the code above, these are the errors you need to rectify:
1--> Your String variables contains trailing space,
private static final String KEY_USERNAME ="username "; private static final String KEY_PASSWORD = "password ";
Correct it to:
private static final String KEY_USERNAME = "username";
private static final String KEY_PASSWORD = "password";
2--> You should have a id
column with which you can identify each record uniquely. Also, you wrote DATABASE_NAME
instead of TABLE_USERS
, and some corrections regarding spacing in the query string.
String CREATE_USER_TABLE = "CREATE TABLE " + DATABASE_NAME + "(" + KEY_USERNAME + "TEXT, " + KEY_PASSWORD + "TEXT" + ");";
Correct it to:
create a class variable as,
private static final String KEY_USERID = "uid";
And,
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USERS + "(" + KEY_USERID + " integer primary key autoincrement, " + KEY_USERNAME + " TEXT, " + KEY_PASSWORD
+ " TEXT" + ");";
db.execSQL(CREATE_USER_TABLE);
}
Dont know if this would be helpful, but incase it does, just let me know.
Upvotes: 1
Reputation: 4001
You do know that your are calling database name
instead of table name
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
//SHouldn't here TABLE NAME COME
String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USERS + "(" + KEY_USERNAME + "TEXT, " + KEY_PASSWORD
+ "TEXT" + ");";
Upvotes: 0
Reputation: 711
There is missing space:
you have to write " TEXT, "+
instead of
"TEXT,"+
Upvotes: 0