Note that there are some explanatory texts on larger screens.

plurals
  1. POSQLite database working on my emulator and doesn't work with my device
    primarykey
    data
    text
    <p>I'm working on an android application where I import my sqlite database into the android project .. the database works fine on the emulator but when i try the app on my device it doesn't work and outputs a message : Unfortunately, appname has stopped.</p> <p>I put my database in the assets folder in my project then use this code to copy it :</p> <pre><code>try { String destPath = "/data/data/" + getPackageName() + "/databases/MyDB"; File f = new File(destPath); if (!f.exists()) { CopyDB( getBaseContext().getAssets().open("mydb"), new FileOutputStream(destPath)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } </code></pre> <p>copyDB function : </p> <pre><code>public void CopyDB(InputStream inputStream, OutputStream outputStream) throws IOException { //---copy 1K bytes at a time--- byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) &gt; 0) { outputStream.write(buffer, 0, length); } inputStream.close(); outputStream.close(); } </code></pre> <p>DBAdapter Class : </p> <pre><code>public class DBAdapter { public static final String KEY_ROWID = "_id"; public static final String KEY_NAME = "username"; public static final String KEY_PASSWORD = "password"; private static final String DATABASE_NAME = "MyDB"; private static final String DATABASE_TABLE = "users"; private static final int DATABASE_VERSION = 4; private final Context context; private DatabaseHelper DBHelper; private SQLiteDatabase db; public DBAdapter(Context ctx) { this.context = ctx; DBHelper = new DatabaseHelper(context); } private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } //---opens the database--- public DBAdapter open() throws SQLException { db = DBHelper.getWritableDatabase(); return this; } //---closes the database--- public void close() { DBHelper.close(); } //---retrieves all the users--- public Cursor getAllusers() { return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME, KEY_PASSWORD}, null, null, null, null, null); } //---retrieves a particular user--- public User getuser(String username) throws SQLException { Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME, KEY_PASSWORD}, KEY_NAME + "=" + "'"+username+ "'", null, null, null, null, null); if(mCursor.moveToFirst()) { mCursor.moveToFirst(); User u=new User(Integer.valueOf(mCursor.getString(0)),mCursor.getString(1),mCursor.getString(2)); return u; }else return null; } </code></pre> <p>}</p> <p>Thank you so much in advance </p>
    singulars
    1. This table or related slice is empty.
    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