繁体   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