美文网首页
练习测试

练习测试

作者: 阿拉侬 | 来源:发表于2016-12-24 16:37 被阅读0次

    1

    2

    3

    4

    5
    6

    分割线


    嵌套练习

    2

    3

    4

    5


    斜体

    123
    加粗
    删除线

    小标题

    • 1
    • 2
    • 3
    • 4
    • 5

    数字编码

    1. 1
    2. 2
    3. 3

    表格

    Tables Are Cool
    col 3 is right-aligned $1600
    col 2 is centered $12
    zebra stripes are neat $1

    图片测试

    测试图片 上一曲图标

    代码测试

    public class DataBaseAdapter {
        MyDatabasesHelper db;
    
        public DataBaseAdapter(Context context) {
            db = new MyDatabasesHelper(context);
        }
    
        //增加数据操作
        public void add(Emp e) {
            SQLiteDatabase sq = db.getWritableDatabase();   //获取数据库写权限
            ContentValues values = new ContentValues();
            values.put(MyDatabasesHelper.NAME, e.getName());
            values.put(MyDatabasesHelper.AGE, e.getAge());
            values.put(MyDatabasesHelper.SEX, e.getSex());
            values.put(MyDatabasesHelper.SALARY, e.getSalary());
            sq.insertOrThrow(MyDatabasesHelper.TAB_NAME, null, values);
    //        sq.execSQL("INSERT INTO emp(name,age,sex)VALUES(\'"+e.getName()+"\',"+e.getAge()+",\'"+e.getSex()+"\'"+")");
            Log.i("add", e.toString());
        }
    
        //修改数据操作
        public void update(Emp e) {
            SQLiteDatabase sq = db.getWritableDatabase();   //获取数据库写权限
            ContentValues values = new ContentValues();
            values.put(MyDatabasesHelper.NAME, e.getName());
            values.put(MyDatabasesHelper.AGE, e.getAge());
            values.put(MyDatabasesHelper.SEX, e.getSex());
            values.put(MyDatabasesHelper.SALARY, e.getSalary());
            sq.update(db.TAB_NAME, values, MyDatabasesHelper.ID + "=?", new String[]{String.valueOf(e.getId())});
            db.close();
        }
    
        //删除数据操作
        public void delete(int id) {
            SQLiteDatabase sq = db.getWritableDatabase();   //获取数据库写权限
            sq.delete(MyDatabasesHelper.TAB_NAME, "id=?", new String[]{String.valueOf(id)});
        }
    
        //查询单个数据操作
        public Emp find(int id) {
            Emp emp = null;
            SQLiteDatabase sq = db.getReadableDatabase();   //获取数据库写权限
            String table = MyDatabasesHelper.TAB_NAME;
            String[] columns = new String[]{MyDatabasesHelper.ID,
                    MyDatabasesHelper.NAME,
                    MyDatabasesHelper.SEX,
                    MyDatabasesHelper.AGE,
                    MyDatabasesHelper.SALARY};
            Cursor result = sq.query(table
                    , columns
                    , "id=?", new String[]{String.valueOf(id)}
                    , null, null, null);
            while (result.moveToNext()) {
                emp = new Emp();
                emp.setId(result.getInt(0));
                emp.setName(result.getString(1));
                emp.setAge(result.getInt(2));
                emp.setSalary(result.getDouble(3));
            }
    
            return emp;
        }
    
        //查询所有数据操作
        public List<Emp> findAll() {
            List<Emp> list = new ArrayList<>();
            SQLiteDatabase sq = db.getReadableDatabase();   //获取数据库写权限
            String table = MyDatabasesHelper.TAB_NAME;
            String[] columns = new String[]{MyDatabasesHelper.ID, MyDatabasesHelper.NAME, MyDatabasesHelper.AGE, MyDatabasesHelper.SEX, MyDatabasesHelper.SALARY};
    
            Cursor result = sq.query(table, columns, null, null, null, null, null);
    
            while (result.moveToNext()) {
                Emp emp = new Emp();
                emp.setId(result.getInt(0));
                emp.setName(result.getString(1));
                emp.setAge(result.getInt(2));
                emp.setSex(result.getString(3));
                emp.setSalary(result.getDouble(4));
                Log.i("tag", emp.toString());
                list.add(emp);
            }
    
            return list;
        }
    
        //删除数据库
        public void dropTable() {
            SQLiteDatabase sq = db.getWritableDatabase();
            sq.execSQL("drop table emp");
        }
    
        //创建数据表
        public void createTable() {
            SQLiteDatabase sq = db.getWritableDatabase();
            sq.execSQL(MyDatabasesHelper.CREATE_TABLE);
        }
    }
    

    相关文章

      网友评论

          本文标题:练习测试

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