您的位置:首页 > 数据库

用谷歌封装好的API进行数据库增删查改(crud)

2016-01-30 14:56 489 查看
谷歌API

优点:

[1]写法简单,不易写错

[2]有返回值,方便开发,有提示

缺点:

[1]不够灵活,多张表时不易查询

//sql语句增加一条记录
db.execSQL("insert into info(name,phone) values(?,?)",new Object[]{"小明","110"});


//API增加一条记录
ContentValues values = new ContentValues();
values.put("name","小明");
values.put("phone", "110");
long insert = db.insert("info", null, values);
if (insert > 0) {
Toast.makeText(getApplicationContext(), "插入成功", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),"插入失败",Toast.LENGTH_LONG).show();
}


//sql语句删除
db.execSQL("delete from info where name=?",new Object[]{"小明"});


//API删除
int delete = db.delete("info", "name=?", new String[]{"小明"});

Toast.makeText(MainActivity.this,"删除了"+delete+"行",Toast.LENGTH_LONG).show();


//sql语句更新
db.execSQL("update info set phone=? where name=?",new Object[]{"120","小明"});


//API更新
ContentValues values = new ContentValues();
values.put("phone", "120");
int updata = db.update("info", values, "name=?", new String[]{"小明"});
Toast.makeText(MainActivity.this, "更新了" + updata + "行", Toast.LENGTH_LONG).show();


//sql语句查询
Cursor cursor = db.rawQuery("select * from info", null);


//API查询
Cursor cursor = db.query("info", null, "name=?", new String[]{"小明"}, null, null, null);






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  对象 谷歌 api 数据库