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);
评论0
最新资源