Android: Data Storage SQLite

Source: Internet
Author: User

Android integrates SQLite at runtime, so each Android application can use the SQLite database.

We manipulate the SQLite database by Sqlitedatabase objects of this class and do not require authentication.

Where the database resides:data/< the project folder >/databases/

 Public classMainactivityextendsActivity {PrivateButton CreateDatabase, CreateTable, Selectdata, InsertData, UpdateData, Deletedaata; PrivateMysqliteopenhelper Helper; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.demo); //Initialize buttonCreateDatabase =(Button) Findviewbyid (r.id.createdatabase); CreateTable=(Button) Findviewbyid (r.id.createtable); Selectdata=(Button) Findviewbyid (r.id.selectdata); InsertData=(Button) Findviewbyid (r.id.insertdata); UpdateData=(Button) Findviewbyid (r.id.updatedata); Deletedaata=(Button) Findviewbyid (r.id.deletedaata); Helper=NewMysqliteopenhelper (mainactivity. This, "Mydb.db",                NULL, 1); /** CREATE DATABASE*/Createdatabase.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubSqlitedatabase sqlitedatabase =helper.getwritabledatabase (); Toast.maketext (mainactivity. This, "Database creation succeeded", 1000). Show ();        }        }); /** Create a table*/Createtable.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View arg0) {//TODO auto-generated Method StubSqlitedatabase sqlitedatabase =helper.getwritabledatabase (); //Create a tablesqlitedatabase. Execsql ("CREATE TABLE student (ID INTEGER PRIMARY KEY autoincrement,name text)"); Toast.maketext (mainactivity. This, "Table creation succeeded", 1000). Show ();        }        }); /** Insert Data*/Insertdata.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {//TODO auto-generated Method StubSqlitedatabase sqlitedatabase =helper.getwritabledatabase (); Sqlitedatabase. Execsql ("INSERT into student (name) VALUES (' tinyphp ')"); Toast.maketext (mainactivity. This, "Insert data Success", 1000). Show ();        }        }); /** Update Data*/Updatedata.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubSqlitedatabase sqlitedatabase =helper.getwritabledatabase (); Sqlitedatabase. Execsql ("Update student set Name= ' monkey ' where id= ' 1 '"); Toast.maketext (mainactivity. This, "Data Update succeeded", 1000). Show ();        }        }); /** Query Data*/Selectdata.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubSqlitedatabase sqlitedatabase =helper.getreadabledatabase (); //Parameters:table name, query columns, query criteria, query parameters, query result grouping, grouping result limits, sortingcursor cursor = sqlitedatabase.query ("Student",Newstring[] {"id", "name"}, "Id=?",NewString[] {"1"},NULL,                        NULL,NULL); String name=NULL;  while(Cursor.movetonext ()) {name=cursor.getstring (Cursor.getcolumnindex ("name")); } toast.maketext (mainactivity. This, "The query result name is:" +name, 1000). Show ();                }        }); /** Delete Data **/Deletedaata.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubSqlitedatabase sqlitedatabase =helper.getwritabledatabase (); Sqlitedatabase.delete ("Student", "id=?",Newstring[]{"1"}); Toast.maketext (mainactivity. This, "Data deletion succeeded", 1000). Show ();            }        }); }}

Example Download >>>>>>>>

Related articles:

Android: The sharepreferences of storage mode

Android: Data Storage SQLite

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.