• 数据库操作


    1. [代码][Java]代码     
    public class Database {
     
    private static DbOpenHelper mDbHelper;
    private static final int DB_VERSION = 1;
    private static final String DB_NAME = "pdf.db";
     
    public static void close() {
    if(mDbHelper != null) {
    mDbHelper.close();
    mDbHelper = null;
    }
    }
     
    public static SQLiteDatabase getDb(Context context) {
    DbOpenHelper helper = getDbOpenHelper(context);
    return helper.getWritableDatabase();
    }
     
    public static void writeMessageToDb(SQLiteDatabase db, String name, 
    String path) {
    ContentValues values = new ContentValues();
    values.put(PDF.name.name(), name);
    values.put(PDF.path.name(), path);
    db.insert(PDF.TABLE_NAME, null, values);
    }
     
    public static HashMap<String, String> loadMessageFromDb(SQLiteDatabase db) {
    Cursor cursor = db.query(PDF.TABLE_NAME, null, null, null, null, null, null);
    HashMap<String, String> result = new HashMap<String, String>();
    while(cursor.moveToNext()) {http://www.huiyi8.com/moban/
    String name = cursor.getString(cursor.getColumnIndex(PDF.name.name()));
    String path = cursor.getString(cursor.getColumnIndex(PDF.path.name()));
     
     
    result.put(name, path);
    }
    cursor.close();
    return result;
    }
     
    private static DbOpenHelper getDbOpenHelper(Context context) {
    if(mDbHelper == null)
    mDbHelper = new DbOpenHelper(context, DB_NAME, DB_VERSION);
     
    return mDbHelper;
    }
     
    private static class DbOpenHelper extends SQLiteOpenHelper {
     
    public DbOpenHelper(Context context, String name, int version) {
    super(context, name, null, version);
    }
     
    @Override
    public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + PDF.TABLE_NAME + " (" +
    PDF._id.name() + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
    PDF.name.name() + " TEXT NOT NULL, " + 
    PDF.path.name() + " TEXT NOT NULL" +
    ");"
    );
    }
     
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    dropTables(db);html模板
    onCreate(db);
    }
     
    private void dropTables(SQLiteDatabase db) {
    db.execSQL("DROP TABLE IF EXISTS " + PDF.TABLE_NAME);
    }
    }
     
    enum PDF {
    _id,
    name,
    path;
     
    static final String TABLE_NAME = "PDF";
    }
    }
  • 相关阅读:
    UVa OJ 102 Ecological Bin Packing (生态学装箱问题)
    最小生成树 zoj1586 QS Network
    最小生成树&并查集 POJ 1861 Network
    最小生成树&并查集POJ 1287 Networking
    2012年亚洲长春区域赛:K Yukari's Birthday
    最小生成树 ZOJ 1203 Swordfish
    HDU 2639 Bone Collector II
    POJ2528 Mayor's posters(区间替换&&线段切割)
    POJ2777 Count Color(区间修改&&懒惰标记&&位运算)
    (转)有关如何入门ACM
  • 原文地址:https://www.cnblogs.com/xkzy/p/3806111.html
Copyright © 2020-2023  润新知