对数据库进行操作的两种方式

时间:2021-09-04 07:56:40

1,通过sql命令来执行操作:

/**
     * 对数据库进行添加操作
     * sql:insert into person(name,age) values('sz',20);\
     *缺陷:
     *    1. 传递参数很麻烦
     *    2. 不能有返回值
     */
    
    public void savePerson(Person p)
    {
        String sql = "insert into person(name,age) values(?,?);";
        
        SQLiteDatabase db = helper.getWritableDatabase();
        
        //db代表的是一个数据库的一个连接
        if(db.isOpen())//判断数据库是否打开
        {
            db.execSQL(sql, new Object[]{p.getName(),p.getAge()}); //向数据库里面去添加一行记录
            
            db.close(); //记住一定要关闭数据库的连接
        }
        
    }
    
    /**
     * 删除一个对象
     * sql:delete from person where _id = 1;
     */
    public void deletePerson(Integer id)
    {
        String sql = "delete from person where _id = ?;";
        
        SQLiteDatabase db = helper.getWritableDatabase();
        
        if(db.isOpen())
        {
            db.execSQL(sql, new Integer[]{id});
            
            db.close();
        }
    }
    
    /**
     * 修改一个对象
     * sql:update person set name ='cccc' where _id=1;
     */
    public void updatePerson(Person p)
    {
        String sql = "update person set name =? where _id=?;";
        
        SQLiteDatabase db = helper.getWritableDatabase();
        
        if(db.isOpen())
        {
            db.execSQL(sql, new Object[]{p.getName(),p.get_id()});
            
            db.close();
        }
    }
    
    /**
     * 查询所有
     */
    public List<Person> queryPerson()
    {
        String sql = "select _id,name,age from person;";
        SQLiteDatabase db = helper.getReadableDatabase();
        List<Person> list = null;
        
        if(db.isOpen())
        {
            Cursor cursor = db.rawQuery(sql, null); //查询
            
            //cursor.getCount() 查看执行sql以后返回的结果集的个数
            if(null != cursor && cursor.getCount() > 0)
            {
                list = new ArrayList<Person>();
                
                while(cursor.moveToNext())
                {
                    Integer id = cursor.getInt(0);
                    String name = cursor.getString(1);
                    Integer age = cursor.getInt(2);
                    
                    list.add(new Person(id,name,age));
                }
                
            }
            
        }
        return list;
    }

2,使用安卓命令来对数据库进行操作执行:

public void savePerson(Person p)
    {
        SQLiteDatabase db = helper.getWritableDatabase();
        if(db.isOpen())
        {
            //nullColumnHack 如果数据库里面的name子都设计的时候不允许为空,但是你传递过来的参数是空
            // 如果不设置这个nullColumnHack参数那么就会报错
            // 如果你设置nullColumnHack这个参数的值为name那么不会报错
            
            ContentValues values = new ContentValues();
            
            values.put("name", p.getName());
            values.put("age", p.getAge());
            
            Long id = db.insert("person", null, values);
            
            Log.d(TAG, "================:" + id);
            db.close();
        }
    }
    
    public void deletePerson(Integer id)
    {
        SQLiteDatabase db = helper.getWritableDatabase();
        if(db.isOpen())
        {
            //select * from person where id=? and name = ?;
            String whereClause = "_id=?";
            String[] whereArgs =new String[]{String.valueOf(id)};
            
            int _id = db.delete("person", whereClause, whereArgs);
            
            Log.d(TAG, "================:" + _id);
            db.close();
        }
    }
    
    public void updatePerson(Person p)
    {
        SQLiteDatabase db = helper.getWritableDatabase();
        if(db.isOpen())
        {
            ContentValues values = new ContentValues();
            values.put("name", "xintx");
            values.put("age", "2");
            
            String whereClause = "_id=?";
            String[] whereArgs = new String[]{String.valueOf(p.get_id())};
            
            db.update("peson", values, whereClause, whereArgs);
            
            db.close();
        }
    }
    
    public void queryItem(Integer id)
    {
        SQLiteDatabase db = helper.getWritableDatabase();
        if(db.isOpen())
        {
            String[] columns = new String[]{"_id","name","age"};
            String selection = "_id=?";
            String[] selectionArgs = new String[]{String.valueOf(id)};
            String groupBy = null; //按什么什么分组
            String having = null; //如果select里面包含了组函数的时候,不能用where去查询 就只有用having
            String orderBy = null; //按什么排序 order by id desc;
            
             Cursor cursor = db.query("person", columns, selection, selectionArgs, groupBy, having, orderBy); 
             
             while(null != cursor && cursor.moveToFirst())
             {
                 Integer _id = cursor.getInt(0);
                 String name = cursor.getString(1);
                 Integer age = cursor.getInt(2);
                 
                 Log.d(TAG, "_id=" + _id + "  name=" + name + "  age = " + age);
                 
                 db.close();
             }
        }
    }