Note that there are some explanatory texts on larger screens.

plurals
  1. POSqlite database path is not working
    primarykey
    data
    text
    <p>I am trying to use sqlite database in my application. This is the problem while running </p> <pre> 07-26 18:03:10.374: E/AndroidRuntime(1347): FATAL EXCEPTION: main 07-26 18:03:10.374: E/AndroidRuntime(1347): android.database.sqlite.SQLiteException: no such table: QUESTIONS (code 1): , while compiling: SELECT * FROM QUESTIONS 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493) 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37) 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253) 07-26 18:03:10.374: E/AndroidRuntime(1347): at com.db.DBAdapter.getQuestionSet(DBAdapter.java:143) 07-26 18:03:10.374: E/AndroidRuntime(1347): at com.example.test_question.MainActivity.getQuestionSetFromDb(MainActivity.java:45) 07-26 18:03:10.374: E/AndroidRuntime(1347): at com.example.test_question.MainActivity.onClick(MainActivity.java:75) 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.view.View.performClick(View.java:4084) 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.view.View$PerformClick.run(View.java:16966) 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.os.Handler.handleCallback(Handler.java:615) 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.os.Handler.dispatchMessage(Handler.java:92) 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.os.Looper.loop(Looper.java:137) 07-26 18:03:10.374: E/AndroidRuntime(1347): at android.app.ActivityThread.main(ActivityThread.java:4745) 07-26 18:03:10.374: E/AndroidRuntime(1347): at java.lang.reflect.Method.invokeNative(Native Method) 07-26 18:03:10.374: E/AndroidRuntime(1347): at java.lang.reflect.Method.invoke(Method.java:511) 07-26 18:03:10.374: E/AndroidRuntime(1347): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 07-26 18:03:10.374: E/AndroidRuntime(1347): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 07-26 18:03:10.374: E/AndroidRuntime(1347): at dalvik.system.NativeStart.main(Native Method) </pre> <p>This is the code</p> <pre><code>public class DBAdapter extends SQLiteOpenHelper { protected static final String TAG = "DataAdapter"; private static String DB_NAME = "mydb.sqlite"; private SQLiteDatabase myDatabase; private static Context myContext; private static String DB_PATH ="/data/data/com/databases/"; public DBAdapter(Context context) { super(context, DB_NAME, null, 1); // TODO Auto-generated constructor stub this.myContext = context; } /** * Creates a empty database on the system and rewrites it with your own * database. * */ public void createDataBase() throws IOException { boolean dbExist = checkDataBase(); if (!dbExist) { // 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) { throw new Error(e.toString()); } } } /** * Check if the database already exist to avoid re-copying the file each * time you open the application. * @return true if it exists, false if it doesn't */ private boolean checkDataBase() { SQLiteDatabase checkDB = null; try { String myPath = DB_PATH + DB_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } catch (SQLiteException e) { // database does't exist yet. } if (checkDB != null) { checkDB.close(); } return checkDB != null ? true : false; } /** * Copies your database from your local assets-folder to the just created * empty database in the system folder, from where it can be accessed and * handled. This is done by transfering bytestream. * */ 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; // 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)) &gt; 0) { myOutput.write(buffer, 0, length); } // Close the streams myOutput.flush(); myOutput.close(); myInput.close(); } public void openDataBase() throws SQLException { // Open the database String myPath = DB_PATH + DB_NAME; myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } @Override public synchronized void close() { if (myDatabase != null) myDatabase.close(); super.close(); } public List&lt;Question&gt; getQuestionSet(int numQ) { List&lt;Question&gt; questionSet = new ArrayList&lt;Question&gt;(); Cursor c = myDatabase.rawQuery("Select * from questions",null); while (c.moveToNext()) { Question q = new Question(); q.setSolution(c.getString(0)); q.setOption3(c.getString(1)); q.setOption2(c.getString(2)); q.setOption1(c.getString(3)); q.setAnswer(c.getString(4)); q.setQuestion(c.getString(5)); questionSet.add(q); } return questionSet; } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); onCreate(db); } </code></pre> <p>Thanks for help</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