Android SQLite The simplest demo implementation (adding and deleting changes)

Source: Internet
Author: User

Originally did not want to write this blog, but see the internet on the Android database operation of the blog is very detailed, for beginners like me to understand the basic operation of SQLite is a certain difficulty, so I refer to some blog posts on the Internet, and I personally groped over, Hope to write a blog record of the most basic operation of SQLite, but also want to be able to help Android novice.

Reference Blog: http://www.20864.com/201247/274.html

Here is just a demonstration of the demo, and did not realize what the specific function, only to achieve the database of additions and deletions to check and change operations.

Here are the steps to implement the demo:

The first step is to create your own SQL operations class DBHelper

 Packagecom.example.SQLiterTest;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper;ImportAndroid.util.Log;/*** The following two methods of Sqliteopenhelper can be used to sqlitedatabase objects: * getreadabledatabase () Create or open a query database * getwritabledatabase () Create or open a writable database*/ Public classDBHelperextendsSqliteopenhelper {/*** constructor, must be implemented *@paramContext Contextual Path *@paramName Database names *@paramFactory optional cursor factory, usually null *@paramversion Current database revision number*/    PrivateSqlitedatabase mdatabase =NULL; Private Static FinalString user_table = "USER";  PublicDBHelper (Context context, String name, Cursorfactory factory,intversion) {        Super(context, name, Factory, version); Mdatabase= This. Getwritabledatabase (); }    //called when the database is first created, in which database tables are typically created@Override Public voidonCreate (Sqlitedatabase db) {//Use the Execsql () method to execute the SQL statement, and if there is no exception, the method does not return a valueDb.execsql ("CREATE table User (ID INTEGER PRIMARY KEY autoincrement," + "name varchar (), address TEXT)"); LOG.V ("Mytab", "-->oncreate"); }    //when the database needs to be modified, the Android system will invoke this method on its own initiative. @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {}    //When you open a database, the callback function is generally not used@Override Public voidOnOpen (Sqlitedatabase db) {Super. OnOpen (DB); LOG.V ("Mytab", "-->onopen"); } @Override Public synchronized voidClose () {Super. Close (); }}

The second step, in the activity of the OnCreate function to implement additions and deletions to check the function.

 Packagecom.example.SQLiterTest;Importandroid.app.Activity;Importandroid.content.ContentValues;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public classMyActivityextendsActivity {/*** Called when the activity is first created. */    PrivateSqlitedatabase DB; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); //gets an instance of SQLite that handles Sqliteopenhelper subclasses, if read-only, can take Getreadabledatabase (),//In This example, we award the data to be sqlitedatabase by the operation of the instance, using the writable way. db =NewDBHelper (Getapplicationcontext (), "MyDB",NULL, 1). Getwritabledatabase (); Contentvalues CV=Newcontentvalues (); Cv.put ("Name", "Jackli"); Cv.put ("Address", "Qingyuan"); //InsertDb.insert ("User",NULL, CV); LOG.V ("Mytab", "-->have insert"); //UpdateContentvalues Cv2 =Newcontentvalues (); Cv2.put ("Name", "Haixin"); Db.update ("User", Cv2, "Name=?",Newstring[]{"Jackli"}); LOG.V ("Mytab", "-->have updated"); //Enquirystring[] columns = {"Name", "Address"}; Cursor Cursor= Db.query ("User", columns, "name =?",NewString[] {"Haixin"},NULL,NULL,NULL);  while(Cursor.movetonext ()) {String myname= Cursor.getstring (Cursor.getcolumnindex ("name")); LOG.V ("Mytab", "---->" +myname); }        //DeleteDb.delete ("User", "name =?",Newstring[]{"Haixin"}); LOG.V ("Mytab", "-->have Delete"); } @Overrideprotected voidOnDestroy () {Super. OnDestroy ();    Db.close (); }}

  

In general, the entire process is only two steps, and the first time you run the demo, you will create a database called "MyDB" and a data table called "User".

After you run the program, some of the following information is displayed in Logcat:

05-19 08:08:30.864    1802-1802/com.example.sqlitertest v/mytab:-->onopen05-19 08:08:30.872    1802-1802/ Com.example.SQLiterTest v/mytab:-->have insert05-19 08:08:30.888    1802-1802/com.example.sqlitertest v/mytab: -->have updated05-19 08:08:30.888    1802-1802/com.example.sqlitertest v/mytab:---->HaiXin05-19 08:08:30.900    1802-1802/com.example.sqlitertest V/mytab:-->have Delete

The process of executing the program is clearly visible through the information in the Logcat.

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.