<span id="Label3"></p><p><p><span style="font-family: 宋体; font-size: 14px;">SQLite is an open source embedded database engine written by D.richard Hipp in C language. It supports most of the SQL92 standards and can be run on all major operating Systems.</span></p></p><p><p><span style="font-size: 14px;">The SQLite database created in android is stored under the:/data/data/< package name >/databases/directory.</span></p></p><p><p><span style="font-size: 15px; font-family: 宋体;"><strong>Main Features:</strong></span></p></p><p><p><span style="font-size: 14px; font-family: 宋体;">-lightweight</span></p></p><p><p><span style="font-size: 14px; font-family: 宋体;">-independence, No dependencies, no installation</span></p></p><p><p><span style="font-size: 14px; font-family: 宋体;">-cross-platform, support for many operating systems</span></p></p><p><p><span style="font-size: 14px; font-family: 宋体;">-support for databases up to 2TB in size</span></p></p><p><p><span style="font-size: 14px; font-family: 宋体;">-each database exists as a single file</span></p></p><p><p><span style="font-size: 14px; font-family: 宋体;">-stored in the hard disk as a B-TREE data structure</span></p></p><p><p><span style="font-family: 宋体; font-size: 15px;"><strong>Data type of sqlite:</strong></span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">SQLite supports null, INTEGER, REAL, text, and BLOB data types</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">Represents: null, integer, floating-point, string, binary, respectively.</span></p></p><p><p><span style="font-family: 宋体; font-size: 15px;"><strong>Dynamic Data type (weak reference):</strong></span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">When a value is inserted into the database, SQLite detects its data type, and if the type does not match the associated column, SQLite attempts to convert the value to the type of the column, and if it cannot, the value is stored as its own type.</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">Using SQLite in Android mainly involves two classes:</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;"><strong>sqlitedatabase</strong> and <strong>sqliteopenhelper</strong>, the following are the main analysis of these two classes.</span></p></p><p><p><span style="font-family: 宋体; font-size: 18px; color: #ff0000;">Sqlitedatabase</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">This class provides some ways to manage SQLite databases, such as creating, deleting, executing SQL commands, and performing other common database administration tasks. The database name is unique for each Program.</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;"><strong>Common Methods:</strong></span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">Db.execsql (String sql)//execute Any SQL statement</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">Db.insert (String table,string nullcolumnhack,contentvalues Values)//insert Record</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">Db.delete (String table,string whereclause,string[] whereargs)//delete Record</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">Db.update (String table,contentvalues values,string whereclause,string[] whereargs)//update Record</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">Db.query (String table,string[] columns,string selection,string[] selectionargs,string groupby,string having,String )//query Record</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">Db.rawquery (String sql,string[] selectionargs)//querying records through SQL statements</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">Here is an example of a simple operation of the SQLite database:</span></p></p><pre><span style="font-size: 13px;"><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">appcompatactivity {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">each program has its own database</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Open or create a database with Openorcreatedatabase to return the Sqlitedatabase object</span></span> <span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* openorcreatedatabase (String name,int mode,sqlitedatabase.cursorfactory factory) * name: database name * Mode: Database permissions, mode_private is private for this application, mode_world_readable and mode_world_writeable are globally readable and writable, respectively. * Factory: a factory class that can be used to instantiate a Cusor object</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">Sqlitedatabase DB</span></span>= Openorcreatedatabase ("user.db", mode_private,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a table</span></span>Db.execsql ("create table if not exists USERTB (" + "_id Integer primary key," + "nam E text not null,age integer not null, "+" sex text is not Null) "<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">inserting records</span> into a table</span>Db.execsql ("insert into USERTB (name,age,sex) values (' Zhang San ', 18, ' female ')"<span style="color: #000000;"><span style="color: #000000;">); Db.execsql (</span></span>"insert into USERTB (name,age,sex) values (' John Doe ', 19, ' male ')"<span style="color: #000000;"><span style="color: #000000;">); Db.execsql (</span></span>"insert into USERTB (name,age,sex) values (' Harry ', 20, ' female ')"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">cursor is a query result object, similar to the resultset in JDBC</span></span>Cursor QueryResult = db.rawquery ("select * from usertb",<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(queryresult! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span><span style="color: #000000;"><span style="color: #000000;">(queryresult.movetonext ()) {log.i (</span></span>"info", "id:" + queryresult.getint (queryresult.getcolumnindex ("_id"<span style="color: #000000;"><span style="color: #000000;">)) </span></span>+ "name:" + queryresult.getstring (queryresult.getcolumnindex ("name")<span style="color: #000000;"><span style="color: #000000;">)) </span></span>+ "age:" + queryresult.getint ("queryresult.getcolumnindex"<span style="color: #000000;"><span style="color: #000000;">)) </span></span>+ "gender:" + queryresult.getstring (queryresult.getcolumnindex ("sex")<span style="color: #000000;"><span style="color: #000000;">))); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">close a Cursor object</span></span><span style="color: #000000;"><span style="color: #000000;">Queryresult.close (); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Close the database</span></span><span style="color: #000000;"><span style="color: #000000;">Db.close (); }}</span></span></span></pre><p><p><span style="font-family: 宋体; font-size: 14px;">When the Openorcreatedatabase ("user.db", Mode_private,null) is executed, a database file is created under the/data/data/< package name >/databases/directory. Open DDMS to View. You can also export it, using tools such as navigate to open the data inside the VIEW.</span></p></p><p><p><span></span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">In addition, the above example records the operation using the Execsql () method through the native SQL statement, of course, You can also use the sqlitedatabase commonly used methods described above, such as insert (), delete (), update (), query ( ) and other METHODS. however, It is important to note that in order to insert records for example, when the amount of data is not large, the use of execsql () using SQL statements to insert the record with the Insert () method is similar to the efficiency, but if the volume of data is larger, then the use of the former is significantly higher than the efficiency of the Latter.</span></p></p><p><p><span style="font-size: 18px; font-family: 宋体; color: #ff0000;">Sqliteopenhelper</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">This class is a helper class for sqlitedatabase, which is used primarily to manage database creation and version Updates. Sqlitehelper is an abstract class that is typically used by creating a subclass that inherits from it and overriding the Oncreat () and Onupgrade () methods.</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">-oncreat (sqlitedatabase db)//is Called when the database is first created, and is typically used for building tables and other Operations.</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">-onupgrade (sqlitedatabase db,int oldversion,int newversion)//called When upgrading the database version</span></p></p><p><p><span style="font-family: 宋体; font-size: 15px;">Here is a simple example of using sqliteopenhelper:</span></p></p><p><p><span style="font-family: 宋体; font-size: 14px;">Create a subclass that inherits from Sqliteopenhelper</span></p></p><pre><span style="font-size: 13px;"><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Sqlitehelper<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Sqliteopenhelper {</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Context: Contextual Object * Name: database name</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">sqlitehelper (context context, String Name) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span>(context, name,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>, 1<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">called when creating a database for the first time, typically to build a table or some initialization operation</span></span><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (sqlitedatabase Db) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Build Table</span></span>Db.execsql ("create table if not exists USERTB (" + "_id Integer primary key," + "name text n OT null,age integer not null, "+" sex-text not Null) "<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">automatically called when the database version is upgraded</span></span><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onupgrade (sqlitedatabase db,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>oldversion,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Newversion) { }}</span></span></span></pre><p><p></p></p><p><p><span style="font-size: 14px; font-family: 宋体;">You can then get a Sqlitedatabase object from an instance of Sqlitehelper and then perform a series of operations on the Database.</span></p></p><pre><span style="font-size: 13px;"><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>MainActivity2<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">appcompatactivity {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main2); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a Sqlitehelper object</span></span>Sqlitehelper helper =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Sqlitehelper (MainActivity2.<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>, "stu.db"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get Sqlitedatabase objects using the Getwritabledatabase () or getreadabledatabase () method</span></span>Sqlitedatabase db =<span style="color: #000000;"><span style="color: #000000;">Helper.getwritabledatabase (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Inserting Records</span></span>Db.execsql ("insert into USERTB (name,age,sex) values (' Zhang San ', 18, ' female ')"<span style="color: #000000;"><span style="color: #000000;">); Db.execsql (</span></span>"insert into USERTB (name,age,sex) values (' John Doe ', 19, ' male ')"<span style="color: #000000;"><span style="color: #000000;">); Db.execsql (</span></span>"insert into USERTB (name,age,sex) values (' Harry ', 20, ' female ')"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get Cursor Object</span></span>Cursor QueryResult = db.rawquery ("select * from usertb",<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(queryresult! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Print all records</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span><span style="color: #000000;"><span style="color: #000000;">(queryresult.movetonext ()) {log.i (</span></span>"info", "id:" + queryresult.getint (queryresult.getcolumnindex ("_id"<span style="color: #000000;"><span style="color: #000000;">)) </span></span>+ "name:" + queryresult.getstring (queryresult.getcolumnindex ("name")<span style="color: #000000;"><span style="color: #000000;">)) </span></span>+ "age:" + queryresult.getint ("queryresult.getcolumnindex"<span style="color: #000000;"><span style="color: #000000;">)) </span></span>+ "gender:" + queryresult.getstring (queryresult.getcolumnindex ("sex")<span style="color: #000000;"><span style="color: #000000;">))); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">close a Cursor object</span></span><span style="color: #000000;"><span style="color: #000000;">Queryresult.close (); } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Close the database</span></span><span style="color: #000000;"><span style="color: #000000;">Db.close (); }}</span></span></span></pre><p><p></p></p><style><style><!--.cbt{font-size:15px;} .cbt div{line-height: 25px;} .cbta{margin-bottom:10px;} .cbtb{margin-top:10px;} .cus:link {color: #436EEE} .cus:visited {color: #436EEE} .cus:hover {color: #EE4000} .cus:active {color: #436EEE}--></style></style>Caobotao source: http://www.cnblogs.com/caobotao/p/5118463.html This copyright belongs to the author and the blog Park is shared, welcome reprint, but without the author's consent must retain this paragraph, and in the article page obvious location to give the original link, otherwise, the right to pursue legal liability is retained.<p><p>The use of SQLite for Android data storage</p></p></span>
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