SQLite Learning Notes

Source: Internet
Author: User

1: Create a Db.java that inherits from Sqliteopenhelper

Package Com.example.sqlite;import Android.content.context;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqlitedatabase.cursorfactory;import Android.database.sqlite.sqliteopenhelper;public Class DB extends Sqliteopenhelper {private String sql = "CREATE TABLE person (" + "ID integer primary key autoincrement," + "NA  Me text, "+" Age integer) ";p ublic DB (context context, String name, cursorfactory factory,int version) {Super (context, name, Factory, version);} @Overridepublic void OnCreate (Sqlitedatabase db) {db.execsql (SQL);} @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {//TODO auto-generated method stub}}


Mainactivity.java

Package Com.example.sqlite;import Android.content.contentvalues;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqliteopenhelper;import Android.os.Bundle; Import Android.support.v7.app.actionbaractivity;import Android.widget.textview;import Android.widget.Toast;public Class Mainactivity extends Actionbaractivity {private TextView text;p rivate sqlitedatabase database = null; @Overridepro tected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); this.text = (TextView) Findviewbyid (R.id.text); Sqliteopenhelper helper = new DB (This, "test.db", NULL, 1);d atabase = Helper.getwritabledatabase ();//Start testing the written function//put five data first for (int i = 0; i<5; i++) {Contentvalues values = new Contentvalues (); Values.put ("name", "Insert Data"); Values.put ("Age", i+20 Insert ("person", values);}    Delete ("Delete from person where age>23"); Remove the cursor cursor = query ("SELECT * from person") that is greater than 23 years old;/showdata (cursor); Display data Contentvalues values = new Contentvalues (), Values.put ("name", "Test Update Method"), Values.put ("Age", 6666), Update ("  Person ", values," age= ", New string[]{" 21 "});    Set the full age of 21 to 6666showData (cursor), cursor.close ();d atabase.close (); }/* * @param tableName table name * @param values DataSet */private void Insert (String tablename,contentvalues values) {if (databas E! = null) {Database.insert (tableName, null, values);//Can of course be database.exesql (SQL statement execution)}else {Toast.maketext (this, " Database object has not been instantiated!! ", Toast.length_long). Show ();}} /* * Delete data based on SQL statement */private void Delete (String sql) {if (database! = null) {database.execsql (SQL);//Of course you can use Database.exesql (SQL statement execution)} else {Toast.maketext (this, "the database object has not been instantiated!! ", Toast.length_long). Show ();}} /* * Query statement that returns a cursor cursor */private cursor query (String SQL) {cursor cursor = database.rawquery (sql, NULL); return cursor; /* Change data */private void update (String table,contentvalues values,string whereclause,string[] whereargs) {if (Database! = N ull) {DATABASE.UPDATe (table, values, Whereclause, Whereargs);//can also be database.exesql (SQL statement execution)}else {Toast.maketext (this, "the database object has not been instantiated!! ", Toast.length_long). Show ();}} /* * Show all data */private void ShowData (cursor cursor) {while (Cursor.movetonext ()) {int nameColumn = Cursor.getcolumnindex ("N Ame "); int agecolumn = Cursor.getcolumnindex (" Age "); String name = cursor.getstring (nameColumn); int age = Cursor.getint (Agecolumn); This.text.append ("Name:" +name+ "Ages:" +age+ "\ n");} This.text.append ("All data query finished! ");}}



SQLite Learning Notes

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.