Package Cn.hackcoder.beautyreader.db;import Android.content.context;import Android.database.sqlite.SQLiteDatabase ; Import Android.database.sqlite.sqliteopenhelper;import android.util.log;/** * Created by Hackcoder on 15-1-25. */public class Databasehelper extends Sqliteopenhelper {private static final String DbName = "sample.db"; private static int dbversion = 1; Public Databasehelper (Context context) {super (context,dbname,null,dbversion); } @Override public void OnCreate (Sqlitedatabase db) {log.d ("===========", "Database Initialization"); Build table String sql = "CREATE table if not exists tb_article (ID integer PRIMARY key autoincrement,title varchar (), con Tent Text,url varchar (+), page integer) "; Db.execsql (SQL); }/** * * @param db * @param oldversion * @param newversion */@Override public void Onupgrad E (sqlitedatabase db, int oldversion, int newversion) {}}
Package Cn.hackcoder.beautyreader.service;import Android.content.context;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Java.util.arraylist;import Java.util.list;import Cn.hackcoder.beautyreader.db.databasehelper;import cn.hackcoder.beautyreader.model.article;/** * Created by Hackcoder on 15-1-25. */public class Articleservice {private Databasehelper databasehelper; Private Sqlitedatabase readabledatabase; Private Sqlitedatabase writabledatabase; Public Articleservice (Context context) {Databasehelper = new Databasehelper (context); } public void Add (article article) {String sql = ' INSERT INTO tb_article (id,title,content,url,page) VALUES (?,?, ?,?,?)"; Getreadabledatabase (). Execsql (SQL, New Object[]{null, Article.gettitle (), Article.getcontent (), Article.geturl (), Article.getpage ()}); } public void Delete (int id) {String sql = ' Delete from tb_article where id =? '; Getreadabledatabase (). ExecsQL (SQL, new Object[]{id}); } public void DeleteAll () {String sql = ' delete from tb_article '; Getreadabledatabase (). Execsql (Sql,null); } public void update (article article) {String sql = "Update tb_article set title=?,content=?,url=?,page =? WHERE id =? "; Getreadabledatabase (). Execsql (SQL, New Object[]{article.gettitle (), Article.getcontent (), Article.geturl (), Article.getpage (), Article.getid ()}); } public void Updatecontentofurl (string url,string content) {String sql = "Update tb_article set content=? Where url =? "; Getreadabledatabase (). Execsql (SQL, new Object[]{content,url}); } public article find (int id) {Article Article = new article (); String sql = "Select Id,title,content,url,page from tb_article where id =?"; cursor cursor = Getreadabledatabase (). Rawquery (SQL, new string[]{string.valueof (ID)}); if (Cursor.movetonext ()) {Article.setid (ID); Article.settitle (CURsor.getstring (Cursor.getcolumnindex ("title"))); Article.setcontent (Cursor.getstring (Cursor.getcolumnindex ("content")); Article.seturl (Cursor.getstring (cursor.getcolumnindex ("url")); Article.setpage (Cursor.getint (Cursor.getcolumnindex ("page")); Cursor.close (); return article; } cursor.close (); return null; } public list<article> findbyurl (String url) {list<article> articles = new arraylist<article> (); String sql = "Select Id,title,content,url,page from tb_article where url =?"; cursor cursor = Getreadabledatabase (). Rawquery (SQL, new String[]{url}); while (Cursor.movetonext ()) {Article Article = new article (); Article.setid (Cursor.getint (Cursor.getcolumnindex ("id"))); Article.settitle (Cursor.getstring (Cursor.getcolumnindex ("title")); Article.setcontent (Cursor.getstring (Cursor.getcolumnindex ("content")); Article.seturl (Cursor.getstring (cursor.getcolumnindex ("url")); Article.setpage (Cursor.getint (Cursor.getcolumnindex ("page")); Articles.add (article); } cursor.close (); return articles; } public int getcountofpage (int page) {String sql = ' SELECT COUNT (*) from tb_article where page =? "; cursor cursor = Getreadabledatabase (). Rawquery (SQL, New String[]{string.valueof (page)}); Cursor.movetofirst (); int count = cursor.getint (0); Cursor.close (); return count; } public list<article> getarticlesofpage (int curpage) {list<article> articles = new Arraylist<art Icle> (); String sql = "Select Id,title,content,url,page from tb_article where page =?"; cursor cursor = Getreadabledatabase (). Rawquery (sql,new string[]{string.valueof (Curpage)}); while (Cursor.movetonext ()) {Article Article = new article (); Article.setid (Cursor.getint (cursor. Getcolumnindex ("id"))); Article.settitle (Cursor.getstring (Cursor.getcolumnindex ("title")); Article.setcontent (Cursor.getstring (Cursor.getcolumnindex ("content")); Article.seturl (Cursor.getstring (cursor.getcolumnindex ("url")); Article.setpage (Cursor.getint (Cursor.getcolumnindex ("page")); Articles.add (article); } cursor.close (); return articles; } public int countofsum () {String-sql = "SELECT COUNT (*) from tb_article"; cursor cursor = Getreadabledatabase (). rawquery (sql, NULL); Cursor.movetofirst (); int count = cursor.getint (0); Cursor.close (); return count; } public list<article> getarticles (int start, int pageSize) {list<article> articles = new ArrayList <Article> (); String sql = "Select Id,title,content,url,page from Tb_article limit?,?"; cursor cursor = Getreadabledatabase (). Rawquery (Sql,new String[]{string.valueOf (start), string.valueof (PageSize)}); while (Cursor.movetonext ()) {Article Article = new article (); Article.setid (Cursor.getint (Cursor.getcolumnindex ("id"))); Article.settitle (Cursor.getstring (Cursor.getcolumnindex ("title")); Article.setcontent (Cursor.getstring (Cursor.getcolumnindex ("content")); Article.seturl (Cursor.getstring (cursor.getcolumnindex ("url")); Article.setpage (Cursor.getint (Cursor.getcolumnindex ("page")); Articles.add (article); } cursor.close (); return articles; } public void Closedb () {if (readabledatabase! = null && readabledatabase.isopen ()) {Readab Ledatabase.close (); } if (writabledatabase! = null && writabledatabase.isopen ()) {writabledatabase.close (); }} public Sqlitedatabase Getreadabledatabase () {return databasehelper.getreadabledatabase (); } public SqlitedatabaseGetwritabledatabase () {return databasehelper.getwritabledatabase (); }}
Android Database Operations