02_sqliteopenhelper Introduction &oncreate Method Introduction

Source: Internet
Author: User
Tags sqlite sqlite database

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) , and onUpgrade(SQLiteDatabase, int, int) optionally onOpen(SQLiteDatabase) , and this class takes care of opening the database if It exists, creating it does not, and upgrading it as necessary. Transactions was used to make sure the database was 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, SQ  Litedatabase.cursorfactory Factory, int version) Added in API level 1Create a helper object to create, open, and/or manage a database. This method is always returns very quickly. The database is not actually created or opened until one of Getwritabledatabase () or getreadabledatabase () is called. Parameterscontext to open or create the database name of the database file, or null for an in-memory database FAC Tory to 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) would be used to upgrade the database; If the database is newer, ondowngrade (sqlitedatabase, int, int) would be used to downgrade the database  

Context: Contexts

Name: The names of the database files, if Name=null will create a database in memory. If you just want to use it for a while and don't want to actually save the data as a file, you can send a null.

If you really want to save your data to a local storage file, then you must give it a name. Memory as long as a power-down/mobile phone is closed, the contents of the memory will be emptied. Or your application exits, and your process is reclaimed by the system and the contents of your memory will be meaningless. The memory that your current program uses will be allocated to other programs to use. So you really want to store the data on disk. Be sure to pass this parameter in and give it the name of the database file.

Cursorfactory: A cursor factory JDBC query will return a result set resultset, and there will be a cursor inside the resultset. After querying it in Android, it returns a cursor, which is a vernier. How to create this cursor requires a cursorfactory, cursor factory. If a null is passed to the system's default cursor factory. Basically, we're all using the cursor factory that the system helped create for us here. So the third argument passes a null.

Version: Database revision number. The version number of the database must start at 1. How to control the database upgrade and downgrade, which involves me to change this version number from 1 to 2. My database is upgraded and the reverse is degraded.

Open Declaration void Com.itheima.sqlitehello.MyOpenHelper.onCreate (Sqlitedatabase db) @OverrideOverrides: OnCreate ( ...) In sqliteopenhelperpublic abstract void OnCreate (Sqlitedatabase db) Added on API level 1Called when the database is creat Ed for the first time. This is where the creation of tables and the initial population of the tables should happen. Parametersdb the  database.  

In Android, your SQL once written incorrectly will bring a series of problems, because it is not very clear that the error reported. So you can put SQL before you try it in MySQL. Once SQL is written incorrectly, it is not easy to find it in the Android code.

The SQLite database general ID is prefaced with _id.

The database file is not created, and natural SQL statements are not executed. When will you create a SQLite database?

02_sqliteopenhelper Introduction &oncreate Method Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.