SQLite Help Class

Source: Internet
Author: User
Tags sqlite

1, first write a class inherit from Sqliteopenhelper

 Public  class dbhelper extends sqliteopenhelper{    Private Static Final intDatabase_version =1;//Database version number    Private Static FinalString database_name ="";//Database name     Public DBHelper(Context context) {Super(Context, database_name,NULL, database_version); }@Override     Public void onCreate(Sqlitedatabase db) {String sql ="";    Db.execsql (SQL); }@Override     Public void Onupgrade(Sqlitedatabase DB,intOldversion,intnewversion) {}}

2, need a database management class, such as INSERT, delete, etc.

 Public Abstract  class Dbdao{    PrivateContext context;PrivateDBHelper DBHelper; PublicSqlitedatabase sqlitedatabase; Public Dbdao(Context context) {Super(); This. Context = Context; }/** * Open Database connection * /     Public void opendb(Context context) {DBHelper =NewDBHelper (context);    Sqlitedatabase = Dbhelper.getwritabledatabase (); }/** * Close Database connection * /     Public void Closedb(Context context) {if(Sqlitedatabase.isopen ())        {Sqlitedatabase.close (); }    }/** * Update data * /     Public int Updata(String table_name, contentvalues values,intID) {OPENDB (context);intUpdate = SQLITEDATABASE.UPDATE (table_name, values,"_id=?",NewString[]{string.valueof (ID)}); Closedb (context);returnUpdate }/** * Insert data * /     Public void Insert(String table_name, contentvalues values)        {opendb (context); Sqlitedatabase.insert (TABLE_NAME,NULL, values);    Closedb (context); }/** * Delete data according to unique _id * /     Public void Delete(String table_name,intID) {OPENDB (context);Try{Sqlitedatabase.delete (table_name,"_id=?",NewString[]{string.valueof (ID)}); }Catch(Exception e)        {E.printstacktrace (); }finally{closedb (context); }    }/** * Query data * *     Public void Search(String table_name,intID) {Sqlitedatabase database = Dbhelper.getreadabledatabase (); String sql ="SELECT * from"+ table_name +"where _id =?"; cursor cursor = database.rawquery (SQL,NewString[]{string.valueof (ID)}); }}

SQLite Help class

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.