2022年Android教程之SQlit数据库操作 .pdf
《2022年Android教程之SQlit数据库操作 .pdf》由会员分享,可在线阅读,更多相关《2022年Android教程之SQlit数据库操作 .pdf(5页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、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 getWri
2、tableDatabase() 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,
3、 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
4、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
5、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
6、_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, + de
7、sc + text + );); db.execSQL(CREATE TABLE + TABLE_NAME_2 + ( + order_no + text not null, + item_no + text not null, 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 5 页 - - - - - - - - - + QTY + text + );); Override public void onUpgrade(SQLiteDatabase db, int old
8、Version, int newVersion) 1.首先得新建数据库的代理类,通过这个类得到具体数据库的代理类。mOpenHelper = new DatabaseHelper(this); 这个 DatabaseHelper 是继承 SQLiteOpenHelper SQLiteDatabase db = mOpenHelper.getWritableDatabase(); 2. 由于操作的是 SQLiteDatabase 数据,故得到一个 SQLiteDatabase 类,这个类里边的方法可以对数据库进行具体的操作。建立数据库:mOpenHelper = new DatabaseHelp
9、er(v.getContext(); SQLiteDatabase db = mOpenHelper.getWritableDatabase(); String sql = create table Student( + stud_no text not null, + stud_name text ); try db.execSQL(sql); setTitle(create table ok!); catch (SQLException e) Log.e(ERROR, e.toString(); setTitle(create table Error!); drop 数据库: mOpenH
10、elper = new DatabaseHelper(v.getContext(); SQLiteDatabase db = mOpenHelper.getWritableDatabase(); String sql = drop table Student; try db.execSQL(sql); setTitle(drop table ok!); catch (SQLException e) Log.e(ERROR, e.toString(); setTitle(drop table Error!); 插入语句:采用 sql 语句进行插入: mOpenHelper = new Datab
11、aseHelper(v.getContext(); SQLiteDatabase db = mOpenHelper.getWritableDatabase(); String sql_1 = insert into Student (stud_no, stud_name) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 5 页 - - - - - - - - - values(S108, Lily Chen); String sql_2 = insert into Stu
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年Android教程之SQlit数据库操作 2022 Android 教程 SQlit 数据库 操作
限制150内