android对SQLite数据库进行操作(二)使用API

原有文件不变和之上的 

android对SQLite数据库进行操作(一)SQL语句     原有文件:

http://blog.csdn.net/qq_21813987/article/details/50646874


在TestCase中新增通过API进行数据库操作,通过API能够更加方便的进行数据库操作,而且告别了SQL语句的大量书写。在实际的开发中占有很大的优势。

public void insertApi(){

//把要插入的数据全部封装至ContentValues对象

ContentValues values = new ContentValues();

values.put("key",values);//以键值对的形式将数据插入

db.insert("table",null,values);//insert()系统自动封装API long android.database.sqlite.SQLiteDatabase.

                          insert(String table, String nullColumnHack, ContentValues values) 第二个参数通常用null代替

}


public void deleteApi(){//返回值为删除的数据条数

int i = db.delete("person", "name = ? and _id = ?", new String[]{"儿子", "3"});
System.out.println(i);

//格式delete(String table, String whereClause, String[] whereArgs)

}


public void updateApi(){

ContentValues values = new ContentValues();

values.put("key",values);//以键值队的形式进行传入要改变后的数据值

int i = db.update("person", values, "name=? and _id=?"
+ "", new String[]{"12121","2323"});// i为更新的数据条数

//格式 (String table, ContentValues values, String whereClause, String[] whereArgs)

}


public void queryApi(){

Cursor cursor = db.query("person", null, null, null, null, null, null, null);
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
String salary = cursor.getString(cursor.getColumnIndex("salary"));
System.out.println(name + ";" + phone + ";" + salary);
}

//格式:query(String table, String[] columns, String selection,
            String[] selectionArgs, String groupBy, String having,
            String orderBy, String limit)

}


下接:http://blog.csdn.net/qq_21813987/article/details/50646874

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值