SQLite Log没有这样的表

时间:2014-05-06 23:42:28

标签: java android sqlite

我已经为用户名和密码创建了一个数据库,当我在这台计算机上的其他计算机上工作时它工作得很好我得到了表不存在的错误,我无法理解为什么它不是创建一个新的数据库中。

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

}

1 个答案:

答案 0 :(得分:2)

在执行之前记录create语句,这可能会有所帮助。

一个简单的猜测是

"create table userTable" + DATA_BASE_NAME

结束为

"create table userTableuserdatabase"

所以你实际上是在创建表userTableuserdatabase,而不是userTable