从资产文件夹中读取sqlite文件

时间:2013-12-31 12:55:27

标签: android

我正在开发一个应用程序,我想从项目的资产文件夹中读取sqlite数据库文件。我搜索过网络但没有找到任何帮助。请帮我。 感谢。

3 个答案:

答案 0 :(得分:11)

您无法直接打开资源文件夹中的文件。相反,您需要将assets文件夹的sqlite数据库复制到内部/外部存储中,然后使用文件路径打开该文件。尝试下面的代码从assests中读取sqlite数据库并将其复制到sdcard中以使用它。

public class DataBaseHelper extends SQLiteOpenHelper {
   private Context mycontext;
   private static String DB_NAME = "(datbasename).sqlite";
   private static String DB_PATH ="/data/data/"+BuildConfig.APPLICATION_ID+"/databases/";
   public SQLiteDatabase myDataBase;

public DataBaseHelper(Context context) throws IOException {
    super(context,DB_NAME,null,1);
    this.mycontext=context;
    boolean dbexist = checkdatabase();
    if (dbexist) {
        System.out.println("Database exists");
        opendatabase(); 
    } else {
        System.out.println("Database doesn't exist");
        createdatabase();
    }
}

public void createdatabase() throws IOException {
    boolean dbexist = checkdatabase();
    if(dbexist) {
        System.out.println(" Database exists.");
    } else {
        this.getReadableDatabase();
        try {
            copydatabase();
        } catch(IOException e) {
            throw new Error("Error copying database");
        }
    }
}   

private boolean checkdatabase() {

    boolean checkdb = false;
    try {
        String myPath = DB_PATH + DB_NAME;
        File dbfile = new File(myPath);
        checkdb = dbfile.exists();
    } catch(SQLiteException e) {
        System.out.println("Database doesn't exist");
    }
    return checkdb;
}

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 byte to inputfile to 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_READWRITE);
}

public synchronized void close() {
    if(myDataBase != null) {
        myDataBase.close();
    }
    super.close();
}
}

答案 1 :(得分:10)

use SQLiteAssetHelper,其中包含首次运行应用时安装预打包数据库所需的所有代码。

答案 2 :(得分:2)

基本上你可以从文件名中获取一个InputStream(来自AssetManager打开函数)并将其写入OutputStream。

    $('.passengerAge').filter(function (req1) {

    var valbebe = parseInt($(this).val(), 10) <= 2;
         
    return valbebe;
          
    });
    $('.passengerAge').filter(function (req2) {
    var valadulte = parseInt($(this).val(), 10) >= 18;
         
    return valadulte;
          
    });

    $('.passengerAge').filter(function (req3) {

    var valenfant = parseInt($(this).val(), 10) < 18 && parseInt($(this).val(), 10) > 2 ;
         
    return valenfant;

如果使用openOrCreateDatabase创建数据库,您可能希望将数据库放在数据库文件夹中......

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select class="passengerAge" id="select_1" name="select_1">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    </select>