Android SQLite数据库操作示例
SQLite介绍
Android开发中使用SQLite数据库
创建数据库
- 构造函数,调用父类SQLiteOpenHelper的构造函数。这个方法需要四个参数:上下文环境,数据库名字,一个可选的游标工厂(通常是NULL),一个代表你正在使用的数据库模型版本的整数。
- onCreate()方法,它需要一个SQLiteDatabase对象作为参数,根据需要对这个对象填充表和初始化数据。
- onUpgrade()方法,它需要三个参数,一个SQLiteDatabase对象,一个旧的版本号和一个新的版本号,这样你就可以清楚如何把一个数据库从旧的模型转变为新的模型。
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MyDBHelper extends SQLiteOpenHelper { private static final String COLUMN_ID = "_id"; public static final String TABLE_NAME = "category"; private static final String DATABASE_NAME = "category.db"; private static final int DATABASE_VERSION = 1; private static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + " fid TEXT, token TEXT, cid TEXT, cname TEXT)"; public CategoryDBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME); onCreate(db); } }
增删改查数据库
public class CategoryDBManager { private MyDBHelper myDBHelper; private static CategoryDBManager categoryDBManager = null; private CategoryDBManager(Context context) { myDBHelper = new MyDBHelper(context); } /** * 单例模式 */ public static CategoryDBManager getInstance(Context context) { if (categoryDBManager == null) { synchronized (CategoryDBManager.class) { if (categoryDBManager == null) { categoryDBManager = new CategoryDBManager(context); } } } return categoryDBManager; } public SQLiteDatabase getDB() { SQLiteDatabase db = myDBHelper.getWritableDatabase(); while (db.isDbLockedByCurrentThread()) { } return db; } public void insertLists(String token, String fid, List<PlateCategoryData> lists) { // 打开可写数据库 SQLiteDatabase db = getDB(); for (PlateCategoryData pd : lists) { // 执行SQL语句,替换占位符 db.execSQL("insert into " + MyDBHelper.TABLE_NAME + "(cid, cname, fid, token) values(?, ?, ?, ?)", new Object[] { pd.getId(), pd.getName(), fid, token }); } // 释放资源 db.close(); } public ArrayList<PlateCategoryData> getLists(String fid, String token) { ArrayList<PlateCategoryData> datas = new ArrayList<PlateCategoryData>(); SQLiteDatabase db = getDB(); // 执行原始查询,得到cursor String querySql = "select cid, cname from " + MyDBHelper.TABLE_NAME + " where fid = ? and token = ?"; Cursor cursor = db.rawQuery(querySql, new String[] { fid, token }); // 移动cursor到第一个数据(无数据返回false) if (cursor.moveToFirst()) { // while判断是否有下一条数据 do { PlateCategoryData pd = new PlateCategoryData(cursor.getString(cursor.getColumnIndex("cid")), cursor.getString(cursor.getColumnIndex("cname"))); datas.add(pd); } while (cursor.moveToNext()); } cursor.close(); db.close(); return datas; } public void updateLists(String fid, String token, ArrayList<PlateCategoryData> datas) { SQLiteDatabase db = getDB(); for (PlateCategoryData pd : datas) { String sql = "update " + MyDBHelper.TABLE_NAME + " set cid = ?, cname = ? where fid = ? and token = ?"; db.execSQL(sql, new Object[] { pd.getId(), pd.getName(), fid, token }); } db.close(); } public void deleteLists(String fid, String token) { SQLiteDatabase db = getDB(); db.execSQL("delete from " + MyDBHelper.TABLE_NAME + " where fid = ? and token = ?", new Object[] { fid, token }); db.close(); } public void closeDB() { SQLiteDatabase db = getDB(); if (db.isOpen()) { myDBHelper.close(); db.close(); } } }
参考链接
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。