Non capisco perché non riesca a copiare il mio file db (abic_) nella directory dell'applicazione ("/ data/data /" + context.getPackageName() + "/ database")Copia il mio DB SQLite dalla cartella Asset a
questa è la mia classe DataBaseHelper:
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();
}
}
questa è la listview che utilizza questa classe:
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);
}
}
Così, quando eseguo l'applicazione del db viene creato nel cartella ma non contiene la tabella tha t è nel db che ho nella cartella delle risorse (vedi l'immagine [1]: http://i.stack.imgur.com/AaFj8.jpg). Grazie in anticipo per il vostro aiuto.
L'emulatore. Il problema non è la dimensione ma la copia. Infatti se dai un'occhiata alla foto che ho collegato, verificherò cosa intendo. – Giumazzi
L'emulatore. Il problema non è la dimensione ma la copia. Infatti se dai un'occhiata alla foto che ho collegato, verificherò cosa intendo. Tuttavia, se vuoi pubblicare come hai risolto il tuo codice, sarò molto contento. Il mio indirizzo è giumazzi (at) gmail com. – Giumazzi
Si prega di consultare l'aggiornamento. –