Go How Android can add and delete an SQLite database [insert, update, and deletion]

Source: Internet
Author: User
Tags ord

Importandroid.content.ContentValues;ImportAndroid.content.Context;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper;ImportAndroid.util.Log; Public classDBHelperextendsSqliteopenhelper {Final Private StaticString mdbname= "Imgfornote"; Final Private Static intMdbversion=1; Private StaticDBHelper minstance=NULL; Private Final StaticString mtuserphoto= "Userphoto"; Final Private StaticString mcreatesqlfornoteclass= "CREATE table if not exists noteclass (ClassId integer PRIMARY key ASC Autoincrement,classna Me NVARCHAR (+), rowtime timestamp default (DateTime (' Now ', ' localtime ')) "; Final Private StaticString mcreatesqlforuserphoto= "CREATE table if not exists Userphoto (PhotoID integer PRIMARY key ASC Autoincrement,photona Me varchar ($), userpt varchar (+), title varchar (255), ClassId integer,content NVARCHAR (+), tag NVARCHAR (200), Remark text,status integer default 0,rowtime timestamp default (DateTime (' Now ', ' localtime ')) "; Final Private StaticString[] minsertsqlfornoteclass={"INSERT into Noteclass (className) VALUES (' Default classification [private] ');", "INSERT into Noteclass ( ClassName) VALUES (' reading notes [private] '); "," INSERT into Noteclass (className) VALUES (' Electronic data [public] '); "}; PrivateDBHelper (Context context, Cursorfactory Factory) {Super(context, Mdbname, Factory, mdbversion); }          Public StaticDBHelper getinstance (context context, Sqlitedatabase.cursorfactory Factory) {if(minstance==NULL) {minstance=NewDBHelper (context,factory); }        returnminstance; } @Override Public voidonCreate (Sqlitedatabase db) {//Create a tableDb.execsql (Mcreatesqlfornoteclass);        Db.execsql (Mcreatesqlforuserphoto); //Initializing Data         for(inti=0;i<minsertsqlfornoteclass.length;i++) Db.execsql (Minsertsqlfornoteclass[i]); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {        //TODO auto-generated Method Stub     }         PrivateCursor execsqlforcursor (String sql, string[] selectionargs) {sqlitedatabase db=getwritabledatabase (); LOG.I ("Execsqlforcursor", SQL); returndb.rawquery (SQL, Selectionargs); }    Private voidexecsql (String sql) {Try{sqlitedatabase db=getwritabledatabase ();        Execsql (SQL,DB); }Catch(Exception e) {LOG.E ("Execsql Exception", E.getmessage ());        E.printstacktrace (); }    }    Private voidexecsql (String sql,sqlitedatabase db) {Try{db.execsql (SQL); LOG.I ("Execsql", SQL); }Catch(Exception e) {LOG.E ("Execsql Exception", E.getmessage ());        E.printstacktrace (); }    }    //Add Photo Info     Public LongInsertuserphoto (String photoname,string title) {Sqlitedatabase db=getwritabledatabase (); Contentvalues CV=Newcontentvalues (); Cv.put ("Photoname", Photoname); Cv.put ("Title", title); returnDb.insert (Mtuserphoto,NULL, CV); }    //Query Photo Information     PublicCursor Searchphoto (introw,string Sort) {Cursor cur=NULL; Try{String ord= (sort==NULL|| Sort.tolowercase (). StartsWith ("a"))? " ASC ":" DESC "; String SQL= "SELECT * from Userphoto ORDER by PhotoID" +Ord; String[] args={string.valueof (row)}; if(row>0) {SQL+ = "Limit?"; }Else{args=NULL; } cur=execsqlforcursor (Sql,args); }Catch(Exception e) {cur=NULL; LOG.E ("Searchphoto Exception", E.getmessage ());        E.printstacktrace (); }        returncur; }      //Edit photo Information     Public intUpdateuserphoto (intPhotoID,intclassid,string title,string content, String tag) {Sqlitedatabase db=getwritabledatabase (); Contentvalues CV=Newcontentvalues (); Cv.put ("ClassId", classId); Cv.put ("Title", title); Cv.put ("Content", content); Cv.put ("Tag", tag); String[] args={string.valueof (photoid)}; returnDb.update (Mtuserphoto, CV, "photoid=?"), args); }    //Delete Photo information     Public intDeleteuserphoto (intphotoid) {Sqlitedatabase db=getwritabledatabase (); String[] args={string.valueof (photoid)}; returnDb.delete (Mtuserphoto, "photoid=?"), args); }}

Go How Android can add and delete an SQLite database [insert, update, and deletion]

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.