Public Code Reference (Sqliteopenhelper)

Source: Internet
Author: User

 PackageCom.fredric.util;Importandroid.content.ContentValues;ImportAndroid.content.Context;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper;/*-* SQLite database operation package*/ Public classDBHelperextendssqliteopenhelper{Private Final StaticString db_name = "Demo.db"; Private Final StaticString tbl_name = "Users"; Private Static Final intDb_version = 1; Private Final StaticString create_tbl= "CREATE TABLE IF not EXISTS users (_id INTEGER PRIMARY KEY autoincrement, username varchar, password varchar)"; PrivateSqlitedatabase DB;  PublicDBHelper (Context context, String name, Cursorfactory factory,intversion) {        Super(context, name, Factory, version); }         PublicDBHelper (Context context) { This(Context, Db_name,NULL, db_version); }    //executed when the database is first built, such as creating tables, initializing data, etc.@Override Public voidonCreate (Sqlitedatabase db) { This. db =DB;    Db.execsql (CREATE_TBL); }    //executes when the database needs to be updated, such as deleting a table, creating a table@Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {            }         Public voidInsert (Contentvalues values) {Sqlitedatabase db=getwritabledatabase (); Db.insert (Tbl_name,NULL, values);      Db.close (); }                //Query Method     PublicCursor rawquery (String sql, string[] args) {Sqlitedatabase db=getreadabledatabase (); Cursor Cursor=db.rawquery (sql, args); returncursor; }                //Delete Data     Public voidDeleteintID) {Sqlitedatabase db=getwritabledatabase (); Db.delete (Tbl_name,"_id=?",Newstring[]{string.valueof (ID)}); }                      //Update the contents of a database     Public voidUpdate (contentvalues values, String whereclause, string[] whereargs) {sqlitedatabase db=getwritabledatabase ();      Db.update (Tbl_name, values, Whereclause, Whereargs); }                //Close the database     Public voidClose () {if(db! =NULL) {db.close (); }      }     }

Public Code Reference (Sqliteopenhelper)

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.