1. 程式人生 > >02_SQliteOpenHelper介紹&oncreate方法介紹

02_SQliteOpenHelper介紹&oncreate方法介紹

.cn public def 數據 tasks 技術 系統 des task

file:///D:/BaiduNetdiskDownload/adt-bundle-windows-x86_64_20140101/adt-bundle-windows-x86_64_20140101/sdk/docs/reference/android/database/sqlite/SQLiteOpenHelper.html

A helper class to manage database creation and version management.

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.

技術分享

技術分享

Open Declaration android.database.sqlite.SQLiteOpenHelper.SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version)



public SQLiteOpenHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version) 
Added in API level 1
Create a helper object to create, open, and/or manage a database. This method always returns very quickly. The database is not actually created or opened until one of getWritableDatabase() or getReadableDatabase() is called.

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; if the database is newer, onDowngrade(SQLiteDatabase, int, int) will be used to downgrade the database  

context:上下文

name:數據庫文件的名字,如果name=null就會在內存當中創建一個數據庫。如果你只是想臨時用一下,並不想把這個數據真正的保存成文件那就可以傳一個null。

如果說你真正的想把你的數據保存到本地的存儲文件的話,那你一定要給它起一個名字。內存只要是一掉電/手機關上了,內存的內容就都會被清空了。或者你的應用程序退出了,你的進程被系統回收掉之後你的內存裏面的內容將毫無意義。你當前程序使用的這塊內存就會分配給其他程序再去使用。所以真正的想把數據存儲到磁盤上一定要把這個參數傳進來,給它指定數據庫文件的名字。

CursorFactory:遊標工廠 JDBC查詢之後會返回一個結果集ResultSet,這個ResultSet裏面就會有一個遊標。在安卓當中查詢之後它返回的就是一個Cursor,就是遊標。怎麽去創建這個遊標,就需要一個CursorFactory,遊標工廠。如果用系統默認的遊標工廠傳一個null就可以了。基本上咱們在這裏都用系統幫咱們創建的遊標工廠。所以第三個參數傳一個null即可。

version:數據庫版本號。數據庫的版本號必須從1開始。怎麽去控制數據庫的升級和降級,這就涉及到我把這個版本號從1變成2.說明我的數據庫升級了,反之則是降級。


Open Declaration void com.itheima.sqlitehello.MyOpenHelper.onCreate(SQLiteDatabase db)


@Override


Overrides: onCreate(...) in SQLiteOpenHelper
public abstract void onCreate (SQLiteDatabase db) 
Added in API level 1
Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.

Parameters
db  The database.  

Open Declaration android.database.sqlite.SQLiteDatabase



SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks. 

技術分享

在安卓當中你的SQL一旦寫錯了會帶來一系列的麻煩,因為它報錯報的並不是很明確。所以可以把SQL之前先在Mysql試一試。SQL一旦寫錯了在安卓代碼當中不太容易發現。

SQLite數據庫 一般id都用_id開頭。

技術分享

技術分享

技術分享

數據庫文件沒有被創建,自然SQL語句也就沒有被執行。究竟什麽時候會創建一個SQLite數據庫呢?

02_SQliteOpenHelper介紹&oncreate方法介紹