Android Development Road 09 (about SQLite database 01)
Last Update:2016-03-18
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size: 15px;">1.SQLite <span style="font-family: 宋体;">database:</span></span></p></p><p><p><span style="font-size: 15px;">SQLite <span style="font-family: 宋体;">is an open-source, embedded relational database that implements a self-contained, 0 configuration, transactional</span> <span style="font-family: Calibri;">SQL</span> <span style="font-family: 宋体;">Database Engine. It is characterized by its highly portable,</span></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">Easy to use, compact, efficient and Reliable. Unlike other database management systems,<span style="font-family: Calibri;">SQLite</span> is <span style="font-family: 宋体;">very simple to install and run, in most cases </span> <span style="font-family: Calibri;">-</span> <span style="font-family: 宋体;">just make sure</span></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">SQLite <span style="font-family: 宋体;">binaries exist to start creating, connecting, and using Databases. </span></span></p></p><p><p><span style="font-size: 15px;">For the <span style="font-family: Calibri;">SDK</span> <span style="font-family: 宋体;">, there are already built-</span> in <span style="font-family: Calibri;">sqlite</span> <span style="font-family: 宋体;">programs, So we do not need to download on the</span> <span style="font-family: Calibri;">sqlite</span> <span style="font-family: 宋体;">official Website. The general database uses static data classes</span> .</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">, and our <span style="font-family: Calibri;">SQLite</span> <span style="font-family: 宋体;">database uses a dynamic database, which is automatically judged based on the value of the Deposit. </span> <span style="font-family: Calibri;"></span> <span style="font-family: 宋体;">The biggest feature of SQLite database compared to other databases is that we</span></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">You can save various types of data to any field without worrying about what the type of the field declaration is. In a special case, it is defined as an <span style="font-family: Calibri;">INTEGER PRIMARY</span></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;"><span style="font-family: 宋体;">The field of KEY can only store a</span> <span style="font-family: Calibri;"></span> <span style="font-family: 宋体;">four-bit integer. </span> <span style="font-family: Calibri;">SQLite</span> <span style="font-family: 宋体;">has the following five types:</span></span></p></p><p><p><span style="font-size: 15px;">1.NULL<span style="font-family: 宋体;">: null value;</span></span></p></p><p><p><span style="font-size: 15px;">2.INTEGER: the shape of the<span style="font-family: 宋体;">symbol, depending on the size of the number deposited;</span></span></p></p><p><p><span style="font-size: 15px;">3.REAL<span style="font-family: 宋体;">: floating point number;</span></span></p></p><p><p><span style="font-size: 15px;">4.TEXT: <span style="font-family: 宋体;">string literal;</span></span></p></p><p><p><span style="font-size: 15px;">5.BLOB<span style="font-family: 宋体;">: Binary object;</span></span></p></p><p><p><span style="font-size: 15px;">2. <span style="font-family: 宋体;">Related Operations</span></span></p></p><p><p><span style="font-size: 15px;">① Create database:<span style="font-family: Calibri;">sqlite3 test.db</span><span style="font-family: 宋体;">;</span></span></p></p><p><p><span style="font-size: 15px;">② creating table: CREATE TABLE<span style="font-family: Calibri;">userInfo</span><span style="font-family: 宋体;">(</span><span style="font-family: Calibri;">userId Integer PRIMARY key autoincrement,name varchar</span>)<span style="font-family: 宋体;">; </span></span></p></p><p><p><span style="font-size: 15px;">The SQLite <span style="font-family: 宋体;">database can parse most of the standard</span> <span style="font-family: Calibri;">SQL</span> <span style="font-family: 宋体;">statements:</span></span></p></p><p><p><span style="font-size: 15px;">① Query statement:<span style="font-family: Calibri;">SELECT *</span> <span style="font-family: 宋体;">from table name </span> <span style="font-family: Calibri;">where</span> <span style="font-family: 宋体;">conditional clause </span> <span style="font-family: Calibri;">Group</span> by <span style="font-family: 宋体;">Group clause </span>having <span style="font-family: Calibri;">... Order</span> by <span style="font-family: 宋体;">sort clause</span></span></p></p><p><p><span style="font-size: 15px;">For example:<span style="font-family: Calibri;">SELECT * FROM UserInfo ORDER BY id desc</span><span style="font-family: 宋体;">;</span></span></p></p><p><p><span style="font-size: 15px;">SELECT * FROM UserInfo GROUP by name has Count<span style="font-family: 宋体;">(</span><span style="font-family: Calibri;">*</span><span style="font-family: 宋体;">)</span><span style="font-family: Calibri;">>1</span></span></p></p><p><p><span style="font-size: 15px;">② Insert statement:<span style="font-family: Calibri;">insert</span> into <span style="font-family: 宋体;">table name (field list) </span> <span style="font-family: Calibri;"></span>values<span style="font-family: 宋体;">(value list)</span></span></p></p><p><p> <span style="font-size:15px;"> example: <span style="font-family:calibri;">insert into userinfo </span> <span style="font-family: The song body;"> (</span> <span style="font-family:calibri;">name </span> <span style="font-family: the song body;" , < span> <span style="font-family:calibri;">age </span> <span style="font-family: Song body;" ") < span> <span style="font-family:calibri;">values </span> <span style="font-family: The song body;"> (</span> <span style="font-family:calibri" , ">" </span> <span style="font-family: Song body;" "> Zhang San </span> <span style="font-family:calibri" , ">" </span> <span style="font-family: the song body;" , < span> <span style="font-family:calibri;">20 </span> <span style="font-family: song body;" "< span> </span> </span></span></span></span></p></p><p><p><span style="font-size: 15px;">③ Update statement:<span style="font-family: Calibri;"></span> the update <span style="font-family: 宋体;">table name </span> <span style="font-family: Calibri;">set</span> <span style="font-family: 宋体;">field name</span> <span style="font-family: Calibri;">=</span> <span style="font-family: 宋体;">value </span> <span style="font-family: Calibri;">where</span> <span style="font-family: 宋体;">condition clause</span></span></p></p><p><p><span style="font-size: 15px;">For example:<span style="font-family: Calibri;">Update person Set name = '</span> <span style="font-family: 宋体;">Zhang San</span> <span style="font-family: Calibri;">' where id = ten</span></span></p></p><p><p><span style="font-size: 15px;">④ Delete statement:<span style="font-family: Calibri;"></span> delete from <span style="font-family: 宋体;">table name </span> <span style="font-family: Calibri;">where</span> <span style="font-family: 宋体;">conditional clause</span></span></p></p><p><p><span style="font-size: 15px;">For example:<span style="font-family: Calibri;">Delete from userInfo where id = ten</span></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">Instance Code:</span></p></p><p><p><span style="font-size: 15px;">1. <span style="font-family: 宋体;">Create a</span> <span style="font-family: Calibri;">JavaBean</span></span></p></p><p><p><span style="font-size: 15px;">public class Student {</span></p></p><p><p><span style="font-size: 15px;">private int sid;</span></p></p><p><p><span style="font-size: 15px;">Private String name;</span></p></p><p><p><span style="font-size: 15px;">Private short age;</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">Public Student () {</span></p></p><p><p><span style="font-size: 15px;">Super ();</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">Public Student (int sid, String name, short Age) {</span></p></p><p><p><span style="font-size: 15px;">Super ();</span></p></p><p><p><span style="font-size: 15px;">This.sid = sid;</span></p></p><p><p><span style="font-size: 15px;">THIS.name = name;</span></p></p><p><p><span style="font-size: 15px;">This.age = age;</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">public int GetSID () {</span></p></p><p><p><span style="font-size: 15px;">Return sid;</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">public void Setsid (int Sid) {</span></p></p><p><p><span style="font-size: 15px;">This.sid = sid;</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">Public String getName () {</span></p></p><p><p><span style="font-size: 15px;">Return name;</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">public void SetName (String Name) {</span></p></p><p><p><span style="font-size: 15px;">THIS.name = name;</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">Public short Getage () {</span></p></p><p><p><span style="font-size: 15px;">Return age;</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">public void Setage (short Age) {</span></p></p><p><p><span style="font-size: 15px;">This.age = age;</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">@Override</span></p></p><p><p><span style="font-size: 15px;">Public String toString () {</span></p></p><p><p><span style="font-size: 15px;">return "Student [sid=" + sid + ", name=" + name + ", age=" + age + "]";</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">2. Create a class Dbopenhelper <span style="font-family: Calibri;"></span> <span style="font-family: 宋体;">that is</span> used to create the SQLite database. Java</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">public class Dbopenhelper extends sqliteopenhelper{</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">private static final String dbname= "data.db";</span></p></p><p><p><span style="font-size: 15px;">private static Cursorfactory factory=null;</span></p></p><p><p><span style="font-size: 15px;">private static final int version=1;</span></p></p><p><p><span style="font-size: 15px;">Public Dbopenhelper (context Context) {</span></p></p><p><p><span style="font-size: 15px;">Super (context, DBNAME, factory, VERSION);</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">When the database is first created, the method is executed</span></p></p><p><p><span style="font-size: 15px;">@Override</span></p></p><p><p><span style="font-size: 15px;">public void OnCreate (sqlitedatabase Db) {</span></p></p><p><p><span style="font-size: 15px;">The field names in the SQL statement correspond to the member variables of the Student.java</span></p></p><p><p><span style="font-size: 15px;">String sql= "create table t_student (sid Integer primary key,"</span></p></p><p><p><span style="font-size: 15px;">+ "name varchar (), age integer)";</span></p></p><p><p><span style="font-size: 15px;">Db.execsql (sql);</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">@Override</span></p></p><p><p><span style="font-size: 15px;">public void Onupgrade (sqlitedatabase db, int oldversion, int Newversion) {</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">3. Create a class that operates the database studentdao. Java</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">public class Studentdao {</span></p></p><p><p><span style="font-size: 15px;">Private Dbopenhelper helper;</span></p></p><p><p><span style="font-size: 15px;">Private Sqlitedatabase db;</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">Public Studentdao (context Context) {</span></p></p><p><p><span style="font-size: 15px;">Create an instance of Dbopenhelper</span></p></p><p><p><span style="font-size: 15px;">Helper = new Dbopenhelper (context);</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">public void Add (Student Student) {</span></p></p><p><p><span style="font-size: 15px;">/**</span></p></p><p><p><span style="font-size: 15px;">* Android uses the getwritabledatabase () and Getreadabledatabase () methods to obtain a sqlitedatabase instance that is used to manipulate the Database.</span></p></p><p><p><span style="font-size: 15px;">* (the Getwritabledatabase () method is called in the Getreadabledatabase () METHOD)</span></p></p><p><p><span style="font-size: 15px;">* The 1.getWritableDatabase () method opens the database in Read-write mode, and once the database is full of disk space, the database is read-only and cannot be Written.</span></p></p><p><p><span style="font-size: 15px;">* 2.getReadableDatabase () method is to read and write to open the database first, if the database disk space is full, will open the failure, when playing</span></p></p><p><p><span style="font-size: 15px;">* will continue to attempt to open the database as read-only after a failed Drive. If the problem is resolved successfully, the Read-only database object is closed and a writable database object is Returned.</span></p></p><p><p><span style="font-size: 15px;">*/</span></p></p><p><p><span style="font-size: 15px;">db = Helper.getwritabledatabase ();</span></p></p><p><p><span style="font-size: 15px;">String sql = "insert into t_student (sid,name,age) values (?,?,?)";</span></p></p><p><p><span style="font-size: 15px;">Db.execsql (sql, New object[] {student.getsid (), student.getname (),</span></p></p><p><p><span style="font-size: 15px;">Student.getage ()});</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">public void Update (Student Student) {</span></p></p><p><p><span style="font-size: 15px;">db = Helper.getwritabledatabase ();</span></p></p><p><p><span style="font-size: 15px;">String sql = "update t_student set name =?, age =?" where Sid =? ";</span></p></p><p><p><span style="font-size: 15px;">Db.execsql (sql, New object[] {student.getname (), student.getage (),</span></p></p><p><p><span style="font-size: 15px;">Student.getsid ()});</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">Public Student Query (int sid) {</span></p></p><p><p><span style="font-size: 15px;">db = Helper.getwritabledatabase ();</span></p></p><p><p><span style="font-size: 15px;">String sql = "select sid,name,age from t_student where sid=?";</span></p></p><p><p><span style="font-size: 15px;">The cursor is actually a collection of stored data</span></p></p><p><p><span style="font-size: 15px;">cursor cursor = db.rawquery (sql, new string[] {string.valueof (sid)});</span></p></p><p><p><span style="font-size: 15px;">Moves the cursor to the next line in the result set found by the cursor, which is to point the cursor to the next found result</span></p></p><p><p><span style="font-size: 15px;">If (cursor.movetonext ()) {</span></p></p><p><p><span style="font-size: 15px;">The name of the specified column can be obtained through the Getcolumnindex () method if no return-1 is present</span></p></p><p><p><span style="font-size: 15px;">return new Student (cursor.getint (cursor.getcolumnindex ("sid")),</span></p></p><p><p><span style="font-size: 15px;">Cursor.getstring (cursor.getcolumnindex ("name")),</span></p></p><p><p><span style="font-size: 15px;">Cursor.getshort (cursor.getcolumnindex ("age"));</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">Return null;</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">Variable parameters are used here to satisfy the case that the number of arguments is not fixed.</span></p></p><p><p><span style="font-size: 15px;">public void Delete (Integer ... sids) {</span></p></p><p><p><span style="font-size: 15px;">If (sids.length > 0) {</span></p></p><p><p><span style="font-size: 15px;">StringBuffer thread-safe, multi-threading, used to perform complex operations on strings</span></p></p><p><p><span style="font-size: 15px;">StringBuffer sb = new StringBuffer ();</span></p></p><p><p><span style="font-size: 15px;">for (int i = 0; i < sids.length; i++) {</span></p></p><p><p><span style="font-size: 15px;">Sb.append ('? '). Append (', ');</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">Sb.deletecharat (sb.length ()-1);</span></p></p><p><p><span style="font-size: 15px;">Sqlitedatabase Database = Helper.getwritabledatabase ();</span></p></p><p><p><span style="font-size: 15px;">String sql = "delete from t_student where SID in (" + SB + ")";</span></p></p><p><p><span style="font-size: 15px;">Database.execsql (sql, (object[)) sids);</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">Public list<student> getscrolldata (int start, int Count) {</span></p></p><p><p><span style="font-size: 15px;">list<student> students = new arraylist<student> ();</span></p></p><p><p><span style="font-size: 15px;">db = Helper.getwritabledatabase ();</span></p></p><p><p><span style="font-size: 15px;">String sql = "select * from T_student limit?,?";</span></p></p><p><p><span style="font-size: 15px;">cursor cursor = db.rawquery (sql, new string[] {string.valueof (start),</span></p></p><p><p><span style="font-size: 15px;">String.valueof (count)});</span></p></p><p><p><span style="font-size: 15px;">While (cursor.movetonext ()) {</span></p></p><p><p><span style="font-size: 15px;">Students.add (new Student (</span></p></p><p><p><span style="font-size: 15px;">Cursor.getint (cursor.getcolumnindex ("sid")),</span></p></p><p><p><span style="font-size: 15px;">Cursor.getstring (cursor.getcolumnindex ("name")),</span></p></p><p><p><span style="font-size: 15px;">Cursor.getshort (cursor.getcolumnindex ("age")));</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">Return students;</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">Public long GetCount () {</span></p></p><p><p><span style="font-size: 15px;">Db=helper.getwritabledatabase ();</span></p></p><p><p><span style="font-size: 15px;">String sql= "select count (sid) from t_student";</span></p></p><p><p><span style="font-size: 15px;">Cursor Cursor=db.rawquery (sql,null);</span></p></p><p><p><span style="font-size: 15px;">If (cursor.movetonext ()) {</span></p></p><p><p><span style="font-size: 15px;">return Cursor.getlong (0);</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">Return 0;</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p>Android Development Road 09 (about SQLite database 01)</p></p></span>