小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

什么是一個(gè)完整的Android數(shù)據(jù)庫的Helper類為現(xiàn)有的SQLite數(shù)據(jù)庫?

 quasiceo 2015-03-22
我試圖部署應(yīng)用程序與現(xiàn)有的SQLite數(shù)據(jù)庫。 我已經(jīng)通過閱讀,并企圖幾個(gè)樣品在線但是我發(fā)現(xiàn),他們總是缺少的代碼,并可以做或工作作為標(biāo)榜。 有沒有人有一個(gè)完整的Android數(shù)據(jù)庫的Helper類部署在Android上現(xiàn)有的SQLite數(shù)據(jù)庫?

-------------------------------------------------------------------------------------------------------------------------
1. 這就是我了,希望它可以幫助別人認(rèn)為是有trouble。
package com.MyPackage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class AnyDBAdapter {
 private static final String TAG = "AnyDBAdapter";
 private DatabaseHelper mDbHelper;
 private static SQLiteDatabase mDb;
 //make sure this matches the 
 //package com.MyPackage;
 //at the top of this file
 private static String DB_PATH = "/data/data/com.MyPackage/databases/";
 //make sure this matches your database name in your assets folder
 // my database file does not have an extension on it 
 // if yours does
 // add the extention
 private static final String DATABASE_NAME = "data";
 //Im using an sqlite3 database, I have no clue if this makes a difference or not
 private static final int DATABASE_VERSION = 3;
 private final Context adapterContext;
 public AnyDBAdapter(Context context) {
  this.adapterContext = context;
 }
 public AnyDBAdapter open() throws SQLException {
  mDbHelper = new DatabaseHelper(adapterContext);
  try {
   mDbHelper.createDataBase();
  } catch (IOException ioe) {
   throw new Error("Unable to create database");
  }
  try {
   mDbHelper.openDataBase();
  } catch (SQLException sqle) {
   throw sqle;
  }
  return this;
 }
 //Usage from outside
 // AnyDBAdapter dba = new AnyDBAdapter(contextObject); //in my case contextObject is a Map
 // dba.open();
 // Cursor c = dba.ExampleSelect("Rawr!");
 // contextObject.startManagingCursor(c);
 // String s1 = "", s2 = "";
 // if(c.moveToFirst())
 // do {
 // s1 = c.getString(0);
 // s2 = c.getString(1);
 // } while (c.moveToNext());
 // dba.close();
 public Cursor ExampleSelect(string myVariable)
 {
  String query = "SELECT locale, ? FROM android_metadata";
  return mDb.rawQuery(query, new String[]{myVariable});
 }
 //Usage
 // AnyDBAdatper dba = new AnyDBAdapter(contextObjecT);
 // dba.open();
 // dba.ExampleCommand("en-CA", "en-GB");
 // dba.close();
 public void ExampleCommand(String myVariable1, String myVariable2)
 {
  String command = "INSERT INTO android_metadata (locale) SELECT ? UNION ALL SELECT ?";
  mDb.execSQL(command, new String[]{ myVariable1, myVariable2});
 }
 public void close() {
  mDbHelper.close();
 }
 private static class DatabaseHelper extends SQLiteOpenHelper {
  Context helperContext;
  DatabaseHelper(Context context) {
   super(context, DATABASE_NAME, null, DATABASE_VERSION);
   helperContext = context;
  }
  @Override
  public void onCreate(SQLiteDatabase db) {
  }
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   Log.w(TAG, "Upgrading database!!!!!");
   //db.execSQL("");
   onCreate(db);
  }
  public void createDataBase() throws IOException {
   boolean dbExist = checkDataBase();
   if (dbExist) {
   } else {
    //make sure your database has this table already created in it
    //this does not actually work here
    /*
     * db.execSQL("CREATE TABLE IF NOT EXISTS \"android_metadata\" (\"locale\" TEXT DEFAULT 'en_US')"
     * );
     * db.execSQL("INSERT INTO \"android_metadata\" VALUES ('en_US')"
     * );
     */
    this.getReadableDatabase();
    try {
     copyDataBase();
    } catch (IOException e) {
     throw new Error("Error copying database");
    }
   }
  }
  public SQLiteDatabase getDatabase() {
   String myPath = DB_PATH + DATABASE_NAME;
   return SQLiteDatabase.openDatabase(myPath, null,
     SQLiteDatabase.OPEN_READONLY);
  }
  private boolean checkDataBase() {
   SQLiteDatabase checkDB = null;
   try {
    String myPath = DB_PATH + DATABASE_NAME;
    checkDB = SQLiteDatabase.openDatabase(myPath, null,
      SQLiteDatabase.OPEN_READONLY);
   } catch (SQLiteException e) {
   }
   if (checkDB != null) {
    checkDB.close();
   }
   return checkDB != null ? true : false;
  }
  private void copyDataBase() throws IOException {
   // Open your local db as the input stream
   InputStream myInput = helperContext.getAssets().open(DATABASE_NAME);
   // Path to the just created empty db
   String outFileName = DB_PATH + DATABASE_NAME;
   // Open the empty db as the output stream
   OutputStream myOutput = new FileOutputStream(outFileName);
   // transfer bytes from the inputfile to the outputfile
   byte[] buffer = new byte[1024];
   int length;
   while ((length = myInput.read(buffer)) > 0) {
    myOutput.write(buffer, 0, length);
   }
   // Close the streams
   myOutput.flush();
   myOutput.close();
   myInput.close();
  }
  public void openDataBase() throws SQLException {
   // Open the database
   String myPath = DB_PATH + DATABASE_NAME;
   mDb = SQLiteDatabase.openDatabase(myPath, null,
     SQLiteDatabase.OPEN_READWRITE);
  }
  @Override
  public synchronized void close() {
   if (mDb != null)
    mDb.close();
   super.close();
  }
 }
}

2.
DatabaseHelper dbHelper = new DatabaseHelper(getApplicationContext());
請(qǐng)確保您的應(yīng)用程序,它在創(chuàng)建DatabaseHelper對(duì)象一次。 讀取數(shù)據(jù):
SQLiteDatabase db = dbHelper.getReadableDatabase();
對(duì)于讀/修改數(shù)據(jù):
SQLiteDatabase db = dbHelper.getWritableDatabase();
insert()query(),update(),delete()SQLiteDatabase對(duì)象的方法: 你也不要通過直接accesssing sqlite的文件,你在做創(chuàng)建數(shù)據(jù)庫 在你的onCreate(...)方法使用SQLiteDatabase對(duì)象的execSQL()方法。執(zhí)行你的CREATE TABLE查詢那里。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多