在 Android 应用程序中导入外部的 SQLite 数据库涉及以下几个步骤:
-
准备数据库文件:
- 确保外部 SQLite 数据库文件已经创建并正确格式化。
- 将数据库文件放置在项目的
assets
目录中,这样可以方便地从应用程序中访问它。
-
创建 DatabaseHelper 类:
- 该类将负责将数据库从
assets
复制到应用程序的内存中,并管理数据库的打开和关闭操作。
- 该类将负责将数据库从
以下是一个示例代码:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_NAME = "your_database.db"; // 数据库名称
private static String DB_PATH = "";
private static final int DB_VERSION = 1;
private SQLiteDatabase mDataBase;
private final Context mContext;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.mContext = context;
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (!dbExist) {
this.getReadableDatabase();
this.close();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// Database doesn't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null;
}
private void copyDataBase() throws IOException {
InputStream myInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLiteException {
String myPath = DB_PATH + DB_NAME;
mDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@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) {
}
}
-
使用 DatabaseHelper 类:
- 在 Activity 或其他组件中使用 DatabaseHelper 类来创建和打开数据库。
public class MainActivity extends AppCompatActivity {
private DatabaseHelper mDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDBHelper = new DatabaseHelper(this);
try {
mDBHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
mDBHelper.openDataBase();
} catch (SQLiteException sqle) {
throw sqle;
}
// Now you can use the database
SQLiteDatabase db = mDBHelper.getReadableDatabase();
}
}
以上步骤完成后,你的应用程序将能够访问并使用外部的 SQLite 数据库文件。确保在 AndroidManifest.xml
文件中添加了必要的权限,例如读取和写入存储的权限。
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/189556.html