-1

I have created a database for username and password, it worked fine when I was at my other computer now on this computer I get the error that the table does not exist, I can't understand why it is not creating a new database.

public class usrPwdDB {

    public static final String USER = "userName";
    public static final String PWD = "password";
    public static final String TABLE_NAME = "userTable";
    public static final String DATA_BASE_NAME = "userdatabase";
    public static final String KEY_ROWID = "_id";
    public static final int DB_VERSION = 5;
    private static final String DATABASE_CREATE = "create table userTable" + DATA_BASE_NAME + " ("    
                + USER + " text not null, "  + PWD + " text not null, );"; 

    DBHelper WDBHelper;
    Context mContext;
    SQLiteDatabase db;

    public usrPwdDB(Context mContext) {
        this.mContext = mContext;
        WDBHelper = new DBHelper(mContext);    
    }

    private static class DBHelper extends SQLiteOpenHelper {
        public DBHelper(Context context) {
            super(context,DATA_BASE_NAME, null, DB_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) {
            db.execSQL("DROP TABLE IF EXISTS userTable");
            onCreate(db);
        }
    }

    public usrPwdDB open() {
        db = WDBHelper.getWritableDatabase();
        return this;
    }

    public void close(){
        WDBHelper.close();
    }

    public long insertInfo(String userName, String password){
        ContentValues content = new ContentValues();
        content.put(USER, userName);
        content.put(PWD, password);
        return db.insertOrThrow(TABLE_NAME, null, content);
    }

    public boolean getUserNameAndPassword(String userName, String Password) throws SQLException {
        Cursor mCursor = db.query(true, TABLE_NAME, new String[] {USER, PWD}, USER+"='"+userName+
                         "' AND password='"+Password+"'", null, null, null, null, null);

        if (mCursor.getCount() > 0) {
            return true;
        }
        return false;
    }

    public Cursor returnData(){
        return db.query(TABLE_NAME, new String[] {USER, PWD}, null, null, null, null, null);
    }

}
Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
  • Your DATABASE_CREATE string has problem in it because you are creating table with userTableuserdatabase name and then trying to access it with different name. – madteapot May 07 '14 at 00:07

1 Answers1

2

Log the create statement before you execute it, that might help.

A simple guess would be that

"create table userTable" + DATA_BASE_NAME

ends up as

"create table userTableuserdatabase"

so you're actually creating the table userTableuserdatabase, not userTable

SimonSays
  • 10,867
  • 7
  • 44
  • 59
  • Hi thanks for your advice unfortunately this doesn't work still getting the error, 01-31 14:16:08.976: E/AndroidRuntime(3345): android.database.sqlite.SQLiteException: no such table: userTable (code 1): , while compiling: SELECT DISTINCT userName, password FROM userTable WHERE userName='' AND password='' – user3535757 May 07 '14 at 09:26
  • You should accept this answer by ticking the tick mark besides the answer. – Lucifer May 07 '14 at 10:53
  • Just be sure that your create statement is correct and gets executed. If it helps, you can output all existing tables to see what you've created. Check here to see how http://stackoverflow.com/questions/82875/how-do-i-list-the-tables-in-a-sqlite-database-file – SimonSays May 07 '14 at 17:38