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