新聞中心
在開發(fā)Android應(yīng)用程序時(shí),經(jīng)常需要通過數(shù)據(jù)庫來存儲(chǔ)和管理數(shù)據(jù)。而在開發(fā)過程中,我們可以使用SQLite數(shù)據(jù)庫來創(chuàng)建和管理數(shù)據(jù)庫。而有時(shí)候我們需要導(dǎo)入一個(gè)已經(jīng)存在的數(shù)據(jù)庫文件進(jìn)行使用,那么本文將會(huì)介紹如何在Android平臺(tái)上導(dǎo)入一個(gè)已經(jīng)存在的數(shù)據(jù)庫文件。

創(chuàng)新互聯(lián)是一家專業(yè)提供咸豐企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為咸豐眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
1.我們需要將我們要導(dǎo)入的數(shù)據(jù)庫文件拷貝到 Android 工程的 assets 目錄下。如果沒有 assets 目錄,可以在 app 下新建一個(gè)名為 assets 的文件夾即可。
2.接下來,在我們的工程中創(chuàng)建 SQLiteOpenHelper 的一個(gè)子類,用于打開現(xiàn)有的數(shù)據(jù)庫文件。這個(gè)類的大致代碼如下所示:
public class MyDatabase extends SQLiteOpenHelper {
private static final String DATABASE_NAME = “mydatabase.db”;
private static final int DATABASE_VERSION = 1;
private final Context mContext;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}
public void createDatabase() throws IOException {
boolean dbExist = checkDataBase();
if (!dbExist) {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error(“Error copying database.”);
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String dbPath = mContext.getDatabasePath(DATABASE_NAME).getPath();
checkDB = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READON);
} catch (SQLiteException e) {
//database does not exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = mContext.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = mContext.getDatabasePath(DATABASE_NAME).getPath();
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// do nothing
}
@Override
public void onCreate(SQLiteDatabase db) {
// do nothing
}
}
3.在我們的MnActivity中,實(shí)例化 MyDatabase 類,并且調(diào)用 createDatabase() 方法來拷貝并打開數(shù)據(jù)庫。如下所示:
public class MnActivity extends AppCompatActivity {
private MyDatabase csvDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mn);
csvDatabase = new MyDatabase(this);
try {
csvDatabase.createDatabase();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.導(dǎo)入成功后,我們可以使用 SQLiteDatabase 對(duì)象連接到數(shù)據(jù)庫,以執(zhí)行期望的操作。如下所示,我們可以讀取導(dǎo)入的數(shù)據(jù)庫中的數(shù)據(jù),然后將其顯示在 ListView 上:
private void loadFromDatabase() {
SQLiteDatabase db = csvDatabase.getReadableDatabase();
List dataList = new ArrayList();
String query = “SELECT * FROM mytable”;
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(1);
String eml = cursor.getString(2);
String phone = cursor.getString(3);
dataList.add(name + “, ” + eml + “, ” + phone);
} while (cursor.moveToNext());
cursor.close();
db.close();
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, android.R.id.text1, dataList);
lstData.setAdapter(adapter);
}
}
相關(guān)問題拓展閱讀:
- android怎么將.sql格式的文件導(dǎo)入到Sqlite去
android怎么將.sql格式的文件導(dǎo)入到Sqlite去
有人說用sqlite3
sqlite3 mydb.db
>螞滲饑.read dd.sql
mydb.db 就是我的android的數(shù)據(jù)庫,數(shù)據(jù)表結(jié)構(gòu)就是(,)這樣的
dd.sql 就是外部數(shù)據(jù)庫
dd.sql里面的打開就是 Insert Into (,) Values(“001″,”David”);
請(qǐng)問這樣可以導(dǎo)入么..或者有什么別的方法可以導(dǎo)入?
—–參考解決悶返方法
關(guān)于android 導(dǎo)入數(shù)據(jù)庫文件的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
創(chuàng)新互聯(lián)成都網(wǎng)站建設(shè)公司提供專業(yè)的建站服務(wù),為您量身定制,歡迎來電(028-86922220)為您打造專屬于企業(yè)本身的網(wǎng)絡(luò)品牌形象。
成都創(chuàng)新互聯(lián)品牌官網(wǎng)提供專業(yè)的網(wǎng)站建設(shè)、設(shè)計(jì)、制作等服務(wù),是一家以網(wǎng)站建設(shè)為主要業(yè)務(wù)的公司,在網(wǎng)站建設(shè)、設(shè)計(jì)和制作領(lǐng)域具有豐富的經(jīng)驗(yàn)。
網(wǎng)頁題目:Android如何導(dǎo)入數(shù)據(jù)庫文件(android導(dǎo)入數(shù)據(jù)庫文件)
分享路徑:http://fisionsoft.com.cn/article/djojjgc.html


咨詢
建站咨詢
