我正在尝试从assets
文件夹访问数据库
我使用了以下代码。
我可以从assets
读取数据库,但在我检查Db中的表时,它只显示android_metadata
和sqlite_sequence
表。
我该怎么做才能解决这个问题?
我正在使用getImageData()
方法从数据库中读取表。
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class DataBaseHelper extends SQLiteOpenHelper {
public static String DB_PATH = "/data/data/********/databases/";
private Context mycontext;
private static String DB_NAME = "STATE_DB";
public SQLiteDatabase myDataBase;
public DataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
this.mycontext=context;
boolean dbexist = checkdatabase();
if (dbexist) {
System.out.println("Database exists");
opendatabase();
} else {
System.out.println("Database doesn't exist");
try {
createdatabase();
}catch (IOException e){
}
}
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public 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;
}
public void createdatabase() throws IOException {
this.getReadableDatabase();
try {
copydatabase();
} catch(IOException e) {
throw new Error("Error copying database");
}
}
public void opendatabase() throws SQLException {
//Open the database
String mypath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
}
public 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("/data/data/com.medical.trofii/databases/STATE_DB");
// 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 ArrayList<String> getImageData() {
//Cursor cursor = db.rawQuery("SELECT * FROM satateTable WHERE countryId = '21'", null);
// imageData = cursor.getString(cursor.getColumnIndex("name"));
// countries.add(imageData);
List<String> tables = new ArrayList<String>();
ArrayList<String> countries = new ArrayList<String>();
String imageData = null;
try{
SQLiteDatabase db = this.getReadableDatabase();
db.isOpen();
Cursor cursor = db.rawQuery("SELECT * FROM sqlite_master WHERE type='table';", null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String tableName = cursor.getString(1);
if (!tableName.equals("android_metadata") && !tableName.equals("sqlite_sequence"))
tables.add(tableName);
cursor.moveToNext();
} while (cursor.moveToNext());
for(String tableName:tables) {
String d = "";
}
}
}}catch (Exception e){
}
return countries;
}
}
答案 0 :(得分:0)
您的目标路径需要数据库名称。这个想法只是
将其粘贴到目的地路径
try {
String destinationPath = "/data/data/" + getPackageName()
+ "/databases/STATE_DB.db";
File f = new File(destPath);
if(!f.exists()){
Log.v(TAG,"File Not Exist");
InputStream in = getAssets().open("STATE_DB.db");
OutputStream out = new FileOutputStream(destinationPath );
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Log.v("TAG","ioexeption");
e.printStackTrace();
}
答案 1 :(得分:0)
只需更改此
即可public static String DB_PATH = "/data/data/********/databases/";
到
public static String DB_PATH = "/data/data/com.medical.trofii/databases/";
和这个
OutputStream myoutput = new FileOutputStream("/data/data/com.medical.trofii/databases/STATE_DB");
到
OutputStream myoutput = new FileOutputStream(outfilename);