注意: 在尝试此代码之前,请在以下代码中找到此行:
private static String DB_NAME ="YourDbName"; // Database name
DB_NAME此处是你的数据库的名称。假设你在
Assets文件夹中有数据库的副本,因此,例如,如果你的数据库名称为
ordersDB,则
DB_NAME的值为
orderDB,
private static String DB_NAME ="ordersDB";
将数据库保留在资产文件夹中,然后执行以下 *** 作:
DataHelper类:
import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import android.content.Context;import android.database.SQLException;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;public class DatabaseHelper extends SQLiteOpenHelper { private static String TAG = "DatabaseHelper"; // Tag just for the LogCat window private static String DB_NAME ="YourDbName"; // Database name private static int DB_VERSION = 1; // Database version private final File DB_FILE; private SQLiteDatabase mDatabase; private final Context mContext; public DatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); DB_FILE = context.getDatabasePath(DB_NAME); this.mContext = context; } public void createDatabase() throws IOException { // If the database does not exist, copy it from the assets. boolean mDatabaseExist = checkDatabase(); if(!mDatabaseExist) { this.getReadableDatabase(); this.close(); try { // Copy the database from assests copyDatabase(); Log.e(TAG, "createDatabase database created"); } catch (IOException mIOException) { throw new Error("ErrorCopyingDatabase"); } } } // Check that the database file exists in databases folder private boolean checkDatabase() { return DB_FILE.exists(); } // Copy the database from assets private void copyDatabase() throws IOException { InputStream mInput = mContext.getAssets().open(DB_NAME); OutputStream mOutput = new FileOutputStream(DB_FILE); byte[] mBuffer = new byte[1024]; int mLength; while ((mLength = mInput.read(mBuffer)) > 0) { mOutput.write(mBuffer, 0, mLength); } mOutput.flush(); mOutput.close(); mInput.close(); } // Open the database, so we can query it public boolean openDatabase() throws SQLException { // Log.v("DB_PATH", DB_FILE.getAbsolutePath()); mDatabase = SQLiteDatabase.openDatabase(DB_FILE, null, SQLiteDatabase.CREATE_IF_NECESSARY); // mDatabase = SQLiteDatabase.openDatabase(DB_FILE, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); return mDatabase != null; } @Override public synchronized void close() { if(mDatabase != null) { mDatabase.close(); } super.close(); }}
编写一个DataAdapter类,例如:
import java.io.IOException;import android.content.Context;import android.database.Cursor;import android.database.SQLException;import android.database.sqlite.SQLiteDatabase;import android.util.Log;public class TestAdapter { protected static final String TAG = "DataAdapter"; private final Context mContext; private SQLiteDatabase mDb; private DatabaseHelper mDbHelper; public TestAdapter(Context context) { this.mContext = context; mDbHelper = new DatabaseHelper(mContext); } public TestAdapter createDatabase() throws SQLException { try { mDbHelper.createDatabase(); } catch (IOException mIOException) { Log.e(TAG, mIOException.toString() + " UnableToCreateDatabase"); throw new Error("UnableToCreateDatabase"); } return this; } public TestAdapter open() throws SQLException { try { mDbHelper.openDatabase(); mDbHelper.close(); mDb = mDbHelper.getReadableDatabase(); } catch (SQLException mSQLException) { Log.e(TAG, "open >>"+ mSQLException.toString()); throw mSQLException; } return this; } public void close() { mDbHelper.close(); } public Cursor getTestData() { try { String sql ="SELECt * FROM myTable"; Cursor mCur = mDb.rawQuery(sql, null); if (mCur != null) { mCur.moveTonext(); } return mCur; } catch (SQLException mSQLException) { Log.e(TAG, "getTestData >>"+ mSQLException.toString()); throw mSQLException; } }}
现在你可以像这样使用它:
TestAdapter mDbHelper = new TestAdapter(urContext);mDbHelper.createDatabase();mDbHelper.open();Cursor testdata = mDbHelper.getTestData();mDbHelper.close();
对于Android 4.1(Jelly Bean),更改:
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
至:
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
在DataHelper类中,此代码将在Jelly Bean 4.2多用户上运行。
编辑:代替使用硬编码路径,我们可以使用
DB_PATH = context.getDatabasePath(DB_NAME).getAbsolutePath();
这将为我们提供数据库文件的完整路径,并适用于所有Android版本
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)