beginTransaction(); 开启事务
setTransactionSuccessful(); 设置事务成功
endTransaction();结束事务
db.beginTransaction(); //开始事务
try {
db.execsql("update student set amount=amount-300 where name=?",new Object[]{"张三"});
db.execsql("update student set amount=amount+300 where name=?",new Object[]{"李四"});
db.setTransactionSuccessful(); //设置事务成功
} catch (Exception e) {
System.out.println("事务执行出现异常,不能设置事务成功!");
}finally{
//无论是否出现异常,都要结束事务。
db.endTransaction();
}
cmd常见指令: sqlite3 xx.db 查看xx数据库.tables 查看所有表
.exit 退出
.databases 查看所有数据库
标签: include标签实现组件复用<!--加载一个布局文件-->
<include layout="@layout/item"/>
属性 android:visibility=有三个选项
<!--
Visible :可见
都是不可见
invisible :还占据空间
gone:不占据空间
-->
列表显示多条数据 simpleAdapter 代码:OtherFishService ofs = new OtherFishService(this);
List<Fish> fishs = ofs.query();
List<Map<String,Object>>data=new ArrayList<Map<String,Object>>();
for(Fish f:fishs){
Map<String,Object> map = new HashMap<String,Object>();
map.put("_id",f._id);
map.put("name",f.name);
map.put("age",f.age);
data.add(map);
}
String[] from = new String[]{"_id","name","age"};
int[] to = new int[]{R.id.tv_id,R.id.tv_name,R.id.tv_age};
SimpleAdapter adapter = new SimpleAdapter(this,
data,Simsun; font-size:14px; line-height:18px; word-wrap:break-word"> R.layout.item,Simsun; font-size:14px; line-height:18px; word-wrap:break-word"> from,Simsun; font-size:14px; line-height:18px; word-wrap:break-word"> to);
lv.setAdapter(adapter);
以上可以实现但是过于麻烦!
我们还可以使用专门为数据库提供的
Cursoradapter
Cursor c = ofs.getAllCursor();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,Simsun; font-size:14px; line-height:18px; word-wrap:break-word"> c,Simsun; font-size:14px; line-height:18px; word-wrap:break-word"> 我们不单单只用android给我们提过的CursorAdapter我们还可以自定义一个CursorAdapter
自定义CursorAdapter public class MyCursorAdapter extends CursorAdapter {//布局加载器(是一个服务,用来加载xml布局文件到java代码中)
private LayoutInflater mInflater;
public MyCursorAdapter(Context context,Cursor c) {
super(context,c);
mInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mInflater = LayoutInflater.from(context);
}
//创建一个item的布局
@Override
public View newView(Context context,Cursor cursor,ViewGroup parent) {
// TODO Auto-generated method stub
//通过布局加载器加载布局
View view = mInflater.inflate(R.layout.item,null);
return view;
//把数据绑定给item里面的布局
public void bindView(View view,Context context,Cursor cursor) {
//1先得到控件
//2得到数据
//3绑定数据给控件
TextView tv_id = (TextView) view.findViewById(R.id.tv_id);
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
TextView tv_age = (TextView) view.findViewById(R.id.tv_age);
String _id = cursor.getString(0);
String name = cursor.getString(1);
String age = cursor.getString(2);
tv_id.setText(_id);
tv_name.setText(name);
tv_age.setText(age);
int position = cursor.getPosition();
if(position%2 ==0){
view.setBackgroundColor(Color.RED);
}else{
view.setBackgroundColor(Color.GREEN);
}
}
为了实现 我们想要的并且系统没提供的效果所以我们要自定义Adapter
不仅可以继承CursorAdapter 实现自定义CursorAdapter ,我们还可以通
过继承BaseAdapter 。
private class MyCursorAdapter extends BaseAdapter {
private Context context;
private Cursor c;
private LayoutInflater mInflater;
public MycursorAdapter1(Context context,Simsun; font-size:14px; line-height:18px; word-wrap:break-word"> super();
this.context = context;
this.c = c;
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public int getCount() {
// TODO Auto-generated method stub
return c.getCount();
public Object getItem(int position) {
Person p = null;
if (c.moveToPosition(position)) {
String id = c.getString(0);
String name = c.getString(1);
String age = c.getString(2);
p = new Person(id,name,age);
}
return p;
public long getItemId(int position) {
return position;
public View getView(int position,View convertView,Simsun; font-size:14px; line-height:18px; word-wrap:break-word"> //1得到视图
//2从视图中拿到要显示对象
//3从cursor里面拿值
//4绑定
//5返回
注意:getView方法实际会执行原本次数2倍(例:7条数据与,getView执行7*2次),第一遍执行是为了测试有多少条数据,第2遍执行才是真正的现实操作
View view = mInflater.inflate(R.layout.item,Simsun; font-size:14px; line-height:18px; word-wrap:break-word"> TextView tv_id = (TextView) view.findViewById(R.id.bt_id);
TextView tv_name = (TextView) view.findViewById(R.id.bt_name);
TextView tv_age = (TextView) view.findViewById(R.id.bt_age);
tv_id.setText(id);
tv_name.setText(name);
tv_age.setText(age);
if ( position% 2 == 0) {
view.setBackgroundColor(Color.RED);
return view;
当我们用完cursor的时候,怎么关闭他?
protected void onDestroy() {
super.onDestroy();r
Cursor c = adapter.getCursor();
if(c != null && !c.isClosed()){
c.close();
}
原文链接:https://www.f2er.com/sqlite/200129.html