Steps for SQLite to manipulate the database

Source: Internet
Author: User
Tags sqlite sqlite database


Package Com.example.dbdemo;import Android.content.context;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqlitedatabase.cursorfactory;import Android.database.sqlite.sqliteopenhelper;import Android.widget.toast;public class Mydatabasehelper extends Sqliteopenhelper {//Create a custom database action class two member variables, static constant string public Static final String create_book= "CREATE table book (ID integer primary key autoincrement,author text,price real,pages Integ Er,name text) ";p ublic static final String create_category=" CREATE table CATEGORY (ID integer PRIMARY key autoincrement "+", Category_name text,category_code integer) ";p rivate Context mcontext; Context object; Public Mydatabasehelper (context context, String name,cursorfactory factory, int version) {Super (context, name,  Factory, version); mcontext=context; Assigns a value to the current context object}//1. Method of creating a database @overridepublic void OnCreate (Sqlitedatabase db) {///////using the Execsql method of db to execute the method of creating the data table Db.execsql ( Create_book);d b.execsql (create_category); Toast.maketext (Mcontext, "Create data successfully!", Toast.length_short). Show ();} Methods for updating data, methods for updating data content, because the database is already available, the database is not created again; @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int NewVersion) {db.execsql ("drop table if exists book"), or//if present, delete the!!! Db.execsql ("drop table if exists category"); onCreate (db);}}

Package Com.example.dbdemo;import Android.app.activity;import Android.content.contentvalues;import Android.database.sqlite.sqlitedatabase;import Android.os.bundle;import Android.view.menu;import Android.view.View ; Import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.toast;public class Mainactivity extends Activity {//1. The member variable that creates the primary activity class is the database operation class private mydatabasehelper dbhelper; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); /---------------------------Call the method of writing the data in the main activity method,//How to make the call Onupgrade () method, use the version number, that is, the fourth parameter Dbhelper=new Mydatabasehelper ( Mainactivity.this, "Bookstore.db", null,2); Button btndb= (button) Findviewbyid (R.ID.BTNDB) Btndb.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {dbhelper.getwritabledatabase ();}}); /------------------To insert the data, write the corresponding code-----------------//sqlitedatabase object, with this object, you can do crud operation of the data! The Sqlitedatabase class provides an insert () method that is specifically used toData to add data. It takes three parameters, the first parameter is the table name, which table we want to add data to, and here we pass the name of the table//The second parameter is used to automatically assign NULL to some nullable columns, which is generally not available;//The third parameter is a Contentvalues object, It provides a series of put () method overloads to add data to the contentvalues by simply passing each column name in the table and the corresponding data to be added to the button btnadddata= (button) Findviewbyid ( R.id.btnadddata); Btnadddata.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) { Sqlitedatabase db=dbhelper.getwritabledatabase (); Returns an Sqlitedatabase object that can operate on the SQLite database, contentvalues values=new contentvalues ();//Start assembling the first Data values.put ("name", " Values.put ("Author", "Zhang"), Values.put ("pages", 454); Values.put ("Price", 16.96); Long I1=db.insert ("book", NULL, values);   inserting the first data; Values.clear (); Clear the data inside the content value object, Values.put ("name", "JQuery"), Values.put ("Author", "Li.sir"), Values.put ("pages", 510); Values.put ("   Price ", 19.95); Long I2=db.insert (" book ", Null,values); Insert the second data, if (i1!=-1) {Toast.maketext (Mainactivity.this, "First Data insert succeeded", Toast.length_long). Show (); if (i2!=-1) {Toast.maketext (Mainactivity.this, "First Data insert succeeded", Toast.length_long). Show ();}}); /For data-inUpdates to the line; Button Btnupdatedata= (button) Findviewbyid (r.id.btnupdata); Btnupdatedata.setonclicklistener (new Onclicklistener ()  {@Overridepublic void OnClick (View v) {sqlitedatabase db=dbhelper.getwritabledatabase (); Obtain Sqlitedatabase object; Contentvalues values=new contentvalues (); Get content value object; Values.put ("Price", 188); int i3=db.update ("book", Values, "Name=?", New String[]{"Warcraft"});//update (table name, value object, column name , determine which row of the original data if (i3!=0) {Toast.maketext (mainactivity.this, "data modification succeeded", Toast.length_long). Show ();}}); /-------------------Use the Delete button to delete the data------------button btndeletedata= (button) Findviewbyid (R.id.btndeletedata); Btndeletedata.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {sqlitedatabase db= Dbhelper.getwritabledatabase (); Get Sqlitedatabase object Dbint i4=db.delete ("book", "PAGES>?", New string[]{"$"}),//delete (table name, which parameter, specific value) Toast.maketext (mainactivity.this, "Data deletion succeeded" +i4, Toast.length_long). Show ();});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.Main, menu); return true;}} 


Steps for SQLite to manipulate the database

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.