Sqlite事务+listview显示数据库信息多种方式)

本文详细介绍了SQLiteDatabase中的事务管理方法,包括如何开启、结束事务,以及在事务中执行SQL语句,并通过示例代码展示了如何在事务内进行数据库更新操作。此外,文章还介绍了SQLiteDatabase的基本命令及CursorAdapter的使用,提供了数据库操作的实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SqliteDatabase事务列表显示多条数据事务,以及事务的完整性SQLiteDatabase控制事务方法:

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,

                            R.layout.item,

                            from,

                            to);

            lv.setAdapter(adapter);

以上可以实现但是过于麻烦!

我们还可以使用专门为数据库提供的

Cursoradapter

OtherFishService ofs = new OtherFishService(this);

            Cursor c = ofs.getAllCursor();

            String[] from = new String[]{"_id","name","age"};

            int[] to = new int[]{R.id.tv_id,R.id.tv_name,R.id.tv_age};

            SimpleCursorAdapter adapter = new SimpleCursorAdapter(

                            this,

                            R.layout.item,

                            c,

                            from,

                            to);

            lv.setAdapter(adapter);

我们不单单只用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里面的布局

        @Override

        public void bindView(View view, Context context, Cursor cursor) {

                // TODO Auto-generated method stub

        //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, Cursor c) {

                        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();

                }

                @Override

                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;

                }

                @Override

                public long getItemId(int position) {

                        // TODO Auto-generated method stub

                        return position;

                }

                @Override

                public View getView(int position, View convertView, ViewGroup parent) {

                        //1得到视图

//2从视图中拿到要显示对象

//3cursor里面拿值

//4绑定

//5返回

注意:getView方法实际会执行原本次数2倍(例:7条数据与,getView执行7*2次),第一遍执行是为了测试有多少条数据,第2遍执行才是真正的现实操作

                        View view = mInflater.inflate(R.layout.item, null);

                        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);

                        if (c.moveToPosition(position)) {

                                String id = c.getString(0);

                                String name = c.getString(1);

                                String age = c.getString(2);

                                tv_id.setText(id);

                                tv_name.setText(name);

                                tv_age.setText(age);

                                 

                        }

                        if ( position% 2 == 0) {

                                view.setBackgroundColor(Color.RED);

                        }

                        return view;

                }

        }

当我们用完cursor的时候,怎么关闭他?

//应用回收资源  退出程序时调用该方法

        @Override

        protected void onDestroy() {

                // TODO Auto-generated method stub

                super.onDestroy();r

                Cursor c = adapter.getCursor();

                if(c != null && !c.isClosed()){

                        c.close();

                }

        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值