Android之SQLiteOpenHelper

1.SQLiteOpenHelper

  SQliteOpenHelper是一个抽象类,来管理数据库的创建和版本的管理。要使用它必须实现它的nCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase, int, int)方法

  onCreate:当数据库第一次被建立的时候被执行,例如创建表,初始化数据等。

  onUpgrade:当数据库需要被更新的时候执行,例如删除久表,创建新表。

2.实现代码

 /** 信息列表 数据库
     */
    public class XxlbDatabase extends SQLiteOpenHelper {

        public static final String CREATE_INFOLIST = "create table Infolist(_id integer primary key autoincrement," +
            "qymc text,zsbh text,qyfzr text,ssfjmc text," +
            "lastdate text,yxqz text,sqbbh text,qyid text,qylx text," +
            "jyfwwb text,jcsj text,jcdw text,qytype text,zcdz text)";

        public static final String CREATE_YPINFO = "create table Infoyp(_id integer primary key autoincrement," +
              "qymc text,zcdz text,zsbh text,qyfzr text,ssfjmc text," +
             "qyid text,sqbbh text,ssfj text,yxqz text)";

        public static final String CREATE_LXRINFO = "create table Infolxr(_id integer primary key autoincrement," +
                "qymc text,qyfzr text,ssfjmc text,qyid text,sqbbh text,ssfj text)";

        private Context context;

    public XxlbDatabase(Context context) {
        super(context, "InfoListTable.db", null, 2);//数据库名称
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_INFOLIST);
        db.execSQL(CREATE_YPINFO);
        db.execSQL(CREATE_LXRINFO);
        Toast.makeText(context,"建表成功",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists Infolist");
        onCreate(db);
    }
}


3.SQLite的使用

  Android提供了一个名为SQLiteDatabase的类,它封装了一些操作数据库的API。使用它能实现基本的CRUD操作,通过getWritableDatabase()和getReadableDatabase()可以获取数据库实例。


点击查询 ——从数据库中查找数据并显示到LISTVIEW中

public class Cyfwdw extends BaseActivity implements OnItemClickListener {
    private XxlbDatabase dbhelper;//数据库类名
    private SQLiteDatabase db;
    private SimpleAdapter adapter;
    private ArrayList<Map<String, String>> listData;
    String urlDownLoad = "http://106.120.172.113:8080/。。。!downdata.dhtml";//下载地址

    @InjectView(R.id.ft_btn_cx) Button ft_btn_cx;
    @InjectView(R.id.qymc) EditText qymc;
    @InjectView(R.id.zsbh) EditText zsbh;
    @InjectView(R.id.zcdz) EditText zcdz;
    @InjectView(R.id.ft_listview) ListView ft_listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cyfwdw_layout);

        dbhelper = new XxlbDatabase(this);
        db = dbhelper.getWritableDatabase();

        //监听事件
        ft_btn_cx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //查询表中的数据
                Cursor cursor = db.rawQuery("select qymc,zsbh,zcdz,yxqz from Infolist", null);
                //通过适配器把数据绑定到listview                listData = new ArrayList<Map<String, String>>();
                while (cursor.moveToNext()) {
                    HashMap<String, String> map = new HashMap<String, String>();
                        map.put("qymc", cursor.getString(cursor.getColumnIndex("qymc")));
                        map.put("zsbh", cursor.getString(cursor.getColumnIndex("zsbh")));
                        map.put("zcdz", cursor.getString(cursor.getColumnIndex("zcdz")));
                        map.put("yxqz", cursor.getString(cursor.getColumnIndex("yxqz")));
                    listData.add(map);
                }
                cursor.close();
                db.close();

                //适配器
                adapter = new SimpleAdapter(Cyfwdw.this, listData, R.layout.item_layout, new String[]{"qymc", "zsbh", "zcdz"}, new int[]{R.id.qymc, R.id.zsbh, R.id.zcdz});
                ft_listview.setAdapter(adapter);
                ft_listview.setOnItemClickListener(Cyfwdw.this);
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Map<String, String> map = listData.get(position);
        Bundle bundle = new Bundle();
        bundle.putSerializable("map", (Serializable)map);

        Intent intent = new Intent(Cyfwdw.this, Cyqyxx.class);
        intent.putExtra("bundler", bundle);
        startActivity(intent);
        finish();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值