A brief discussion on SQLite database

Source: Internet
Author: User
Tags sqlite database

1. Writing class inheritance Sqlithelper;

 Public class extends Sqliteopenhelper {}

2. Implement its constructor;

     Public Sqlitehelper (Context context) {        thisnull, db_version);    }          Public Sqlitehelper (Context context, String name, Cursorfactory factory,            int  version) {         Super (context, TABLE_NAME, factory, db_version);        }

3. Define the database name, table name, version, and all columns of the table

    Private Static FinalString db_name = "Playerdatas";//Database name    Private Static FinalString table_name = "datas";//Table name    Private Static Final intDb_version = 1;//Database Version//column name of the table    Private Static FinalString table_id = "_id"; Private Static FinalString table_videoid = "VideoID"; Private Static FinalString Table_url = "URL"; Private Static FinalString table_title = "TITLE"; Private Static FinalString table_content = "CONTENT"; Private Static FinalString table_actors = "ACTORS"; Private Static FinalString table_director = "DIRECTOR"; Private Static FinalString table_type = "TYPE"; Private Static FinalString Table_icon = "ICON";

4. Implement the OnCreate method, write the SQL statement, create the table

@Override Public voidonCreate (Sqlitedatabase db) { This. db =DB; StringBuilder Builder=NewStringBuilder (); Builder.append ("CREATE table if not exists");        Builder.append (table_name); Builder.append ("("); Builder.append (table_id). Append ("Integer primary key AutoIncrement,"); Builder.append (table_videoid). Append ("Integer,"); Builder.append (Table_icon). Append ("Integer,"); Builder.append (Table_url). Append ("varchar (50),"); Builder.append (Table_title). Append ("varchar (30),"); Builder.append (table_content). Append ("varchar (50)"); Builder.append (" ) ");    Db.execsql (Builder.tostring ()); }

5 Implement the Onupgrade method, which is called when the version is upgraded

    /**      * Version update     * /     @Overridepublicvoidint int newversion) {        Db.execsql ("DROP TABLE IF EXISTS" + table_name);        OnCreate (db);    }

6. Write query, delete, update and other operation methods.

/*** Query all data *@return     */     PublicCursor Query () {Sqlitedatabase db=getreadabledatabase (); //Get CorsorCursor C = db.query (table_name,NULL,NULL,NULL,NULL,NULL,NULL); returnC; }    /*** Find data by ID *@paramID *@return     */     PublicCursor Querybyid (intvideoid) {Sqlitedatabase db=getreadabledatabase (); Cursor C= Db.query (table_name,NewString[]{table_videoid}, "Videoid=?",                 NewString[]{string.valueof (VideoID)},NULL,NULL,NULL); returnC; }        /*** Delete data based on unique identification _id *@paramID*/     Public voidDeleteintID) {Sqlitedatabase db=getwritabledatabase (); System.out.println (">>>>>>>>>>>>>>>>>>>>>>>>>>" +db); Db.delete (table_name,"_id=?",Newstring[]{string.valueof (ID)}); }    /*** Insert Method *@paramValues Insert Value*/     Public voidInsert (Contentvalues values) {Sqlitedatabase db=getwritabledatabase (); Db.insert (table_name,NULL, values);    Db.close (); }

7. How to close the database

    /**      * Close Database     * /public     void  close ()        {if  null) {            db.close ();        }    }

A brief discussion on SQLite database

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.