使用数据库登录系统

时间:2012-04-14 22:51:52

标签: android

我想使用SQLLIte创建简单的登录系统我创建了数据库但是我在游标中出错了。

我的数据库代码是

public static final String KEY_RAWID = "_id";
    public static final String KEY_RAWNAME = "name";
    public static final String KEY_PASSWORD = "password";
    public static final String KEY_EMAIL = "email";
        private static final String DATABASE_NAME = "users_DB";
    private static final String DATABASE_TABLE = "users";
    private dbusers ourhelper;
    private final Context ourcontext;
    private SQLiteDatabase ourDatabase;

    private static class dbusers extends SQLiteOpenHelper {

        public dbusers(Context context) {
            super(context, DATABASE_TABLE, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub

            db.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" + KEY_RAWID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " +    KEY_RAWNAME
                    + " TEXT NOT NULL, " + KEY_PASSWORD + " TEXT NOT   NULL, "
                    + KEY_EMAIL + " TEXT NOT NULL);");



        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
            onCreate(db);
        }

    }

    public Dbinfo(Context c) {
        ourcontext = c;
    }

    public Dbinfo open() {
        ourhelper = new dbusers(ourcontext);
        ourDatabase = ourhelper.getWritableDatabase();
        return this;
    }

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

我在这个函数中出错了

public Boolean check(String nameS, String passwordS) {
        // TODO Auto-generated method stub
        String[] columns = new String[] { KEY_RAWID, KEY_RAWNAME, KEY_PASSWORD,
                KEY_EMAIL };
        Cursor c = null;
        Boolean check = true;

        c=ourDatabase.rawQuery("SELECT name,password FROM users WHERE name="+nameS,null);



        if (c.getCount() > 0)// && c2.getCount() > 0) {
        {   check = false;
            return check;
        }

        return check;

错误:

04-15 00:29:23.302: E/AndroidRuntime(1300): android.database.sqlite.SQLiteException: no such column: khaled: , while compiling: SELECT name,password FROM users WHERE name=khaled

虽然我已在数据库中添加了数据,但我已检查数据库中的数据是否已成功添加。有什么帮助吗?

3 个答案:

答案 0 :(得分:0)

nameS必须在引号中:

c=ourDatabase.rawQuery("SELECT name,password FROM users WHERE name='"+nameS+"'",null);

答案 1 :(得分:0)

此行需要更改:

c=ourDatabase.rawQuery("SELECT name,password FROM users WHERE name="+nameS,null);

c=ourDatabase.rawQuery("SELECT name,password FROM users WHERE name='"+nameS+"'",null);

答案 2 :(得分:0)

推荐的方法是:

c = ourDatabase.rawQuery("SELECT name, password FROM users WHERE name = ?",
  new String[]{nameS});