1

我做了一个安卓词典应用程序。我创建了一个名为“kamusJawa.sqlite”的数据库并将其复制到资产文件夹中。我尝试了此链接Own Database in Assets Folder on Android Eclipse Project 中的代码

这是我的数据库管理器类:

 package com.kamusJI;

 public class DBHelper extends SQLiteOpenHelper{

private static String DBPATH = "/data/data/com.kamusJI/databases/";
private static String DBNAME = "kamusJawa.sqlite";
private SQLiteDatabase DBSQ;
private final Context KJICtx;

public DBHelper(Context context) throws IOException {
    super(context, DBNAME, null, 1);
    this.KJICtx = context;
    // TODO Auto-generated constructor stub
    boolean dbexist = cekDB();
    if (dbexist) {
        //System.out.println("Database exists");
        openDB(); 
    } else {
        System.out.println("Database doesn't exist");
            createDB();

    }
}

public void createDB() throws IOException{
    boolean dbExist = cekDB();
    if(!dbExist){
        this.getReadableDatabase();
        try{
            salinDB();
        }catch (IOException e){
            throw new Error("Gagal menyalin database");
        }
    }
}
 boolean cekDB() {
    //SQLiteDatabase cekDatabase = null;
 boolean cekdb = false;
    try{
    String path = DBPATH + DBNAME;
    File dbfile = new File(path);
    //cekDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
    cekdb = dbfile.exists();
    }catch(SQLException e){
        System.out.println("Database tidak ada");
    }

    return cekdb;
    //return cekDatabase !=null ? true : false;
} 

private void salinDB() throws IOException{
    AssetManager AM = KJICtx.getAssets();
    File DbFile = new File(DBPATH+DBNAME);
    InputStream in = KJICtx.getAssets().open(DBNAME);
    //OutputStream out = new FileOutputStream(DbFile);
    OutputStream out = new FileOutputStream("/data/data/com.kamusJI/databases/kamusJawa.sqlite");
    DbFile.createNewFile();
    byte[] b = new byte[1024];
    int i, r;
    String[] Files = AM.list("");
    Arrays.sort(Files);

    i= 1;
    String fdb = String.format("kamusJawa.db.00%d", i);
    while(Arrays.binarySearch(Files, fdb)>=0){
        //InputStream in = AM.open(fdb);
        while(( r = in.read(b))>0)
            out.write(b,0,r);
        in.close();
        i++;
        fdb = String.format("kamusJawa.db.00%d", i);
    }
    out.flush();
    out.close();
}

public void openDB() throws SQLException{
    String path = DBPATH+DBNAME;
    DBSQ = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
}

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

@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

}

这是我的主要课程:

package com.kamusJI;


public class KJI extends ListActivity {
private KJI this_class = this;
String[] Menu = {"Basa Jawa", "Bahasa Indonesia", "Tambah Data"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.Cari, Menu));
    ListView lv = getListView();
    lv.setTextFilterEnabled(false);

    /* Defines On Item Click callback method */
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Intent action = null;
            switch(position) {
                case 0:
                case 1:
                    action = new Intent(getApplicationContext(), Cari.class);
                    action.putExtra("MODE", position);
                    break;
                case 2:
                    action = new Intent(getApplicationContext(), Tambah.class);
                    action.putExtra("MODE", position);
                    break;
                case 3:
                    finish();
                    return;
            }
            startActivity(action);
            Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();


        }
    });
}

public void InitDatabase() {
    AsyncTask<String, Void, String> InitDB = new AsyncTask<String, Void, String>() {
        Dialog progress = null;
        String msg;
        DBHelper dbhelper;

        @Override
        protected void onPreExecute() {
            try {
                dbhelper = new DBHelper(this_class);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (!dbhelper.cekDB())
            progress = ProgressDialog.show(this_class, "", "Installing Database.\nPlease wait.");
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                dbhelper.createDB();
                msg = "Database successfully installed.";
            } catch (IOException ioe) {
                msg = "Database installation failed.";
            }
            return msg;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (progress!=null) {
                progress.dismiss();
                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
            }
        }
    };
    InitDB.execute(new String());
   }
}

当我运行我的应用程序时,然后我转到文件资源管理器,我找不到 data/data/com.kamusJI/databases。怎么会这样?

4

2 回答 2

0

将您的数据库扩展名更改为 .db

于 2013-05-14T06:12:44.743 回答
0

您需要特殊权限(例如root 访问权限)才能读取路径:

/data/data /com.package/databases

于 2016-12-01T22:12:51.480 回答