标签:android blog http java 使用 数据
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(); } } }
Android SQLite数据库操作示例,布布扣,bubuko.com
标签:android blog http java 使用 数据
原文地址:http://blog.csdn.net/wzy_1988/article/details/36189245