Before learning Oracle, simply think that the database only exists on the server side, after learning Android only to discover that the original Android and iOS itself is "carry" the database--sqlite, is a lightweight, embedded, relational database, Android, A widely used database system such as iOS. Used to store the on-premises state. Just write a realization of the function of the news collection, write out for everyone to reference.
In Android we manipulate the SQLite database by Sqlitedatabase objects of this class. Since the SQLite database does not need to establish the connection and authentication characteristics like the C/S database, as well as the characteristics of the SQLite database single file database, obtaining the Sqlitedatabase object is as simple as getting the object of the operation file.
SQLite is created by creating a database, creating a table, and then making additional deletions and checks. So the first step is to create a database that Sqliteopenhelper is an abstract class to manage the creation and versioning of the database. To use it you must implement its ncreate (Sqlitedatabase), Onupgrade (sqlitedatabase, int, int) method
Create a database, build a table
1Private Static FinalString dbname= "News.db"; 2Private Static Final intVersion=3; //Set version number 3Private Static FinalString tbl_detailnews= "News"; //create a table with a table named news 4Private Static FinalString tbl_detailnews_column_title= "_title"; 5Private Static FinalString tbl_detailnews_column_url= "_url"; 6Private Static FinalString tbl_detailnews_column_docid= "_docid"; 7Private Static FinalString tbl_detailnews_column_state= "_state"; 8 9 PublicNewsdbhelper (Context context) {10Super(Context,dbname,NULL, VERSION);11 }12 13@Override14 Public voidonCreate (Sqlitedatabase db) {15//TODO auto-generated Method StubStringBuffer sb=NewStringBuffer ();Sb.append ("CREATE table if not exists");Sb.append (tbl_detailnews+ "(");Sb.append (tbl_detailnews_column_docid + "varchar" PRIMARY key, "); //Set Primary key Sb.append (tbl_detailnews_column_title+ "varchar (100),");Sb.append (tbl_detailnews_column_url+ "varchar (100),");Sb.append (tbl_detailnews_column_state+ "integer");Sb.append (")");24Db.execsql (sb.tostring ());25 26 }27@Override28 Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {String sql2= "drop table if exists" +tbl_detailnews;30Db.execsql (SQL2); //Create 31onCreate (db);32}
Android provides a class named Sqlitedatabase that encapsulates some of the APIs that manipulate the database. Using it to implement basic CRUD operations, you can get the DB instance through Getwritabledatabase () and Getreadabledatabase (), and you can write some DAO layer methods to perform the operation on the table:
Public classDetailnewsdao {PrivateNewsdbhelper Helper; PublicDetailnewsdao (Context context) {Helper=NewNewsdbhelper (context); //Establish a connection with the database } //Insert Data Public voidInsertdetsilnews (News) {Sqlitedatabase db=helper.getwritabledatabase (); Db.execsql (INSERT INTO News (_TITLE,_URL,_DOCID) +//will be the title of the news to be collected, the logo docid, the detailed address URL to the database, you can open the news in detail"VALUES (?,?,?)",Newstring[]{news.gettitle (), News.getdetailurl (), News.getdocid ()}); Db.close (); } //delete data Public voiddel (String docid) { //DOCID Delete data based on incoming parameters Sqlitedatabase db=helper.getreadabledatabase (); Db.execsql ("Delete from news where _docid =?",Newobject[]{docid}); Db.close (); } //Querying data PublicList<news>findselected () {Sqlitedatabase db=helper.getreadabledatabase (); Cursor C=db.rawquery ("SELECT * FROM News",NULL); //Only use Rawquery (), add, delete, change and build table when querying data, use Execsql () List<News> list=NewArraylist<news>(); while(C.movetonext ()) {News News=NewNews (); News.settitle (c.getstring (C.getcolumnindex ("_title"))); News.setdetailurl (c.getstring (C.getcolumnindex ("_url"))); News.setdocid (c.getstring (C.getcolumnindex ("_docid"))); List.add (news); } c.close (); Db.close (); returnlist; }}
Use Actionbar to do the menu button, click Collection, execute code:
if (Item.gettitle (). Equals ("collection")) { Toast.maketext (This, "Favorite succeeded", Toast.length_long). Show () ; Detailnewsdao.insertdetsilnews (news); Item.settitle ("Cancel Collection"); Else { Detailnewsdao.del (news.getdocid ()); Toast.maketext (This, "Cancel collection", Toast.length_long). Show (); Item.settitle ("collection"); }
So the whole collection, cancel the collection is done.
SQLite implements the news collection and cancels the collection