SQLite database operations

Source: Internet
Author: User

1. Save the database to the memory of the mobile phone.

1.1 The operation method is relatively simple. We only need to write a class that inherits the sqliteopenhelper class to complete related operations.

Public class sqlitehelper extends sqliteopenhelper {private final static string datablbase_name = "message. DB "; private final static int datablbase_version = 1; // version private final static string table_name =" pro_msg "; // table name public static string id =" _ id "; // automatically increment public static string pro_name = "name"; // The Name Of The running program public sqlitehelper (context) {super (context, datablbase_name, null, datablbase_version ); // todo auto-generated constructor stub}/*** 1. if the database file does not exist, sqliteopenhelper will automatically call this method to create a database (such as creating tables and views ..) * 2. if the database file exists, this method is not executed. In the whole process, this method is only executed once * 3. in this case, you can add some initial data to the database * @ Param context */@ overridepublic void oncreate (sqlitedatabase dB) {// create table message (ID integer primariy key autoincrem) string SQL = "create table" + table_name + "(" + ID + "integer primary key autoincrement," + pro_name + "text);" mongodb.exe csql (SQL );} /*** 1. If the data file exists and the current version number is higher than the version number created or upgraded last time, this method will be called * 2. during the execution of this method, if you need to back up the current data, you need to perform more rigorous operations * 3. create a new database after deleting the original database **/@ overridepublic void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// todo auto-generated method stub }}
1.2 database-related operations: Use sqlitehelper. getwritabledatabase or getreadabledatabase () to obtain the sqlitedatabase object for related operations
Getreadabledatabase () is not used to open the database in read-only mode. Instead, getwritabledatabase () is executed first. It is called only when a failure occurs.
Both the getwritabledatabase () and getreadabledatabase () methods can obtain a sqlitedatabase instance used to operate the database.
However, the getwritabledatabase () method opens the database in read/write mode. Once the disk space of the database is full, the database can only read but cannot write. If getwritabledatabase () is used to open the database, an error occurs. The getreadabledatabase () method first opens the database in read/write mode. If the disk space of the database is full, it will fail to be opened. When the opening fails, it will continue to attempt to open the database in read-only mode. 2. How to package data on the SD card and APK together? Release 2.1: Put the database on the SD card
Public class dbhelper {// define the path of the database file private string dbfile = android. OS. environment. getexternalstoragedirectory () + "/test. DB "; private sqlitedatabase database; Private Static dbhelper instance; public static dbhelper getinstance () {If (instance = NULL) {instance = new dbhelper ();} return instance ;} // create a data table private dbhelper () {file = new file (dbfile); If (file. exists () file. delete (); string createtable = "create table person (ID integer primary key autoincrement, name varchar (50)"; database = getdatbase(registry.database.exe csql (createtable );} /*** get the database operation object * @ return */private sqlitedatabase getdatbase () {return sqlitedatabase. openorcreatedatabase (dbfile, null);}/*** data insertion test */public long insertdata (string data) {contentvalues CV = new contentvalues (); cv. put ("name", data); // database = getdatbase (); long rs = database. insert ("person", null, CV); Return Rs;}/*** query data * @ return */Public String selectdata () {string rs = NULL; string SQL = "Select name from person"; // database = getdatbase (); cursor = database. rawquery (SQL, null); If (cursor. movetolast () {rs = cursor. getstring (0);} Return Rs ;}
2.2 manually create an SQLite database (SQLite expert personal can be used) after the database is packaged and released with the APK. This software is similar to the commonly used database software 2.2.1 and put the manually created database in the res \ raw folder, then it is released along with the APK. But how to access our database? 2.2.2 the APK file is equivalent to the EXE file in windows. How can the file size change when the EXE file is started? Therefore, when you run the program for the first time, copy the database files to the memory card or SD card. You can use the operrawresource method to obtain the inputstream object of the Chinese source files in the res \ raw directory. Then perform other operations. Of course, our database files can also be downloaded from the network instead of packaged in the APK, saving the copy operation, however, this will cause inconvenient publishing and excessive network traffic consumption. Use sqlitedatabase. openorcreatedatabase (dbfile,
Null); obtain sqlitedatabase for related operations
Related Article

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.