Android Development--sqlite Database

Source: Internet
Author: User

I. Introduction of SQLite

Google for Andriod's larger data processing provides SQLite, he is in the storage, management, maintenance and other aspects of the excellent, the function is very strong. SQLite has the following features:
1. Lightweight
With SQLite you only need to bring a dynamic library, you can enjoy its full functionality, and the size of the dynamic library want to be small.
2. Independence
The core engine of the SQLite database does not need to rely on third-party software, nor does it require the so-called "install".
3. Isolation
All information in the SQLite database (such as tables, views, triggers, etc.) is contained within a folder for easy administration and maintenance.
4. Cross-platform
SQLite currently supports most of the operating systems, not the computer operating system more in the many mobile phone systems can also be run, such as: Android.
5. Multi-lingual interface
The SQLite database supports multiple language programming interfaces.
6. Security

The SQLite database implements independent transaction processing through exclusive and shared locks at the database level. This means that multiple processes can read data from the same database at the same time, but only one can write data.

Ii. Types of data

The biggest difference between sqlite and other common DBMS is its support for data types. Other common DBMS typically supports strongly typed data, which means that each column type must be pre-specified, but SQLite uses a weakly typed field. In fact, only the following five types of storage are available internally:
?
Null: Represents a null value
Integer: Used to store an integer, which can be stored using the 1,2,3,4,6,8 bit, depending on the size.
Real:ieee floating Point
TEXT: Stored by string
BLOB: stored in binary value, without any changes.
Note that these types are properties of the value itself, not the properties of the column.
However, in order to be compatible with other DBMS (and SQL Standards), the type of the column can be specified in its CREATE TABLE statement, and for this reason, SQLite has a concept of column similarity (column Affinity). Column affinity is a property of a column, and SQLite has the following column similarities:
The Text:text column uses Null,text or blobs to store any data inserted into this column, and if the data is a number, it is converted to text.
The Numeric:numeric column can use any storage type, which first attempts to convert the inserted data into a real or integer type, and if successful it is stored as real and integer, otherwise unchanged.
Integer: Similar to numeric, except that it converts the integer value to Integer, if it is a real type and does not have a decimal part, it is also converted to an integer
Real: And the numeric type is just that it will convert to real and the integer value to be converted to real.
None: Try not to make any changes.
SQLite determines the column similarity of each column based on the CREATE TABLE statement. The rules are as follows (case-insensitive):
1. If an int is included in the data type, it is an integer
2. If the data type includes Char,clob,text, the text
3. If a blob is included in the data type, or if no data type is specified, none
4. If the data type includes Real,floa or Doub, it is the real
5. The rest of the situation is numeric

Create an Android project, add a class

  

Mess class to build entities

 Public classMess {PrivateString Lad;PrivateString name;PrivateString Messeng;PrivateString date;PrivateString Zhang;Private intID;  PublicString Getlad () {returnLad; }     Public voidSetlad (String lad) { This. Lad =Lad; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getmesseng () {returnMesseng; }     Public voidSetmesseng (String messeng) { This. Messeng =Messeng; }     PublicString getDate () {returndate; }     Public voidsetDate (String date) { This. Date =date; }     PublicString Getzhang () {returnZhang; }     Public voidSetzhang (String Zhang) {Zhang=Zhang; }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }

Openhelp class, Inherit Sqliteopenhelper first

 Public classOpenhelpextendsSqliteopenhelper {//Create a database    Private Static FinalString db_name= "Text.db"; //Database Version    Private Static Final intVersion=1;  PublicOpenhelp (Context context) {Super(Context,db_name,NULL, version); }    //Create a table@Override Public voidonCreate (Sqlitedatabase db) {LOG.I ("Hi", "no database, CREATE DATABASE"); String SQL= "CREATE table t_message (ID integer primary Key,tou varchar (), userName varchar (), message varchar (), datetime varch AR (50)) ";    Db.execsql (SQL); } @Override Public voidOnupgrade (Sqlitedatabase arg0,intArg1,intarg2) {        //TODO auto-generated Method Stub            }}

Test class, operation, write method

 Public classTest {Privatesqlitedatabase Help;  PublicTest (Sqlitedatabase context) { This. help=context; }    //Add     Public voidaddmess () {contentvalues con=NewContentvalues ();//Instantiate ContentvaluesCon.put ("Tou", R.drawable.lad); Con.put ("UserName", "teenager"); Con.put ("Message", "How's it going?"); Con.put ("DateTime", "11-29"); Help.insert ("T_message",NULL, con); }    //Delete     Public voiddeletemess (Integer Ind) {String Whereclause= "Id=?";//delete condition string[] Whereargs={"1"};//the deleted parameter Help.delete ("T_message", Whereclause, Whereargs);//execute Delete}//Modify     Public voidupdatemess (mess me) {contentvalues con=NewContentvalues ();//Instantiate ContentvaluesCon.put ("UserName", "Sao Year");//add the fields and content you want to changeString whereclause= "id=?";//Modify ConditionString[] whereargs={"1"};//Modifying the parameters of a conditionHelp.update ("T_message", Con, Whereclause, Whereargs);//Perform modifications            }    //Enquiry     PublicList<mess>find () {List<mess> list=NewArraylist<mess>(); Cursor cur=help.query ("T_message",NULL,NULL,NULL,NULL,NULL,NULL);//querying and obtaining cursors         if(Cur.movetofirst ()) {//determine if the cursor is empty               for(inti = 0; I <cur.getcount (); i++) {cur.movetonext (); Mess SS=NewMess ();//entity classSs.setlad (Cur.getstring (Cur.getcolumnindex ("Tou")))); Ss.setname (cur.getstring (Cur.getcolumnindex ("UserName"))); Ss.setmesseng (cur.getstring (Cur.getcolumnindex ("Message"))); Ss.setdate (cur.getstring (Cur.getcolumnindex ("DateTime"))); Ss.setid (Cur.getint (Cur.getcolumnindex ("id")));            List.add (ss);        }} help.close (); returnlist; }    }

Running in the Mainactivity class

 public  class   Mainactivity extends Activity { protected void   OnCreate (Bundle savedinstancestate) {Supe             R.oncreate (savedinstancestate);             Setcontentview (R.layout.deng); Openhelp help  =new  openhelp (mainactivity.             This   base  =             Help.getwritabledatabase (); Test St  =new  test (base  ); St.addmess ();  //  Run method, add, delete, change  

After executing the above code, the system will generate a "test.db" database file in the/data/data/[package_name]/databases directory.

 

View by: Open cmd, find the installation directory like my E:\adt-bundle-windows-x86_64-20140702\sdk\platform-tools

    

Android Development--sqlite Database

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.