将现有的SQLite数据库放置并读取

3
在Android文件夹结构中,现有的SQLite数据库应该放在哪里?是drawable文件夹还是layout文件夹?
我没有找到任何解决方案。
将不胜感激任何帮助。
5个回答

3

你应该将它放在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);

}

当你通过我提到的函数将数据库文件复制到你想要的目录中时,你可以像打开和读取普通存在的数据库文件一样,打开和读取该数据库。 - undefined

1
作为名称应说明的,drawablelayout不适用于数据库。如果您想要在应用中分发预设数据库,则放置在哪里并不重要(除了当您尝试将其放置在drawablelayout中时,您将无法构建您的应用程序)。最合理的地方是assets文件夹。还有一个非常好的助手可以帮助您设置这样的数据库以供应用程序使用:https://github.com/jgilfelt/android-sqlite-asset-helper

0
个人而言,我将sqllite数据库放在assets文件夹中。 要使用它,你可以将其复制到"/data/data/your.application.package.name/databases/"目录下。

0

路径:data -> data -> com.yourcompany.yourappid -> 数据库 或者您可以使用Astro文件管理器应用程序在您的设备上找到数据库并浏览设备的文件夹结构。

以下代码演示如何定位您设备上的SQL数据库文件:

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' );
}

谢谢,但是你没有理解我的问题。我已经在SQLite管理器中创建了一个数据库,并希望将其与我的应用程序捆绑在一起。那么,我应该把它放在哪里,以便在我的活动中使用它? - undefined

0

你应该将外部数据库文件放在 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();
    }
}}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接