課程
/移動開發
/Android
/Android基礎教程-SQLite高級操作
//我已經有數據庫了,如何導入并查詢呢
2020-01-18
源自:Android基礎教程-SQLite高級操作 2-1
正在回答
操作方法:1. 把原數據庫包括在項目源碼的 res/raw 目錄下,然后建立一個DBManager類,代碼如下:
package
com.android.ImportDatabase;
import
java.io.File;
java.io.FileNotFoundException;
java.io.FileOutputStream;
java.io.IOException;
java.io.InputStream;
android.content.Context;
android.database.sqlite.SQLiteDatabase;
android.os.Environment;
android.util.Log;
public
class
DBManager {
????
private
final
int
BUFFER_SIZE =
400000
;
static
String DB_NAME =
"countries.db"
//保存的數據庫文件名
String PACKAGE_NAME =
"com.android.ImportDatabase"
String DB_PATH =
"/data"
????????????
+ Environment.getDataDirectory().getAbsolutePath() +
"/"
+ PACKAGE_NAME;?
//在手機里存放數據庫的位置
SQLiteDatabase database;
Context context;
DBManager(Context context) {
????????
this
.context = context;
}
void
openDatabase() {
.database =
.openDatabase(DB_PATH +
+ DB_NAME);
SQLiteDatabase openDatabase(String dbfile) {
try
{
if
(!(
new
File(dbfile).exists())) {
//判斷數據庫文件是否存在,若不存在則執行導入,否則直接打開數據庫
????????????????
InputStream is =
.context.getResources().openRawResource(
????????????????????????
R.raw.countries);
//欲導入的數據庫
FileOutputStream fos =
FileOutputStream(dbfile);
byte
[] buffer =
[BUFFER_SIZE];
count =
0
while
((count = is.read(buffer)) >
) {
????????????????????
fos.write(buffer,
, count);
fos.close();
is.close();
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
null
);
return
db;
catch
(FileNotFoundException e) {
Log.e(
"Database"
,
"File not found"
e.printStackTrace();
(IOException e) {
"IO exception"
舉報
本視頻教程講解Sqlite數據庫查詢和事務操作以及將數據分頁加載
1 回答導出數據庫
1 回答創建數據庫
1 回答點擊創建數據庫并插入數據按鈕異常退出app
1 回答數據庫對象
1 回答誰能告訴我如何獲取絕本地數據庫對路徑的代碼。
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2020-06-08
操作方法:1. 把原數據庫包括在項目源碼的 res/raw 目錄下,然后建立一個DBManager類,代碼如下:
package
?com.android.ImportDatabase;
?import
?java.io.File;
import
?java.io.FileNotFoundException;
import
?java.io.FileOutputStream;
import
?java.io.IOException;
import
?java.io.InputStream;
?import
?android.content.Context;
import
?android.database.sqlite.SQLiteDatabase;
import
?android.os.Environment;
import
?android.util.Log;
?public
?class
?DBManager {
????
private
?final
?int
?BUFFER_SIZE =
400000
;
????
public
?static
?final
?String DB_NAME =
"countries.db"
;
//保存的數據庫文件名
????
public
?static
?final
?String PACKAGE_NAME =
"com.android.ImportDatabase"
;
????
public
?static
?final
?String DB_PATH =
"/data"
????????????
+ Environment.getDataDirectory().getAbsolutePath() +
"/"
????????????
+ PACKAGE_NAME;?
//在手機里存放數據庫的位置
?????
private
?SQLiteDatabase database;
????
private
?Context context;
?????
DBManager(Context context) {
????????
this
.context = context;
????
}
?????
public
?void
?openDatabase() {
????????
this
.database =
this
.openDatabase(DB_PATH +
"/"
?+ DB_NAME);
????
}
?????
private
?SQLiteDatabase openDatabase(String dbfile) {
????????
try
?{
????????????
if
?(!(
new
?File(dbfile).exists())) {
//判斷數據庫文件是否存在,若不存在則執行導入,否則直接打開數據庫
????????????????
InputStream is =
this
.context.getResources().openRawResource(
????????????????????????
R.raw.countries);
//欲導入的數據庫
????????????????
FileOutputStream fos =
new
?FileOutputStream(dbfile);
????????????????
byte
[] buffer =
new
?byte
[BUFFER_SIZE];
????????????????
int
?count =
0
;
????????????????
while
?((count = is.read(buffer)) >
0
) {
????????????????????
fos.write(buffer,
0
, count);
????????????????
}
????????????????
fos.close();
????????????????
is.close();
????????????
}
????????????
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
????????????????????
null
);
????????????
return
?db;
????????
}
catch
?(FileNotFoundException e) {
????????????
Log.e(
"Database"
,
"File not found"
);
????????????
e.printStackTrace();
????????
}
catch
?(IOException e) {
????????????
Log.e(
"Database"
,
"IO exception"
);
????????????
e.printStackTrace();
????????
}
????????
return
?null
;
????
}