1. Review
The previous article learned about the storage and acquisition of Sharedpreference; as a supplemental note sharedpreference
1) is a lightweight way of storing data
2) Essence is based on XML file Key-value key value pair data
3) typically used to store simple configuration information
4) Default storage path:/data/data/< Package name >/shared_prefs directory
2. Focus
(1) Introduction to SQLite
(2) Data type
(3) Advantages
(4) Sqlitedatabase
(5) Sqliteopenhelper
3. Introduction
Open source embedded database, in the form of a single file
(1) Lightweight, a dynamic library, single file
(2) Independence, no reliance, no installation
(3) Isolation, all in the folder
(4) Cross-platform
(5) Multi-lingual interface
(6) Security, transaction:
Independent transactions are achieved through exclusive and shared locks on the database; multiple threads read, one thread writes;
4. Data storage Type
Null Integer Real Text Blob
NULL, Integer, floating point, String value, binary value, Dynamic Data type (weak reference), automatic conversion of related data types
5. Advantages
Consume less resources, good performance, 0 management costs; No network server available with Sqlite;
6. Implement
The db file created is stored under the data/< package name >/databases/
(1) Sqlitedatabase: Managing database Classes Creating a Delete Execute SQL command
Example: creating a table operation
Sqlitedatabase db=openorcreatedatabase ("yuan.db", Mode_private,null);d b.execsql ("CREATE table if not exists labelnet ( _id Integer PRIMARY key autoincrement,name text not null,pass text not null) ");
Above your sqlitedatabase created DB object can be executed through the Execsql () method can execute all the SQL statements implemented additions and deletions (SQL statement too much, it is disgusting);
(2) contentvalues implementation
Use Contentvalues to implement encapsulation data, and then through Insert (), update (), delete (), query () Four methods to implement additions and deletions, only need to pass the parameters of the line;
Note: Creating a table also requires a SQL Statement!!!!
Example:
Added: Call the above DB object and add 20 new data
Contentvalues values=new contentvalues (); for (int i=0;i<20;i++) {values.put ("name", "original"), Values.put ("Pass", " 123456 ");//return value is Iddb.insert (Dbname,null, values); Values.clear ();}
Modify:
Contentvalues values=new contentvalues () values.put ("Pass", "111111"); Modify Db.update (DBNAME, values, "_ID>?", New string[]{"10"});
Delete:
Delete Db.delete (DBNAME, "pass=?", New string[]{"111111"});
query: Using the cursor implementation,
Cursor cursor=dbs.query (Dbname,null, "_ID>?", New string[]{"0"},null,null, "_id"); String str= "database read data"; if (cursor!=null) {//Gets the stated string [] Curname=cursor.getcolumnnames (); while (Cursor.movetonext ()) {for (String cn:curname) {log.i ("cursor", cursor.getstring (Cursor.getcolumnindex (CN))); Str+=cursor.getstring ( Cursor.getcolumnindex (CN));} LOG.I ("cursor", "..... ..... ..... ..... ..... ..... ..........................."); str+= "\ n";}} Tv_show.settext (str);
7. Sqliteopenhelper7.1 Introduction
To help you create a database, you can easily create a database as long as you inherit the Sqliteopenhelper class. The Sqliteopenhelper class encapsulates the logic used to create and update databases, based on the needs of the development application.
The new class needs to inherit from the Sqliteopenhelper class: Several methods need to be implemented: OnCreate (), Onupgrade (), and constructors
7.2Example
Package Com.example.sqlite;import Android.content.context;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqlitedatabase.cursorfactory;import Android.database.sqlite.sqliteopenhelper;public Class DB extends Sqliteopenhelper {public DB (context context, String name) {Super (context, name, NULL, 1);} /** * When creating a database for the first time call * generally put the library and key table operations here */@Overridepublic void OnCreate (Sqlitedatabase db) {//generally put the library and key table operations here Db.execs QL ("CREATE table if not ' exists yuantest (_id integer primary key autoincrement,name text not null,pass text not null)");} /** * Automatically call */@Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) when database version is renamed, { //when database When the version is sent renamed, it is automatically called}}
7.3 Use
(1) Getreadabledatabase () returns a read-only database
(2) Getwritabledatabase () return to readable writable
Realize:
DB Db=new db (This, "yuan.db"); Sqlitedatabase dbs=db.getwritabledatabase ();
This returns the DB object, the following operation and 61 kind of
8.Cursor Cursors
No matter how you execute the query, it will return a cursor, which is an Android SQLite database cursor,
(1) get result set by using GetCount () method How many records there are;(2) traverse all records by Movetofirst (), MoveToNext (), and Isafterlast () methods;(3) The field name is obtained by Getcolumnnames ();(4) Convert to field number by Getcolumnindex ();(5) The value of the current record of a given field is obtained by getString (), GetInt () and other methods;(6) Re-execute the query through the Requery () method to get the cursor;(7) Releasing cursor resources through the close () method;when used , be sure to release the cursor resource (resource);
9. Summary
Contentvalues Actually, like Map, HashMap, calls the clear () method when it needs to be emptied.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
android-data Storage (SQLite)