第一步java
package db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DataBaseHelper extends SQLiteOpenHelper { public final static String DBBase_Name = "Local";// ----數據庫名稱 public final static int DataBase_Version = 1; public DataBaseHelper(Context context) { super(context, DBBase_Name, null, DataBase_Version); } @Override // 建立 數據庫表 public void onCreate(SQLiteDatabase myDB) { try { String strSql = "CREATE TABLE [image] (" + "[id] INTEGER NOT NULL PRIMARY KEY," + "[image] BLOB)"; myDB.execSQL(strSql); } catch (Exception e) { } } @Override public void onUpgrade(SQLiteDatabase myDB, int oldVersion, int newVersion) { String strSql = ""; switch (oldVersion) { case 2: try { } catch (Exception e) { // TODO: handle exception } break; default: break; } } }
第二步android
package db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; public class BaseDao { public static SQLiteDatabase gRSqliteDB = null; public static SQLiteDatabase gWSqliteDB = null; // -----獲取可讀寫數據庫 public static boolean getDataBase(Context context) { try { DataBaseHelper dbHelper = new DataBaseHelper(context); if (gRSqliteDB == null) { gRSqliteDB = dbHelper.getReadableDatabase(); } if (gWSqliteDB == null) { gWSqliteDB = dbHelper.getWritableDatabase(); } } catch (Exception e) { return false; } return true; } // ----數據庫關閉 public static void CloseDataBase() { if (gWSqliteDB != null) { gWSqliteDB.close(); } if (gRSqliteDB != null) { gRSqliteDB.close(); } } }
第三步sql
BaseDao.getDataBase(context); boolean b = insertTable(string); // 插入字符串語句 public boolean insertTable(String string) { Boolean bRet = false; try { ContentValues contentValues = new ContentValues(); contentValues.put("id", 1); contentValues.put("image", string); long iRet = baseDao.gWSqliteDB.insert("image", null, contentValues); bRet = iRet > 0; } catch (Exception e) { e.getMessage(); } return bRet; } // 查詢語句 public String selectTable(String id) { String s = null; String sql = "select image from image where id ='" + id + "'"; try { Cursor cursor = baseDao.gRSqliteDB.rawQuery(sql, null); while (cursor.moveToNext()) { s = cursor.getString(cursor.getColumnIndex("image")); } cursor.close(); } catch (Exception e) { } return s; }