Android------sqliteopenhelper class

Source: Internet
Author: User

Sqliteopenhelper: is a tool class for managing databases provided by Android that can be used to manage database creation and version updates.
is an abstract class, which means that if we want to use it, we need to create our own helper class to inherit it.




Method:
OnCreate ():
Generate database tables for the first time you use the software.
When the Sqlitedatabase instance is acquired, the Android system automatically generates a database if the database does not exist, followed by the call to the OnCreate method.
Onupgrade ():
The database table structure is updated when the software is upgraded, and is called when the database version has changed.




Getreadabledatabase (): Opens the Sqlitedatabase object corresponding to the database in a read-write manner.
Getwriteabledatabase (): Opens the Sqlitedatabase object corresponding to the database in a read manner.


Both of these methods can open or create a database and return an object that can read and write to the database.
The difference is that when the database is not writable (for example: Disk space is full) the object returned by the Getreadabledatabase () method opens the database in a read-only manner
The Getwriteabledatabase () method will cause an exception.




CURSOR: Cursor result set


Personsqliteopenhelper.java:


public class Personsqliteopenhelper extends Sqliteopenhelper {/** * Database constructor *  * @param context *//*name: Database name//*f Actory: Cursor Factory//*version: Database version number >=1 */public Personsqliteopenhelper (context context) {Super (context, "MYDB.DB3", null , 1);//TODO auto-generated Constructor stub}/** * method called when the database was first created */@Overridepublic void OnCreate (Sqlitedatabase db) {//T ODO auto-generated method Stubstring sql = "CREATE table person (_id integer, name varchar (a), age integer);"; Db.execsql (SQL);//create Person Table}/** * Call method when database version number is updated */@Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int NewVersion) {//TODO auto-generated method stub}}

Persondao.java:


public class Persondao {public static personsqliteopenhelper Mhelper;public Persondao (Context context) {//TODO Auto-gene Rated constructor Stubmhelper = new Personsqliteopenhelper (context); Add to person A data public void insert (person person) {Sqlitedatabase db = Mhelper.getwritabledatabase (); if (Db.isopen ()) {/ /If the database is open, perform an insert operation//String sql = "INSERT into person (name, age) VALUES (' Lisi ', 12);"; Db.execsql ("INSERT into person (name,age) VALUES (?,?)", new object[] {person.getname (), Person.getage ()});d b.close ();}} public void Delete (int id) {Sqlitedatabase db = Mhelper.getwritabledatabase (), if (Db.isopen ()) {db.execsql ("delete from P Erson where _id =? ", new integer[] {ID});d b.close ();}} public void update (int ID, String name) {Sqlitedatabase db = Mhelper.getwritabledatabase (); if (Db.isopen ()) {Db.execsql (" Update person Set name =? where _id =? ", new object[] {name, id});d b.close ();}} Public list<person> Queryall () {Sqlitedatabase db = Mhelper.getreadabledatabase (); if (Db.isopen ()) {//Db.execsql ("select * from person;"); cursor cursor = db.rawquery ("Select _id, name, age from person;", null); int ID, age; String name;if (cursor = null && cursor.getcolumncount () > 0) {list<person> personlist = new ARRAYLIST&L T Person> (); while (Cursor.movetonext ()) {id = cursor.getint (0), name = Cursor.getstring (1); age = Cursor.getint (2); Personlist.add (new person (ID, name, age));} Db.close (); return personlist;} Db.close ();} return null;} Public person Queryitem (int id) {Sqlitedatabase db = Mhelper.getreadabledatabase (); if (Db.isopen ()) {int _id; String Name;int age; cursor cursor = db.rawquery ("Select _id,name,age from person where _id =?", new string[] {id + ""}); if (cursor! = NULL & amp;& Cursor.movetofirst ()) {_id = cursor.getint (0); name = cursor.getstring (1); age = Cursor.getint (2);d b.close (); return new person (_id, name, age);} Db.close ();} return null;}}


Testcase.java:

Import Java.util.list;import DAO. Persondao;import entities. Person;import DB. Personsqliteopenhelper;import Android.database.sqlite.sqlitedatabase;import Android.test.androidtestcase;import Android.util.log;public class TestCase extends Androidtestcase {private static final String TAG = "TestCase";p ublic void t EST () {////database when created//call this method, the database is not created personsqliteopenhelper Openhelper = new Personsqliteopenhelper (GetContext ());// At this point, the OnCreate method is created and executed Openhelper.getreadabledatabase ();} public void Testinsert () {Persondao dao = new Persondao (GetContext ());d Ao.insert (new person (0, "guanxi", 77));} public void Testdelete () {Persondao dao = new Persondao (GetContext ());d ao.delete (1);} public void Testupdate () {Persondao dao = new Persondao (GetContext ());d ao.update (1, "Fengjie");} public void Testqueryall () {Persondao dao = new Persondao (GetContext ()); list<person> persons = Dao.queryall (); for (person person:persons) {log.i (TAG, person.tostring ());}} public void Testqueryitem () {Persondao DAO = new Persondao (GetContext ()); Person person = Dao.queryitem (1); LOG.I (TAG, person.tostring ());}}




This program source code: Http://pan.baidu.com/s/1hqrgwGO




Android------sqliteopenhelper class

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.