I'm having a problem with creating a sqlite database for my android app. I'm using SQLiteOpenHelper
class, I have successfully created the database and i am sure of that through logCat
but I think I'm failing to create a table and fill it with dummy data just to simulate a login procedure.I Just want to create the database and create a table inside of it and insert some data in the table, i want to do that in the onCreate
method of another activity. I will show you what i have tried.
Here is the SQLiteOpenHelper
class:-
public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "qpc.db";
public static final String TABLE_USERS = "users";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_USERNAME = "username";
public static final String COLUMN_PASSWORD = "password";
public DBHandler dbHandler;
public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_USERS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_USERNAME + "Text ,"
+ COLUMN_PASSWORD + "Text );";
db.execSQL(query);
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_USERNAME,"test");
contentValues.put(COLUMN_PASSWORD,"123");
db.insert(TABLE_USERS,null,contentValues);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
onCreate(db);
}
public void open() throws SQLException
{
dbHandler.getWritableDatabase();
}
public void close()
{
dbHandler.close();
}
public boolean login(String username, String password){
SQLiteDatabase db = getReadableDatabase();
String query = "SELECT "+ COLUMN_ID + " FROM " + TABLE_USERS +
" WHERE username= "+ COLUMN_USERNAME +
" AND password= "+ COLUMN_PASSWORD + ";";
Cursor cursor = db.rawQuery(query,new String[]{username,password});
int count = cursor.getCount();
if(cursor != null) {
if (count > 0) {
return true;
}
}
return false;
}
Here is how I call it in the LoginActivity
public class LoginActivity extends ActionBarActivity {
private static Button loginBtn;
public static EditText usernameTxt,passwordTxt;
private static String writtenUsername,writtenPassword;
private Users user;
private DBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//identifying login button
loginBtn = (Button) findViewById(R.id.loginButton);
//identifying username editText
usernameTxt = (EditText) findViewById(R.id.username);
//identifying password editText
passwordTxt = (EditText) findViewById(R.id.password);
dbHandler = new DBHandler(this, null, null, 1);
//check if database exists
File dbtest = getApplicationContext().getDatabasePath("qpc.db");
if(dbtest.exists())
{
Log.d("Database23", "exists");
}
else {
Log.d("Database23", "doesn't exist");
}
}
//handling login button
public void attemptLogin(View view){
//getting what was written in username and password editText
writtenUsername = usernameTxt.getText().toString();
writtenPassword = passwordTxt.getText().toString();
try {
if (!writtenUsername.matches("") && !writtenPassword.matches("")) {
if (dbHandler.login(writtenUsername, writtenPassword) == true) {
Toast.makeText(this, "yes", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "no", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "Please fill all fields", Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
Toast.makeText(this,e.getMessage(), Toast.LENGTH_LONG).show();
}
}
Aucun commentaire:
Enregistrer un commentaire