android开发——数据库操作

1、制作自己的DatabaseHelper,继承SQLiteOpenHelper

下面的例子中,数据库建一张表:

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String CREATE_USER = "create table User ("
            + "id integer primary key autoincrement, "
            + "emp_name text, "
            + "emp_id text, "
            + "sex text, "
            + "face_url text, "
            + "emp_company text, "
            + "work_typename text, "
            + "pass_period text, "
            + "modify_time text, "
            + "emp_category text, "
            + "has_feature integer)";


    public DatabaseHelper(Context context, String name,
                          SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建数据库
        db.execSQL(CREATE_USER); // 创建用户表
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 升级数据库
        db.execSQL("drop table if exists User"); // 如果发现User表就先删除
        onCreate(db);
    }
}

 2、制作一个DatabaseUtil操作helper

 context的全局获取可以看一下我的这边博客:https://blog.csdn.net/river66/article/details/86570091

public class DbUtil {

    private static SQLiteDatabase database = null;

    public static SQLiteDatabase getDatabase() {
        if (database == null) {
            DatabaseHelper dbHelper = new DatabaseHelper(MyApplication.myContext,
                    "hanvon_db.database", null, 1);
            database = dbHelper.getWritableDatabase();
        }
        return database;
    }
    
    //升级数据库
    public static void updateDB(int version) {
        DatabaseHelper dbHelper = new DatabaseHelper(MyApplication.myContext,
                "hanvon_db.database", null, version);
        database = dbHelper.getWritableDatabase();
    }
    
    //插入数据,关键字:ContentValues、query、Cursor、update、close
    public static void insertUser(User user) {

        SQLiteDatabase db = getDatabase();

        ContentValues values = new ContentValues();
        values.put("has_feature", user.getHasFeature());
        QueryEmployeeInfoObject employee = user.getEmployee();
        values.put("emp_name", employee.getEmp_name());
        values.put("emp_id", employee.getEmp_id());
        values.put("sex", employee.getSex());
        values.put("face_url", employee.getFacephoto());
        values.put("emp_company", employee.getEmp_company());
        values.put("work_typename", employee.getWork_typename());
        values.put("pass_period", employee.getPass_period());
        values.put("modify_time", employee.getModify_time());
        values.put("emp_category", employee.getEmp_category());

        Cursor cursor = db.query("User", null, "emp_id=?", new String[]{employee.getEmp_id()}, null, null, null);
        if (cursor.moveToFirst()) {
            // 存在此用户,更新数据
            db.update("User", values, "emp_id = ?", new String[]{employee.getEmp_id()});
            cursor.close();
            return;
        }
        // 不存在此用户,插入一条新数据
        db.insert("User", null, values);
        cursor.close();
    }

     /**
     * 根据工号获取员工主键 ID
     * 关键字:moveToFirst、moveToNext、getColumnIndex、getInt
     * @param empId
     * @return
     */
    public static int queryUserByEmpID(String empId) {

        int id = -1;
        SQLiteDatabase db = getDatabase();
        Cursor cursor = db.query("User", null, "emp_id = ?", new String[]{empId}, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                id = cursor.getInt(cursor.getColumnIndex("id"));
            } while (cursor.moveToNext());
        }
        cursor.close();
        db = null;
        return id;
    }

    /**
     * 根据工号删除员工信息
     * 关键字:delete
     * @param empId
     */
    public static void deleteUserByEmpId(String empId) {
        if (TextUtils.isEmpty(empId)) {
            return;
        }
        SQLiteDatabase db = getDatabase();
        // 查询用户表中是否存在此身份证号用户
        Cursor cursor = db.query("User", null, "emp_id = ?", new String[]{empId}, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                // 从表中删除用户
                db.delete("User", "emp_id = ?", new String[]{empId});
            } while (cursor.moveToNext());
        }
        cursor.close();
        db = null;
    }
    /**
     * 查询数据库中所有用户数量
     * 关键字:getCount
     */
    public static int queryAllUserNum() {
        SQLiteDatabase db = getDatabase();
        Cursor cursor = db.query("User", new String[]{"id"}, null, null, null, null, null);
        int count = cursor.getCount();
        if (count <= 0) {
            count = 0;
        }
        cursor.close();
        db = null;
        return count;
    }
}

猜你喜欢

转载自blog.csdn.net/river66/article/details/86575136