Android SQLite数据库操作代码类分享

使用示例:

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("===========","数据库初始化"); //建表 String sql = "create table if not exists tb_article(id integer primary key autoincrement,title varchar(50),content TEXT,url varchar(50),page integer)"; db.execSQL(sql); } /** * * @param db * @param oldVersion * @param newVersion */ @Override public void onUpgrade(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<Article>(); 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()) { readableDatabase.close(); } if (writableDatabase != null && writableDatabase.isOpen()) { writableDatabase.close(); } } public SQLiteDatabase getReadableDatabase() { return dataBaseHelper.getReadableDatabase(); } public SQLiteDatabase getWritableDatabase() { return dataBaseHelper.getWritableDatabase(); } }

时间: 2024-09-18 05:23:45

Android SQLite数据库操作代码类分享的相关文章

Android SQLite数据库操作代码类分享_Android

使用示例: 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 clas

[Android] SQLite数据库之增删改查基础操作

    在编程中经常会遇到数据库的操作,而Android系统内置了SQLite,它是一款轻型数据库,遵守事务ACID的关系型数据库管理系统,它占用的资源非常低,能够支持Windows/Linux/Unix等主流操作系统,同时能够跟很多程序语言如C#.PHP.Java等相结合.下面先回顾SQL的基本语句,再讲述Android的基本操作. 一. adb shell回顾SQL语句     首先,我感觉自己整个大学印象最深的几门课就包括<数据库>,所以想先回顾SQL增删改查的基本语句.而在Androi

Android SQLite数据库彻底掌握数据存储_Android

SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么. 例如:可以在Integer类型的字段中存放字符串,或者在布尔型字段中存放浮点数,或者在字符型字段中存放日期型值.  但有一种情况例外:定义为INTEGER PRIMARY KEY的字段只能存储64位整数, 当向这种字段保存除整数以外的数据时,将会产生错误. 另外, SQLite 在解析CREATE TABLE 语句时,会忽略 CREATE TABLE 语句中跟在字段名后面的数据类型信息,如下面语

Android SQLite数据库基本操作方法

程序的最主要的功能在于对数据进行操作,通过对数据进行操作来实现某个功能.而数据库就是很重要的一个方面的,Android中内置了小巧轻便,功能却很强的一个数据库–SQLite数据库.那么就来看一下在Android程序中怎么去操作SQLite数据库来实现一些需求的吧,仍然以一个小例子开始: 在创建Android项目之前,我们应该想一下我们要定义的数据库的相关信息和里面的表格的相关信息,为了日后数据库的更新更加方便 ,我们可以用一个专门的类保存数据库的相关信息,以后如果要更新数据库的话只需要该动这个类

php数据库操作model类(使用__call方法)_php技巧

本文实例讲述了php数据库操作model类.分享给大家供大家参考,具体如下: 该数据库操作类使用__call()方法实现了数据的查找功能. 代码如下: <?php /* 作者 : shyhero */ define("HOSTNAME","127.0.0.1"); define("USERNAME","root"); define("PASSWORD",""); define(&q

php实现的简单数据库操作Model类_php技巧

本文实例讲述了php实现的简单数据库操作Model类.分享给大家供大家参考,具体如下: 该数据库模型类可实现数据库的增删改查,简化数据库操作. 1. config.php代码: <?php define("HOSTNAME","127.0.0.1"); define("USERNAME","root"); define("PASSWORD",""); define("DA

Android SQLite数据库版本升级的管理实现

Android SQLite数据库版本升级的管理实现 我们知道在SQLiteOpenHelper的构造方法: super(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) 中最后一个参数表示数据库的版本号.当新的版本号大于当前的version时会调用方法: onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 所以我们

android sqlite数据库表字段更新

问题描述 android sqlite数据库表字段更新 android sqlite数据库表字段更新,调用onUpgrade方法出现了Can't upgrade read-only database from version 3 to 4: 异常,有哪个大神碰到过或者知道怎么解决. 代码: public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String sql = " alter table pe

PHP基于单例模式实现的数据库操作基类_php技巧

本文实例讲述了PHP基于单例模式实现的数据库操作基类.分享给大家供大家参考,具体如下: 配置文件: <?php $db = array( 'host'=>'localhost', 'user'=>'root', 'password'=>'', 'database'=>'test', ) ?> php 数据库基类: <?php class db { public $conn; public static $sql; public static $instance=n