課程
/移動開發(fā)
/Android
/Android基礎(chǔ)教程-SQLite高級操作
//我已經(jīng)有數(shù)據(jù)庫了,如何導(dǎo)入并查詢呢
2020-01-18
源自:Android基礎(chǔ)教程-SQLite高級操作 2-1
正在回答
操作方法:1. 把原數(shù)據(jù)庫包括在項目源碼的 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"
//保存的數(shù)據(jù)庫文件名
String PACKAGE_NAME =
"com.android.ImportDatabase"
String DB_PATH =
"/data"
????????????
+ Environment.getDataDirectory().getAbsolutePath() +
"/"
+ PACKAGE_NAME;?
//在手機里存放數(shù)據(jù)庫的位置
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())) {
//判斷數(shù)據(jù)庫文件是否存在,若不存在則執(zhí)行導(dǎo)入,否則直接打開數(shù)據(jù)庫
????????????????
InputStream is =
.context.getResources().openRawResource(
????????????????????????
R.raw.countries);
//欲導(dǎo)入的數(shù)據(jù)庫
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數(shù)據(jù)庫查詢和事務(wù)操作以及將數(shù)據(jù)分頁加載
1 回答導(dǎo)出數(shù)據(jù)庫
1 回答點擊創(chuàng)建數(shù)據(jù)庫并插入數(shù)據(jù)按鈕異常退出app
1 回答數(shù)據(jù)庫對象
1 回答創(chuàng)建數(shù)據(jù)庫
1 回答誰能告訴我如何獲取絕本地數(shù)據(jù)庫對路徑的代碼。
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2020-06-08
操作方法:1. 把原數(shù)據(jù)庫包括在項目源碼的 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"
;
//保存的數(shù)據(jù)庫文件名
????
public
?static
?final
?String PACKAGE_NAME =
"com.android.ImportDatabase"
;
????
public
?static
?final
?String DB_PATH =
"/data"
????????????
+ Environment.getDataDirectory().getAbsolutePath() +
"/"
????????????
+ PACKAGE_NAME;?
//在手機里存放數(shù)據(jù)庫的位置
?????
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())) {
//判斷數(shù)據(jù)庫文件是否存在,若不存在則執(zhí)行導(dǎo)入,否則直接打開數(shù)據(jù)庫
????????????????
InputStream is =
this
.context.getResources().openRawResource(
????????????????????????
R.raw.countries);
//欲導(dǎo)入的數(shù)據(jù)庫
????????????????
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
;
????
}