Note that there are some explanatory texts on larger screens.

plurals
  1. POCopy my own SQLite DB from Asset folder to
    primarykey
    data
    text
    <p>I don't understand why I'm not able to copy my db file (abic_) to the application directory ("/data/data/" + context.getPackageName() + "/databases")</p> <p>This is my DataBaseHelper class:</p> <pre><code> 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(); } }</code></pre> <p>This is the listview that uses this class:</p> <pre><code> 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); } } </code></pre> <p>So when I run the application the db is created in the folder but it doesn't contain the table that is in the db I have in asset folder (see the picture [1]: <a href="http://i.stack.imgur.com/AaFj8.jpg">http://i.stack.imgur.com/AaFj8.jpg</a>). Thanks in advance for your help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload