Android SQLite數(shù)據(jù)庫操作代碼類分享
更新時(shí)間:2015年03月20日 11:21:16 投稿:junjie
這篇文章主要介紹了Android SQLite數(shù)據(jù)庫操作代碼類分享,本文直接給出實(shí)現(xiàn)代碼和使用代碼,需要的朋友可以參考下
使用示例:
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("===========","數(shù)據(jù)庫初始化");
//建表
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();
}
}
您可能感興趣的文章:
- Android SQLite數(shù)據(jù)庫增刪改查操作的使用詳解
- Android SQLite數(shù)據(jù)庫增刪改查操作的案例分析
- Android使用SQLite數(shù)據(jù)庫的簡(jiǎn)單實(shí)例
- android創(chuàng)建數(shù)據(jù)庫(SQLite)保存圖片示例
- android通過jxl讀excel存入sqlite3數(shù)據(jù)庫
- Android中的SQL查詢語句LIKE綁定參數(shù)問題解決辦法(sqlite數(shù)據(jù)庫)
- Android中操作SQLite數(shù)據(jù)庫快速入門教程
- Android操作存放在assets文件夾下SQLite數(shù)據(jù)庫的方法
- Android操作SQLite數(shù)據(jù)庫(增、刪、改、查、分頁等)及ListView顯示數(shù)據(jù)的方法詳解
- Android創(chuàng)建和使用數(shù)據(jù)庫SQLIte
相關(guān)文章
為Retrofit統(tǒng)一添加post請(qǐng)求的默認(rèn)參數(shù)的方法
這篇文章主要介紹了為Retrofit統(tǒng)一添加post請(qǐng)求的默認(rèn)參數(shù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
Android應(yīng)用中使用XmlSerializer序列化XML數(shù)據(jù)的教程
這篇文章主要介紹了Android應(yīng)用中使用XmlSerializer序列化XML數(shù)據(jù)的教程,XmlSerializer序列化XML同時(shí)也是將數(shù)據(jù)寫為XML格式的基本方法,需要的朋友可以參考下2016-04-04
Android Application級(jí)別自定義Toast
這篇文章主要為大家詳細(xì)介紹了Android Application級(jí)別自定義Toast,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Flutter Android端啟動(dòng)白屏問題的解決
Flutter 應(yīng)用在 Android 端上啟動(dòng)時(shí)會(huì)有一段很明顯的白屏現(xiàn)象,白屏的時(shí)長(zhǎng)由設(shè)備的性能決定,設(shè)備性能越差,白屏?xí)r間越長(zhǎng)。這篇文章主要介紹了Flutter Android端啟動(dòng)白屏問題的解決。感興趣的小伙伴們可以參考一下2018-07-07
Android開發(fā)實(shí)現(xiàn)Switch控件修改樣式功能示例【附源碼下載】
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)Switch控件修改樣式功能,涉及Android Switch開關(guān)控件樣式設(shè)置與事件響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
Android實(shí)現(xiàn)可以展開的TextView
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可以展開的TextView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Flutter版本的自定義短信驗(yàn)證碼實(shí)現(xiàn)示例解析
這篇文章主要介紹了Flutter版本的自定義短信驗(yàn)證碼實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

