美文网首页
使用Google提供的api操作数据库

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

作者: dev晴天 | 来源:发表于2018-08-11 18:28 被阅读0次
    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 <code>null</code>.
         *            SQL doesn't allow inserting a completely empty row without
         *            naming at least one column name.  If your provided <code>values</code> is
         *            empty, no column names are known and an empty row can't be inserted.
         *            If not set to null, the <code>nullColumnHack</code> parameter
         *            provides the name of nullable column name to explicitly insert a NULL into
         *            in the case where your <code>values</code> 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操作数据库

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