如何使用Asset文件夹中的SQLite连接
发布时间:2020-12-12 18:56:15 所属栏目:百科 来源:网络整理
导读:如何使用Asset文件夹中的SQLite连接. 解决方法 这称为DataBaseHelper. java文件 public class DataBaseHelper extends SQLiteOpenHelper{private static String TAG = "TAG";private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";private
如何使用Asset文件夹中的SQLite连接.
解决方法这称为DataBaseHelper. java文件public class DataBaseHelper extends SQLiteOpenHelper { private static String TAG = "TAG"; private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/"; private static String DB_NAME = "ServiceInfo.sqlite"; private SQLiteDatabase mDataBase; private final Context mContext; public DataBaseHelper(Context context) { super(context,DB_NAME,null,1); DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; this.mContext = context; } public void createDataBase() throws IOException { boolean mDataBaseExist = checkDataBase(); if(!mDataBaseExist) { this.getReadableDatabase(); try { copyDataBase(); } catch (IOException mIOException) { throw new Error("ErrorCopyingDataBase"); } } } private boolean checkDataBase() { SQLiteDatabase mCheckDataBase = null; try { String myPath = DB_PATH + DB_NAME; mCheckDataBase = SQLiteDatabase.openDatabase(myPath,SQLiteDatabase.NO_LOCALIZED_COLLATORS); } catch(SQLiteException mSQLiteException) { Log.e(TAG,"DatabaseNotFound " + mSQLiteException.toString()); } if(mCheckDataBase != null) { mCheckDataBase.close(); } return mCheckDataBase != null; } private void copyDataBase() throws IOException { InputStream mInput = mContext.getAssets().open(DB_NAME); String outFileName = DB_PATH + DB_NAME; OutputStream mOutput = new FileOutputStream(outFileName); byte[] mBuffer = new byte[1024]; int mLength; while ((mLength = mInput.read(mBuffer))>0) { mOutput.write(mBuffer,mLength); } mOutput.flush(); mOutput.close(); mInput.close(); } public boolean openDataBase() throws SQLException { String mPath = DB_PATH + DB_NAME; mDataBase = SQLiteDatabase.openDatabase(mPath,SQLiteDatabase.NO_LOCALIZED_COLLATORS); return mDataBase != null; } @Override public synchronized void close() { if(mDataBase != null) mDataBase.close(); super.close(); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) { Log.v(TAG,"UpgradingDatabase,This will drop current database and will recreate it"); } } 现在AdapetrClass.java public class KamaDBAdapter { protected static final String TAG = "TAG"; private final Context mContext; private SQLiteDatabase mDb; private DataBaseHelper mDbHelper; private static String ACCOUNT_TABLE = "account"; public static String ACCOUNT_EXTRADATA = "extraData"; public static String ACCOUNT_ID = "ID"; public static String ACCOUNT_ADDITIONALDATA = "additionalData"; public static String ACCOUNT_DATA = "data"; public KamaDBAdapter(Context context) { this.mContext = context; mDbHelper = new DataBaseHelper(mContext); } public KamaDBAdapter createDatabase() throws SQLException { try { mDbHelper.createDataBase(); } catch (IOException mIOException) { Log.e(TAG,mIOException.toString() + " UnableToCreateDatabase"); throw new Error("UnableToCreateDatabase"); } return this; } public KamaDBAdapter open() throws SQLException { try { mDbHelper.openDataBase(); mDbHelper.close(); mDb = mDbHelper.getReadableDatabase(); } catch (SQLException mSQLException) { Log.e(TAG,mSQLException.toString()); throw mSQLException; } return this; } public void close() { mDbHelper.close(); } public int countAccountData() { Cursor mCoursor = mDb.query(ACCOUNT_TABLE,new String[] {},null); int mReturnedCount = mCoursor.getCount(); mCoursor.close(); return mReturnedCount; } public long insertData(String mExtra,String mAdditionalData,String mData) { ContentValues initialValues = new ContentValues(); initialValues.put(ACCOUNT_EXTRADATA,mExtra); initialValues.put(ACCOUNT_ADDITIONALDATA,mAdditionalData); initialValues.put(ACCOUNT_DATA,mData); return mDb.insert(ACCOUNT_TABLE,initialValues); } public boolean updateData(int mPosition,String mExtra,mData); return mDb.update(ACCOUNT_TABLE,initialValues,"ID=" + mPosition,null) > 0; } public String retriveData(int mPosition) { Cursor mCursor = mDb.query(ACCOUNT_TABLE,new String[] {ACCOUNT_DATA},null); mCursor.moveToFirst(); String mReturn = mCursor.getString(mCursor.getColumnIndex(ACCOUNT_DATA)); mCursor.close(); return mReturn; } public String retriveAdditionalData(int mPosition) { Cursor mCursor = mDb.query(ACCOUNT_TABLE,new String[] {ACCOUNT_ADDITIONALDATA},null); mCursor.moveToFirst(); String mReturn = mCursor.getString(mCursor.getColumnIndex(ACCOUNT_ADDITIONALDATA)); mCursor.close(); return mReturn; } public boolean deleteAccount(int mPosition) { return mDb.delete(ACCOUNT_TABLE,ACCOUNT_ID + "=" + mPosition,null) > 0; } } 现在在你的主类中调用这个类:这样的东西: private static KamaDBAdapter mDbHelper; mDbHelper = new KamaDBAdapter(Usage.this); mDbHelper.createDatabase(); 现在您的数据库已复制到设备.您可以从本地设备访问您在Asset文件夹中放置的相同数据库. 希望它可以帮到你. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |