使用Google提供的api操作数据库

1 增加:insert

  ContentValues values = new ContentValues();
                values.put("name","王五");//内部封装好了key values key为列的名字
                values.put("number","3333333");
                db.insert("student",null,values);// 返回值类型long  返回值代表新行的id
                Toast.makeText(this, "操作成功", Toast.LENGTH_SHORT).show();
                break;


 封装好的api底层就是组拼 sql语句  方便开发

缺点:


  /**
     * Convenience method for inserting a row into the database.
     *
     * @param table the table to insert the row into
     * @param nullColumnHack optional; may be null.
     *            SQL doesn't allow inserting a completely empty row without
     *            naming at least one column name.  If your provided values is
     *            empty, no column names are known and an empty row can't be inserted.
     *            If not set to null, the nullColumnHack parameter
     *            provides the name of nullable column name to explicitly insert a NULL into
     *            in the case where your values is empty.
     * @param values this map contains the initial column values for the
     *            row. The keys should be the column names and the values the
     *            column values
     * @return the row ID of the newly inserted row, or -1 if an error occurred
     */

2 删除

 case R.id.btn_del:
               // db.execSQL("delete from student where name='李四'");
                db.delete("student","name=?",new String[]{"王五"});// 返回值代表影响的行数
                Toast.makeText(this, "操作成功", Toast.LENGTH_SHORT).show();
                break;


3 修改:
 case R.id.btn_update:
               //  db.execSQL("update student set number='111111111' where name='李四'");
                ContentValues value = new ContentValues();
                value.put("number","7777777");
                db.update("student",value,"name=?",new String[]{"王五"});
                Toast.makeText(this, "操作成功", Toast.LENGTH_SHORT).show();
                break;
指定某字段作为更新条件 在在ContentValue中查;  返回结果代表跟新了行数

4 query (这个google封装的api不好用  一般查询的时候使用 rawquery)
如果多张表使用谷歌封装好的api不容易查询

你可能感兴趣的:(使用Google提供的api操作数据库)