0

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();
    }
}
4
  • 1
    This might help you developer.android.com/training/basics/data-storage/… You have there information to read, write and delete information from a table. Commented Aug 31, 2015 at 9:48
  • @Gheo I already know these basics, I'm just having some issues when i apply them, i successfully created a database, all i want to do is to create a table with dummy data inside of it in the onCreate method Commented Aug 31, 2015 at 9:53
  • What is the issue you are facing? Commented Aug 31, 2015 at 9:55
  • @Keshav1234 the issue is that the table is not created, and of course the insert code is not working also, any solutions? Commented Aug 31, 2015 at 9:57

2 Answers 2

1

The spacing in the create table query is wrong. Try this.

String query = "CREATE TABLE " + TABLE_USERS + "("
        + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + COLUMN_USERNAME + " Text,"
        + COLUMN_PASSWORD + " Text);";

Also write a separate method to insert a record. Don't do it in onCreate.

Cheers

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, in addition to what you wrote i also modified my login() method to String query = "SELECT "+ COLUMN_ID + " FROM " + TABLE_USERS + " WHERE username=? AND password=?";
1

What is your Fault

You add a "EXTRA-SPACE" [CREATE TABLE ] in your data base query

 @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);

At first please read android.database.sqlite and Demo Purpose Android SQLite Database Tutorial

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.