Android advanced tutorial (13th)-Use of the android database sqlitedatabase !!

Source: Internet
Author: User

 

Hello everyone, I haven't updated my blog for a long time. Recently, due to poor health, I 've been waiting for a long time. Now, let's go to the topic ~

Android
Three data storage methods are provided. The first is file storage, and the second isSharedpreferences



Storage; DatabaseSqlitedatabase


Storage.

I don't need to talk about file storage,Sharedpreferences



You can access simple data (INT, double, float. etc). It is often used for data caching because it is easy to read and store. For details, see this series. Android
Advanced tutorial (7) ---- use of preferences in Android!

Let's talk about it today.Sqlitedatabase

Use.

MasterSqlitedatabase
, We will continue to masterContentprovider
Lay a solid foundation.

In order to give everyone a better grasp, we will finish this section by hand.Demo
.

Step 1: Create an android project named sqlitedatabasedemo.

Step 2: create a new classBooksdb. Java
This class must inherit from the abstract class android. database. SQLite. sqliteopenhelper. We need to implement two methods: oncreate () and onupdate.CodeAs follows:

Package COM. android. tutor; <br/> Import android. content. contentvalues; <br/> Import android. content. context; <br/> Import android. database. cursor; <br/> Import android. database. SQLite. sqlitedatabase; <br/> Import android. database. SQLite. sqliteopenhelper; <br/> public class booksdb extends sqliteopenhelper {<br/> private final static string database_name = "books. DB "; <br/> private final static int datab Ase_version = 1; <br/> private final static string table_name = "books_table"; <br/> Public final static string book_id = "book_id "; <br/> Public final static string book_name = "book_name"; <br/> Public final static string book_author = "book_author "; </P> <p> Public booksdb (context) {<br/> // todo auto-generated constructor stub <br/> super (context, database_name, null, database_version); <BR/>}< br/> // create a table <br/> @ override <br/> Public void oncreate (sqlitedatabase dB) {<br/> string SQL = "CREATE TABLE" + table_name + "(" + book_id <br/> + "integer primary key autoincrement," + book_name + "text, "+ book_author +" text); "; <br/> db.exe csql (SQL ); <br/>}< br/> @ override <br/> Public void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {<br/> string SQL = "Drop table If exists "+ table_name; <br/> db.exe csql (SQL); <br/> oncreate (db ); <br/>}</P> <p> Public cursor select () {<br/> sqlitedatabase DB = This. getreadabledatabase (); <br/> cursor = dB <br/>. query (table_name, null, null); <br/> return cursor; <br/>}< br/> // Add operation <br/> Public long insert (string bookname, string author) <br/>{< br/> sqlitedatabase DB = This. getwritabledatabase (); <Br/>/* contentvalues */<br/> contentvalues CV = new contentvalues (); <br/> cv. put (book_name, bookname); <br/> cv. put (book_author, author); <br/> long row = dB. insert (table_name, null, CV); <br/> return row; <br/>}< br/> // delete operation <br/> Public void Delete (int id) <br/>{< br/> sqlitedatabase DB = This. getwritabledatabase (); <br/> string where = book_id + "=? "; <Br/> string [] wherevalue = {integer. tostring (ID) }; <br/> dB. delete (table_name, where, wherevalue); <br/>}< br/> // modify the operation <br/> Public void Update (int id, string bookname, string author) <br/>{< br/> sqlitedatabase DB = This. getwritabledatabase (); <br/> string where = book_id + "=? "; <Br/> string [] wherevalue = {integer. tostring (ID) }; </P> <p> contentvalues CV = new contentvalues (); <br/> cv. put (book_name, bookname); <br/> cv. put (book_author, author); <br/> dB. update (table_name, CV, where, wherevalue); <br/>}< br/>

 

 

Step 3: Modify the main. xml layout, which consists of two edittexts and one listview. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <edittext <br/> Android: id = "@ + ID/bookname" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> </edittext> <br/> <edittext <br/> Android: id = "@ + ID/author" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> </edittext> <br/> <listview <br/> Android: id = "@ + ID/bookslist" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> </listview> <br/> </linearlayout> <br/>

Step 4: Modify the sqlitedatabasedemo. Java code as follows:

Package COM. android. tutor; <br/> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. database. cursor; <br/> Import android. OS. bundle; <br/> Import android. view. menu; <br/> Import android. view. menuitem; <br/> Import android. view. view; <br/> Import android. view. viewgroup; <br/> Import android. widget. adapterview; <br/> Import android. widget. baseadapter; <br/> Import android. wi DGET. edittext; <br/> Import android. widget. listview; <br/> Import android. widget. textview; <br/> Import android. widget. toast; <br/> public class sqlitedatabasedemo extends activity implements adapterview. onitemclicklistener {<br/> private booksdb mbooksdb; <br/> private cursor mcursor; <br/> private edittext bookname; <br/> private edittext bookauthor; <br/> private listview bookslist; </P> <p> private int B Ook_id = 0; <br/> protected final static int menu_add = menu. first; <br/> protected final static int menu_delete = menu. first + 1; <br/> protected final static int menu_update = menu. first + 2; </P> <p> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> setupviews (); <br/>}</P> <p> Public void setupviews () {<br/> mbooks DB = new booksdb (this); <br/> mcursor = mbooksdb. select (); </P> <p> bookname = (edittext) findviewbyid (R. id. bookname); <br/> bookauthor = (edittext) findviewbyid (R. id. author); <br/> bookslist = (listview) findviewbyid (R. id. bookslist); </P> <p> bookslist. setadapter (New bookslistadapter (this, mcursor); <br/> bookslist. setonitemclicklistener (this); <br/>}</P> <p> @ override <br/> Public Boolean oncreateopti Onsmenu (menu) {<br/> super. oncreateoptionsmenu (menu); </P> <p> menu. add (menu. none, menu_add, 0, "add"); <br/> menu. add (menu. none, menu_delete, 0, "delete"); <br/> menu. add (menu. none, menu_delete, 0, "Update"); <br/> return true; <br/>}</P> <p> Public Boolean onoptionsitemselected (menuitem item) <br/>{< br/> super. onoptionsitemselected (item); <br/> switch (item. getitemid () <br/>{< br/> case m Enu_add: <br/> Add (); <br/> break; <br/> case menu_delete: <br/> Delete (); <br/> break; <br/> case menu_update: <br/> Update (); <br/> break; <br/>}< br/> return true; <br/>}</P> <p> Public void add () {<br/> string bookname = bookname. gettext (). tostring (); <br/> string author = bookauthor. gettext (). tostring (); <br/> // neither the title nor the author can be blank, or exit <br/> If (bookname. equals ("") | author. equals ("") {<br/> return; <br/>} <Br/> mbooksdb. insert (bookname, author); <br/> mcursor. requery (); <br/> bookslist. invalidateviews (); <br/> bookname. settext (""); <br/> bookauthor. settext (""); <br/> toast. maketext (this, "add successed! ", Toast. length_short ). show (); <br/>}</P> <p> Public void Delete () {<br/> If (book_id = 0) {<br/> return; <br/>}< br/> mbooksdb. delete (book_id); <br/> mcursor. requery (); <br/> bookslist. invalidateviews (); <br/> bookname. settext (""); <br/> bookauthor. settext (""); <br/> toast. maketext (this, "delete successed! ", Toast. length_short ). show (); <br/>}</P> <p> Public void Update () {<br/> string bookname = bookname. gettext (). tostring (); <br/> string author = bookauthor. gettext (). tostring (); <br/> // neither the title nor the author can be blank, or exit <br/> If (bookname. equals ("") | author. equals ("") {<br/> return; <br/>}< br/> mbooksdb. update (book_id, bookname, author); <br/> mcursor. requery (); <br/> bookslist. invalidateviews (); <br/> bookname. s Ettext (""); <br/> bookauthor. settext (""); <br/> toast. maketext (this, "Update successed! ", Toast. length_short). Show (); <br/>}</P> <p> @ override <br/> Public void onitemclick (adapterview <?> Parent, view, int position, long ID) {</P> <p> mcursor. movetoposition (position); <br/> book_id = mcursor. getint (0); <br/> bookname. settext (mcursor. getstring (1); <br/> bookauthor. settext (mcursor. getstring (2); </P> <p >}</P> <p> public class bookslistadapter extends baseadapter {<br/> private context mcontext; <br/> private cursor mcursor; <br/> Public bookslistadapter (context, cursor) {</P> <p> mcontext = context; <br/> mcursor = cursor; <br/>}< br/> @ override <br/> Public int getcount () {<br/> return mcursor. getcount (); <br/>}< br/> @ override <br/> Public object getitem (INT position) {<br/> return NULL; <br/>}< br/> @ override <br/> Public long getitemid (INT position) {<br/> return 0; <br/>}< br/> @ override <br/> Public View getview (INT position, view convertview, viewgroup parent) {<br/> textview mtextview = new textview (mcontext); <br/> mcursor. movetoposition (position); <br/> mtextview. settext (mcursor. getstring (1) + "___" + mcursor. getstring (2); <br/> return mtextview; <br/>}</P> <p >}< br/>}

Step 5: RunProgramThe effect is as follows:

 

 

 


 

Step 6: view the database we have created. There are two methods: first, use the command to view: ADB shell ls data/COM. Android. Tutor/databases.

Another method is to use ddms to view the following database under the package name of the corresponding application under data/data ,:

 

 

 

Because the code is currently written and time-based, not all comments are added. If you have any questions, leave a message and I will reply as soon as possible.Source codePlease leave your email

Address: OK. It's here today. Good night!


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.