Sqlite achieves news collection and cancellation, and sqlite achieves news collection

Source: Internet
Author: User

Sqlite achieves news collection and cancellation, and sqlite achieves news collection

After learning about oracle, I thought that the database only exists on the server side. After learning about android, I found that android and Ios are "carrying" the database-SQLite, is a lightweight, embedded, and relational database system. It is a widely used database system such as Android and IOS. Used to store the local persistent state. I just wrote a function to add news to favorites for your reference.

In Android, we operate the SQLite database through objects of the SQLiteDatabase class. Because the SQLite database does not need to establish connections and Identity Authentication like the C/S database, and the single-file database features of the SQLite database, it makes obtaining the SQLiteDatabase object as simple as obtaining the object of the Operation file.

Sqlite needs to create databases, tables, and add, delete, modify, and query functions. Therefore, the first step is to create a database. SQliteOpenHelper is an abstract class to manage database creation and version management. To use it, you must implement its nCreate (SQLiteDatabase), onUpgrade (SQLiteDatabase, int, int) method.

// Create a database and a table
1 private static final String DBNAME = "news. db "; 2 private static final int VERSION = 3; // set VERSION 3 private static final String TBL_DETAILNEWS =" news "; // Create Table 4 private static final String TBL_DETAILNEWS_COLUMN_TITLE = "_ title"; 5 private static final String TBL_DETAILNEWS_COLUMN_URL = "_ url "; 6 private static final String TBL_DETAILNEWS_COLUMN_DOCID = "_ docid"; 7 private static final String TBL_DETAILNEWS_COLUMN_STATE = "_ state"; 8 9 public NewsDBHelper (Context context) {10 super (context, DBNAME, null, VERSION); 11} 12 13 @ Override14 public void onCreate (SQLiteDatabase db) {15 // TODO Auto-generated method stub16 StringBuffer sb = new StringBuffer (); 17 sb. append ("create table if not exists"); 18 sb. append (TBL_DETAILNEWS + "("); 19 sb. append (TBL_DETAILNEWS_COLUMN_DOCID + "varchar (100) primary key,"); // Set primary key 20 sb. append (TBL_DETAILNEWS_COLUMN_TITLE + "varchar (100),"); 21 sb. append (TBL_DETAILNEWS_COLUMN_URL + "varchar (100),"); 22 sb. append (TBL_DETAILNEWS_COLUMN_STATE + "integer"); 23 sb. append (")"); 24 db.exe cSQL (sb. toString (); 25 26} 27 @ Override28 public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {29 String sql2 = "drop table if exists" + TBL_DETAILNEWS; 30 db.exe cSQL (sql2); // create 31 onCreate (db); 32}

Android provides a class named SQLiteDatabase, which encapsulates APIs for database operations. It can be used to implement basic CRUD operations. getWritableDatabase () and getReadableDatabase () can be used to obtain database instances, and some dao-layer methods can be written to perform table operations:

Public class DetailNewsDao {private NewsDBHelper helper; public DetailNewsDao (Context context) {helper = new NewsDBHelper (context ); // establish a connection with the database} // insert public void insertDetsilNews (News news) {SQLiteDatabase db = helper. getWritableDatabase (); db.exe cSQL ("insert into news (_ title, _ url, _ docid)" + // title of the news to be added to favorites, which identifies docid, the detailed Address url is passed into the database, so that you can view the detailed News "values (?,?,?) ", New String [] {news. getTitle (), news. getDetailUrl (), news. getDocid ()}); db. close () ;}// delete data public void del (String docid) {// delete data SQLiteDatabase db = helper according to the input docid. getReadableDatabase (); db.exe cSQL ("delete from news where _ docid =? ", New Object [] {docid}); db. close () ;}// query the public List of data <News> findSelected () {SQLiteDatabase db = helper. getReadableDatabase (); Cursor c = db. rawQuery ("select * from news", null); // rawQuery () is used only when data is queried. execSQl () is used to add, delete, modify, and create tables () list <News> list = new ArrayList <News> (); while (c. moveToNext () {News news = new News (); 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 (); return list ;}}

 

Use actionbar to set the menu button, click favorites, and execute the Code:

If (item. getTitle (). equals ("favorites") {Toast. makeText (this, "added to Favorites", Toast. LENGTH_LONG ). show (); detailNewsDao. insertDetsilNews (news); item. setTitle ("Remove from favorites");} else {detailNewsDao. del (news. getDocid (); Toast. makeText (this, "Remove from favorites", Toast. LENGTH_LONG ). show (); item. setTitle ("favorites ");}

In this way, the entire favorites will be removed.

 

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.