我已经尝试了很多关于如何在android项目中显示资产文件夹中的数据的代码,但我无法做到这一点。请帮我解决这个问题。
感谢您的帮助
编辑: -
我的适配器类
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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 android.util.Log;
public class DBAdapter extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/com.example.mynwedb/databases/wbty";
private static String DB_NAME = "wbty";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DBAdapter(Context context)
{
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist)
{
Log.i("DB....", "database available....");
}
else
{
this.getWritableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
Log.i("DB..", "database created.....");
}
}
public boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}catch(SQLiteException e){
Log.e("CheckDb","DB not found");
//database does't exist yet.
if(checkDB != null){
checkDB.close();
}
}
finally
{
if(checkDB != null){
checkDB.close();
}
this.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public SQLiteDatabase openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
return myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void getData()
{
SQLiteDatabase myDB ;
Cursor cursor ;
try {
myDB=this.openDataBase();
cursor=myDB.rawQuery("SELECT * FROM descriptions",null);
if (cursor != null ) {
if (cursor.moveToFirst()) {
do {
// put your code to get data from cursor
}while (cursor.moveToNext());
}
}
if(cursor != null)
{
myDB.close();
cursor.close();
}
}catch(SQLException sqle){
throw sqle;
}
}
}
和MyActivity类: -
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DBAdapter myDbHelper = new DBAdapter(MainActivity.this);
try
{
myDbHelper.createDataBase();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
myDbHelper.close();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我已将db文件保存在assets文件夹中。
答案 0 :(得分:0)
你得到哪种错误?请告诉我们有关您的问题的更多信息并发布logcat。这不应该是一个答案,但我需要解释你做错了什么:
您为outputFile写道:
String outFileName = DB_PATH + DB_NAME;
但是这会给你以下路径:
"/data/data/com.example.mynwedb/databases/wbtywbty";
因为您的DB_PATH
是
"/data/data/com.example.mynwedb/databases/wbty";
并且您的DB_NAME
是:
"wbty";
如果我们获得更多信息,我会尽力帮助你。
checkDatabase()
:
String myPath = DB_PATH + DB_NAME;