Tôi không hiểu tại sao tôi không thể sao chép tệp db của mình (abic_) vào thư mục ứng dụng ("/ data/data /" + context.getPackageName() + "/ cơ sở dữ liệu")Sao chép DB SQLite của riêng tôi từ thư mục Nội dung sang
Đây là lớp DataBaseHelper tôi:
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper extends SQLiteOpenHelper{
//The Android's default system path of your application database.
private String DB_PATH;
private static String DB_NAME = "abic_";
private static final Integer DB_VERSION = 1;
private SQLiteDatabase mydb;
private final Context myContext;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
DB_PATH = "/data/data/" + context.getPackageName() + "/databases";
}
@Override
public void onCreate(SQLiteDatabase db) {;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void apriDatabase() throws SQLException {
try {
String percorso = DB_PATH + DB_NAME;
mydb = SQLiteDatabase.openDatabase(percorso, null, SQLiteDatabase.OPEN_READONLY);
} catch (Exception e) {
e.printStackTrace();
}
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}
}
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;
// if the path doesn't exist first, create it
File f = new File(outFileName);
if (!f.exists()){
f.mkdir();
f.createNewFile();
}
//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();
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
@Override
public synchronized void close() {
if(mydb != null)
mydb.close();
super.close();
}
}
Đây là listview mà sử dụng lớp này:
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class abicabList extends ListActivity {
protected EditText searchText;
protected SQLiteDatabase mydb;
protected Cursor cursor;
protected ListAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mydb = (new DataBaseHelper(this)).getWritableDatabase();
searchText = (EditText) findViewById (R.id.searchText);
}
public void search(View view) {
String text = searchText.getText().toString();
String cap = "CAP";
// || is the concatenation operation in SQLite
if (text.length()14) {
cursor = mydb.rawQuery("SELECT _id, ABI, BANCA, CAB, CAP, Filiale, City FROM abicab WHERE ABI || CAB LIKE ? LIMIT 30",
new String[]{"%" + searchText.getText().toString().substring(6,10) + "%" + searchText.getText().toString().substring(11,15) + "%"});
adapter = new SimpleCursorAdapter(
this,
R.layout.abicab_list_item,
cursor,
new String[] {"BANCA", "Filiale", "City", "CAP"},
new int[] {R.id.BANCA, R.id.Filiale, R.id.City ,R.id.CAP});
setListAdapter(adapter);
}
}
Vì vậy, khi tôi chạy ứng dụng db được tạo ra trong thư mục nhưng nó không chứa bảng tha t là trong db tôi có trong thư mục tài sản (xem hình ảnh [1]: http://i.stack.imgur.com/AaFj8.jpg). Cảm ơn trước sự giúp đỡ của bạn.
Các giả lập. Vấn đề không phải là kích thước mà là bản sao. Infact nếu bạn có một cái nhìn vào pic tôi đã liên kết Bạn sẽ xác minh những gì tôi có ý nghĩa. – Giumazzi
Trình giả lập. Vấn đề không phải là kích thước mà là bản sao. Infact nếu bạn có một cái nhìn vào pic tôi đã liên kết Bạn sẽ xác minh những gì tôi có ý nghĩa. Tuy nhiên, nếu U muốn đăng bài như thế nào bạn đã giải quyết gắn mã của bạn, tôi sẽ rất hài lòng. Địa chỉ của tôi là giumazzi (at) gmail com. – Giumazzi
Vui lòng xem cập nhật. –