類似這樣
然後根據你要存的資料表去呼叫
A Layout 要存到A表
就呼叫相關的Create、Insert....等的副程式
- public class DbHleper extends SQLiteOpenHelper {
- private static final String DATABASE_NAME = "ADB";
- private final static int DATABASE_VERSION = 1;
- public DbUtil(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db)
- {
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- String sql = "drop table is exists AInfo";
- db.execSQL(sql);
- onCreate(db);
- }
- // ************** A methods ***********************
- public void createAInfo() {
-
- }
- public void dropAInfo() {
-
- }
- public void deleteAInfo() {
-
- }
- public void deleteAInfoOfAId(int id) {
- SQLiteDatabase db = this.getWritableDatabase();
- String where = "KeyId = ?";
- String[] whereValue = { Integer.toString(id) };
- db.delete("AInfo", where, whereValue);
-
- }
- public String insertAInfo(String A1,String A2, String A3,String A4, String A5) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues contentValue = new ContentValues();
-
- contentValue.put("A1", A1);
- contentValue.put("A2", A2);
- contentValue.put("A3", A3);
- contentValue.put("A4", A4);
- contentValue.put("A5", A5);
- try {
- long rowIndex = db.insert("AInfo", null, contentValue);
- return String.valueOf(rowIndex);
- } catch (Exception e) {
- return e.toString();
- }
- }
- public Cursor selectAInfo() {
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db
- .query("AInfo", null, null, null, null, null, null);
- return cursor;
- }
- // **************End A methods ***********************
- // ************** B methods ***********************
-
- public void createBInfo() {
- try {
- SQLiteDatabase db = this.getWritableDatabase();
- String sql = "create table if not exists BInfo (B1 varchar(32),B2 varchar(32),B3 varchar(32),B4 varchar(32))";
- db.execSQL(sql);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void dropBInfoTable() {
-
- }
- public String insertBInfo(String B1,String B2, String B3,String B4) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues contentValue = new ContentValues();
- contentValue.put("B1", B1);
- contentValue.put("B2", B2);
- contentValue.put("B3", B3);
- contentValue.put("B4", B4);
- try {
- long rowIndex = db.insert("BInfo", null, contentValue);
- return String.valueOf(rowIndex);
- } catch (Exception e) {
- return e.toString();
- }
- }
-
- public String selectBInfo_B1 (String B2){
- SQLiteDatabase db = this.getWritableDatabase();
- String BInfo_B1 ="";
- if(B2.length()>0)
- {
- Cursor cursor = db.query("BInfo", new String[]{"B1"}, " B2=?",new String[]{BName}, null, null, null);
- if(cursor.getCount()>0)
- {
- cursor.moveToFirst();
- BInfo_B1 =cursor.getString(cursor.getColumnIndex("B1"));
- }
- }
-
- return BInfo_B1;
- }
- // **************End B methods ***********************
-
- }
複製代碼 |