放置现有的sqlite数据库并阅读

时间:2013-08-09 09:29:12

标签: android database

在android文件夹结构中将现有的sqllite数据库放在哪里?它是可绘制文件夹还是布局文件夹?

我找不到任何解决办法。

任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:2)

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

}

答案 1 :(得分:1)

由于名称应指明drawablelayout不适用于数据库。如果您想使用自己的应用分发预先设定的数据库,那么它与您放置它的位置无关(除了当您尝试将其放入drawablelayout时,您将不会能够构建您的应用程序)。最理智的地方是assets文件夹。还有一个非常好的帮手可以帮助您设置这样的数据库以便与应用程序一起使用:https://github.com/jgilfelt/android-sqlite-asset-helper

答案 2 :(得分:0)

路径:数据 - >数据 - > 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' );
}

答案 3 :(得分:0)

我个人将sqlite数据库放在assets文件夹中。 为了使用它,您可以将其复制到“/data/data/your.application.package.name/databases /

答案 4 :(得分:0)

您应将外部数据库文件放在资产文件夹中:

然后为它制作一个Class。

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