Android教程之SQlit数据库操作  

来源:未知 责任编辑:责任编辑 发表时间:2013-12-01 14:14 点击:

Android教程之SQlit数据库操作   

android.database.sqlite.SQLiteOpenHelperpublic       SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)  Create a helper object to create, open, and/or manage a database. The database is not actually created or opened until one of getWritableDatabase() or getReadableDatabase() is called.
直到调用getWritableDatabase()函数或者getReadableDatabase()时才会创建或者打开数据库。(如果数据库没有创建,那么会先创建数据库)
Parameters 
context to use to open or create the database
name of the database file, or null for an in-memory database
factory to use for creating cursor objects, or null for the default
version number of the database (starting at 1); if the database is older, onUpgrade(SQLiteDatabase, int, int) will be used to upgrade the database


A helper class to manage database creation and version management.
SQLiteOpenHelper类是管理数据库生成和数据库版本建立的辅助类。
You create a subclass implementing onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and optionally onOpen(SQLiteDatabase), and this class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary. Transactions are used to make sure the database is always in a sensible state

SQLiteOpenHelper,如果这个数据库存在,那么SQLiteOpenHelper负责管理数据库。
如果数据库不存在,那么SQLiteOpenHelper会建立一个数据库。
如果需要的话,升级数据库。

        private static final String DB_NAME = "CartDB.db";
        private static final int DB_VERSION = 2;
        private static final String TABLE_NAME_1 = "MyOrder";
        private static final String TABLE_NAME_2 = "OrderLine";


private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME_1 + " (" + "order_no"
        + " text not null, " + "type" + " text not null, " + "desc"
        + " text" + ");");
        db.execSQL("CREATE TABLE " + TABLE_NAME_2 + " (" + "order_no"
        + " text not null, " + "item_no" + " text not null, "
        + "QTY" + " text" + ");");
        }

发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • Android 完全退出程序
  • 原创:Android应用开发-Andorid歌词秀,含源码
  • android 屏幕保护
  • Android手机软件汉化教程---第四课 dex文件汉化
  • 众多Android 开源项目推荐,给力工作给力学习
  • Android Audio代码分析4
  • Android得到已安装的应用程序信息!
  • Android开发者指南(29) —— USB Host and Accessory
  • Android成长的幕后推手:工程师鲁宾
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1