美文网首页
[木木方文安卓学习笔记九]SQLite的简单使用

[木木方文安卓学习笔记九]SQLite的简单使用

作者: 丿沧海一粟丿 | 来源:发表于2017-08-25 17:00 被阅读0次
    //保存数据
    private  void saveData(UserModel model) {
    //        打开或者创建mo.db数据库
            SQLiteDatabase db = openOrCreateDatabase("mo.db", Context.MODE_PRIVATE, null);
            //创建user表
    //        db.execSQL("DROP TABLE user");//删除表
            String sql = "create table if not exists user (_id integer PRIMARY KEY AUTOINCREMENT NOT NULL,token varchar,full_name varchar,photo varchar,account varchar,pass_word varchar,user_id varchar,employer_id int,mobile varchar,employer_type int)";
    //        sql = "create table if not exists user (_id integer PRIMARY KEY AUTOINCREMENT NOT NULL,name varchar,age int)";
            db.execSQL(sql);
            ContentValues cv = new ContentValues();
            cv.put("token", model.getToken().toString());
            cv.put("full_name", model.getFullName().toString());
            cv.put("photo", model.getPhoto().toString());
            cv.put("account", model.getMobile());
            cv.put("pass_word", model.getPassword().toString());
            cv.put("user_id", model.getUserId().toString());
            cv.put("employer_id", model.getEmployerId().toString());
            cv.put("mobile", model.getMobile().toString());
            cv.put("employer_type", model.getEmployerType().toString());
            db.insert("user", null, cv);
            db.close();
        }
    //读取数据
    private void getUserData() {
        SQLiteDatabase db = openOrCreateDatabase("mo.db", Context.MODE_PRIVATE, null);
        String sql = "SELECT * FROM user";
        // 查询user表中所有的数据
        Cursor cursor = db.query("user", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            //获得表中值
            String name = cursor.getString(cursor.getColumnIndex("full_name"));
            Log.i("007", name);
        }
        db.close();
    }
    

    //正式需要自己加上各种判断。

    相关文章

      网友评论

          本文标题:[木木方文安卓学习笔记九]SQLite的简单使用

          本文链接:https://www.haomeiwen.com/subject/gxxpdxtx.html