簡體   English   中英

放置現有的sqlite數據庫並閱讀

[英]placing existing sqlite database and reading

在android文件夾結構中放置現有sqllite數據庫的位置? 是可繪制文件夾還是布局文件夾?

我沒有找到任何解決方案。

任何幫助將不勝感激。

你應該把它放在assets文件夾中。 這樣你就可以確保它會附加到你的apk上。 這是您可以將數據庫文件從assets文件夾復制到工作目錄的方法:

private void copyDataBase() throws IOException{

//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);

// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;

//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();

}

現在從目錄中讀取數據庫:

 public void openDataBase() throws SQLException{

//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}

因為名稱應該表明drawablelayout不適用於數據庫。 如果你想用你的應用程序分發預先設置的數據庫,那么你把它放在哪里是不相關的(除了你試圖把它放在drawablelayout你將無法構建你的應用程序)。 最理智的地方是assets文件夾。 並且有一個非常好的幫助器可以幫助您設置這樣的數據庫以便與應用程序一起使用: https//github.com/jgilfelt/android-sqlite-asset-helper

路徑:數據 - >數據 - > com.yourcompany.yourappid - >數據庫或您可以使用Astro File Manager應用程序在設備上查找數據庫並瀏覽設備文件夾結構。

此代碼介紹如何在您的設備上找到sql的DB文件:

var dbName = 'dbData';
var dbPath;
var dbFile;
if ( Ti.Platform.osname == 'android' ) {
    dbPath = 'file:///data/data/' + Ti.App.getID() + '/databases/';
    dbFile = Ti.Filesystem.getFile( dbPath + dbName ); 
}
else {
    dbPath = Ti.Filesystem.applicationSupportDirectory + '/database/';
    dbFile = Ti.Filesystem.getFile( dbPath + dbName + '.sql' );
}

Personaly我將sqllite數據庫放在assets文件夾中。 要使用它,您可以將其復制到“/data/data/your.application.package.name/databases/

您應該將外部數據庫文件放在assets文件夾中:

然后為它制作一個類。

package com.appgiudeextra.Database;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.appguideextra.Items.MasterItem;

public class DBConnect extends SQLiteOpenHelper {
 public int GetCursor;
// ****************** Declare all the global variable
// ****************************//
 private Context myContext;
 public String DB_PATH = "data/data/com.appguideextra/databases/"; // path
// of
// your
// datbase
 public static String DB_NAME = "AppGuide.sqlite";// your database name
 static String ASSETS_DB_FOLDER = "db";
 private SQLiteDatabase db;

 public DBConnect(Context context, String db_name) {
    super(context, db_name, null, 2);
    if (db != null && db.isOpen())
        close();

    this.myContext = context;
    DB_NAME = db_name;

    try {
        createDataBase();
        openDataBase();
    } catch (IOException e) {
        // System.out.println("Exception in creation of database : "+
        // e.getMessage());
        e.printStackTrace();
    }

}

public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();

    if (dbExist) {
        // System.out.println("Database Exist");
    } else {
        this.getReadableDatabase();

        try {
            copyDatabase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

private void copyDatabase() throws IOException {
    InputStream input = myContext.getAssets().open(DB_NAME);
    String outputFileName = DB_PATH + DB_NAME;
    OutputStream output = new FileOutputStream(outputFileName);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = input.read(buffer)) > 0) {
        output.write(buffer, 0, length);
    }

    // Close the streams
    output.flush();
    output.close();
    input.close();
    // System.out.println(DB_NAME + "Database Copied !");
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void openDataBase() throws SQLException {
    // Open the database
    String myPath = DB_PATH + DB_NAME;
    db = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);
}

public boolean isOpen() {
    if (db != null)
        return db.isOpen();
    return false;
}

@Override
public synchronized void close() {
    if (db != null)
        db.close();
    super.close();
}

private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        // System.out.println("My Pathe is:- " + myPath);
        // System.out.println("Open");
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
        // System.out.println("checkDB value:" + checkDB);
        // System.out.println("My Pathe is:- " + myPath);
    } catch (Exception e) {
        // database does't exist yet.
    }

    if (checkDB != null) {
        // System.out.println("Closed");
        checkDB.close();
        // System.out.println("My db is:- " + checkDB.isOpen());
    }

    return checkDB != null ? true : false;
}

public Cursor execCursorQuery(String sql) {
    Cursor cursor = null;
    try {
        cursor = db.rawQuery(sql, null);
        GetCursor = cursor.getCount();
        Log.i("Inside execCursorQuery try", sql);
    } catch (Exception e) {
        Log.i("Inside execCursorQuery exception", e.getMessage());
    }
    return cursor;
}

public void execNonQuery(String sql) {
    try {
        db.execSQL(sql);
        // Log.d("SQL", sql);
    } catch (Exception e) {
        // Log.e("Err", e.getMessage());
    } finally {
        // closeDb();
    }
}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM