Android data storage-SQLite

Source: Internet
Author: User

SQLite is a lightweight, embedded, and relational database. It is currently used in iPhone, Android, and other mobile phone systems and is widely used by other companies.

Android SQLite, you can see from the android SDK that all database operations are encapsulated,

A common class is contentvalues, which is similar to a hashmap that stores key-value pairs.

We generally use SQLite and write a type to inherit from sqliteopenhelper. Through the getwritabledatabase method of this class, we can get an instance of sqlitedatabase.

Then we can perform basic data operations (add, delete, modify, and query ).


Public class dbhelper extends sqliteopenhelper {</P> <p> Public final static string db_name = "Contact"; <br/> Public final static int version = 1; <br/> Private Static dbhelper instance = NULL; </P> <p> private sqlitedatabase dB; <br/> Public static dbhelper getinstance (context) {<br/> If (instance = NULL) <br/>{< br/> instance = new dbhelper (context ); <br/>}< br/> return instance; <br/>}</P> <p> private void op Endatabase () <br/>{< br/> DB = This. getwritabledatabase (); <br/>}</P> <p> Public dbhelper (context) {<br/> super (context, db_name, null, version ); <br/> // todo auto-generated constructor stub <br/>}</P> <p> @ override <br/> Public void oncreate (sqlitedatabase dB) {<br/> // todo auto-generated method stub </P> <p> stringbuffer tableusercreate = new stringbuffer (); </P> <p> tableusercreate. append ("create table u Ser (ID integer primary key autoincrement, ") <br/>. append ("Name text,") <br/>. append ("phonenumber text)"); </P> <p >}</P> <p> @ override <br/> Public void onupgrade (sqlitedatabase arg0, int arg1, int arg2) {<br/> // todo auto-generated method stub <br/> string SQL = "Drop table if exists user "; </P> <p> db.exe csql (SQL); <br/> oncreate (db ); <br/>}< br/> // Add a user <br/> Public Boolean saveuser (User user) <br/> {Opendatabase (); <br/> contentvalues value = new contentvalues (); <br/> string phonenumber = user. getphonenumber (); <br/> system. out. println ("pjoneid:" + phonenumber); <br/> cursor = dB. query ("user", new string [] {"ID", "phonenumber", "name"}, "phonenumber =? ", New string [] {phonenumber}, null); <br/> cursor. movetonext (); <br/> system. out. println ("count:" + cursor. getcount (); <br/> If (cursor. getcount () = 0) <br/>{< br/> value. put ("name", user. getname (); <br/> value. put ("phonenumber", user. getphonenumber (); <br/> dB. insert ("user", null, value); <br/> system. out. println ("save"); <br/>}< br/> system. out. println ("no save"); <br/> If (cursor! = NULL) {<br/> cursor = NULL; <br/> dB. close (); <br/>}< br/> return true; <br/>}</P> <p> // obtain all users in the database <br/> public list <Map <string, Object> getuser () {<br/> opendatabase (); <br/> List <Map <string, Object> List = new arraylist <Map <string, Object> (); <br/> cursor = dB. query ("user", new string [] {"ID", "name", "phonenumber"}, null, null ); <br/> If (cursor! = NULL) {<br/> If (cursor. isbeforefirst () {<br/> while (cursor. movetonext () <br/>{< br/> Map <string, Object> map = new hashmap <string, Object> (); <br/> map. put ("name", cursor. getstring (cursor. getcolumnindex ("name"); <br/> map. put ("ID", cursor. getstring (cursor. getcolumnindex ("ID"); <br/> map. put ("phonenumber", cursor. getstring (cursor. getcolumnindex ("phonenumber"); </P> <p> list. add (MAP); <br/>}</P> <p >}< br/>} <Br/> If (cursor! = NULL) {<br/> cursor = NULL; <br/> dB. close (); <br/>}< br/> return list; <br/>}</P> <p> // modify the User Information <br/> Public Boolean edituser (User user) {<br/> // todo auto-generated method stub <br/> opendatabase (); <br/> contentvalues CV = new contentvalues (); <br/> cv. put ("phonenumber", user. getphonenumber (); <br/> cv. put ("name", user. getname (); <br/> dB. update ("user", CV, "id = + '" + User. GETID () + "'", null); <br/> DB. close (); <br/> return true; <br/>}</P> <p> // obtain a field in the user. <br/> Public String GetUserName (string phonenumber) {<br/> // todo auto-generated method stub <br/> opendatabase (); <br/> string name = NULL; <br/> cursor = dB. query ("user", new string [] {"name"}, "phonenumber = '" + phonenumber + "'", null ); <br/> If (cursor. isbeforefirst () {<br/> while (cursor. movetonext () <br/> {<br /> Name = cursor. getstring (cursor. getcolumnindex ("name"); <br/> system. out. println ("name =" + name); <br/>}< br/> If (cursor! = NULL) {<br/> cursor = NULL; <br/> dB. close (); <br/>}< br/> return name; <br/>}< br/> // delete a user by conditions <br/> Public Boolean deleteuserbyid (int id) {<br/> // todo auto-generated method stub <br/> opendatabase (); <br/> dB. delete ("user", "id = '" + ID + "'", null); <br/> system. out. println ("true"); <br/> dB. close (); <br/> return true; <br/>}</P> <p> @ override <br/> Public String getusernumber (string Str) {<br/> // todo Au To-generated method stub <br/> opendatabase (); <br/> string number = NULL; <br/> cursor = dB. query ("user", new string [] {"phonenumber"}, "name = '" + STR + "'", null ); <br/> If (cursor. isbeforefirst () {<br/> while (cursor. movetonext () <br/>{< br/> Number = cursor. getstring (cursor. getcolumnindex ("name"); <br/> system. out. println ("number =" + number); <br/>}< br/> If (cursor! = NULL) {<br/> cursor = NULL; <br/> dB. close (); <br/>}< br/> return number; <br/>}</P> <p >}< br/>

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.