Android SQLite database Operation code class sharing _android

Source: Internet
Author: User
Tags sqlite sqlite database

Use examples:

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 {
    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 (m), Conte NT Text,url varchar (m), page integer) ";
    Db.execsql (SQL);

  /**
   * *
   @param db *
   @param oldversion
   * @param newversion/
   @Override public
  void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {

  }
}

Class Source:

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 {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&LT;ARTICL
    E> ();
    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&lt ;
    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 ()) {Readabledataba
    Se.close ();
    } if (writabledatabase!= null && writabledatabase.isopen ()) {writabledatabase.close ();
  } public Sqlitedatabase Getreadabledatabase () {return databasehelper.getreadabledatabase ();
  Public Sqlitedatabase Getwritabledatabase () {return databasehelper.getwritabledatabase ();
 }
}
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.