vendredi 6 février 2015

Android sqlite error for login activity

When the user logs in, the application seems to crash and then work. An error appears on the LogCat saying:



Caused by: android.database.sqlite.SQLiteException: no such column: cliff (code 1): , while compiling: SELECT DISTINCT _id, name, password FROM Users WHERE name=cliff


Why would it throw up an error but then work? Can anyone see my problem. Below is my code...



public class DBAdapter {
static final String KEY_ROWID = "_id";
static final String KEY_USERNAME = "name";
static final String KEY_PASSWORD = "password";
static final String KEY_CARDNUM = "cardnum";
static final String KEY_CARDNAME = "cardname";
static final String TAG = "DBAdapter";

static final String DATABASE_NAME = "CPDB";
static final String DATABASE_TABLE = "Users";
static final String DATABASE_TABLEC = "Cards";
static final int DATABASE_VERSION = 1;

static final String DATABASE_CREATE =
"create table Users (_id integer primary key autoincrement, " + "name text not null, password text not null, cardnum integer not null, cardname text not null);";


final Context context;

DatabaseHelper DBHelper;
SQLiteDatabase db;

public DBAdapter (Context ctx){
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context){
super (context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db){
try{
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}

@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 Users");
onCreate (db);

}
}

//--opens the DB--
public DBAdapter open() throws SQLException
{
db= DBHelper.getWritableDatabase();
return this;
}

//--close the DB--
public void close()
{
DBHelper.close();
}

//--retrieves chosen user--
public Cursor checkUser(String username) throws SQLException{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_USERNAME, KEY_PASSWORD}, KEY_USERNAME + "=" + username, null, null, null, null,null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}

}


And then for the login java class ............



public class login extends Activity {
Button btn1,btn2;

@Override
public void onCreate (Bundle savedInstanceState) {
/* Called when the activity is first created*/
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btn1 = (Button)findViewById(R.id.bttn_signup);
btn2 = (Button)findViewById(R.id.bttn_signin);
}

public void onClick(View view){

if(view.getId() == R.id.bttn_signup) {
startActivity(new Intent("com.cardpocket.cardpocket.signup"));
}

if(view.getId() == R.id.bttn_signin) {

EditText email = (EditText) findViewById(R.id.tf_email);
EditText pass = (EditText) findViewById(R.id.tf_password);

String username = email.getText().toString();
String password = pass.getText().toString();


DBAdapter db = new DBAdapter(this);

db.open();
Cursor c = db.checkUser(username);
//set someUsers as a list
ArrayList<String> theUser = new ArrayList<>();


if (c.moveToFirst()) {
do {
String aUser = c.getString(c.getColumnIndex("password"));
theUser.add(aUser);


} while (c.moveToNext());
}

if (theUser.contains(password)) {

startActivity(new Intent("com.cardpocket.cardpocket.mainactivity"));

/*
//set the value and pass to the mainactivity
Intent i = new Intent(this, mainactivity.class);
i.putExtra("username", username);
startActivity(i);}*/

} else {
//do something else
Toast.makeText(this, "No contact found", Toast.LENGTH_LONG).show();
}
}
}


Thank you in advance. :)


Aucun commentaire:

Enregistrer un commentaire