Note that there are some explanatory texts on larger screens.

plurals
  1. PONo such table error when try to use database in android project
    primarykey
    data
    text
    <p>I am creating database tables to my android project and the following is the helper class. Here I am able to create the database, when I try to get the data from the method getSettings() it is showing an error saying that no such table 'Settings'. But I created the table in onCreate method. After that I debug and found that OnCreate method is not calling. Can you guys please have a look into the follwoing code and suggest me.</p> <pre><code>public class DataBaseHelper extends SQLiteOpenHelper { private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window //destination path (location) of our database on device private static String DB_PATH = ""; private static String DB_NAME ="testDatabase";// Database name private SQLiteDatabase mDataBase; private final Context mContext; private final String SETTINGS_TABLE = "settings"; private final String SETTINGS_COLUMN_ID = "_id"; private final String SETTINGS_COLUMN_PROFILE_ID = "profileId"; private final String SETTINGS_TABLE_CREATE = "create table " + SETTINGS_TABLE + "(" + SETTINGS_COLUMN_ID + " integer primary key autoincrement, " + SETTINGS_COLUMN_PROFILE_ID + " varchar(100), );"; private final int SETTINGS_ID = 1; public DataBaseHelper(Context context) { super(context, DB_NAME, null, 1);// 1? its Database Version DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; this.mContext = context; } public void createDataBase() throws IOException { //If database not exists copy it from the assets boolean mDataBaseExist = checkDataBase(); if(!mDataBaseExist) { this.getWritableDatabase(); this.close(); try { //Copy the database from assests copyDataBase(); Log.e(TAG, "createDatabase database created"); } catch (IOException mIOException) { throw new Error("ErrorCopyingDataBase"); } } } //Check that the database exists here: /data/data/your package/databases/Da Name private boolean checkDataBase() { File dbFile = new File(DB_PATH + DB_NAME); //Log.v("dbFile", dbFile + " "+ dbFile.exists()); return dbFile.exists(); } //Copy the database from assets private void copyDataBase() throws IOException { InputStream mInput = mContext.getAssets().open(DB_NAME); String outFileName = DB_PATH + DB_NAME; OutputStream mOutput = new FileOutputStream(outFileName); byte[] mBuffer = new byte[1024]; int mLength; while ((mLength = mInput.read(mBuffer))&gt;0) { mOutput.write(mBuffer, 0, mLength); } mOutput.flush(); mOutput.close(); mInput.close(); } //Open the database, so we can query it public boolean openDataBase() throws SQLException { String mPath = DB_PATH + DB_NAME; mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY); mDataBase = getWritableDatabase(); return mDataBase != null; } @Override public synchronized void close() { if(mDataBase != null) mDataBase.close(); super.close(); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub mDataBase = db; System.out.println("Oncreate table"); try { db.execSQL(SETTINGS_TABLE_CREATE); InsertSettingsData(); Log.d(TAG, "onCreate Success create SETTINGS_TABLE_CREATE"); } catch (Exception e) { e.printStackTrace(); } } private int InsertSettingsData() { // TODO Auto-generated method stub Log.d(TAG, "InsertConfigInitialData"); try { ContentValues resourceTable = new ContentValues(); resourceTable.put(SETTINGS_COLUMN_ID, SETTINGS_ID); resourceTable.put(SETTINGS_COLUMN_PROFILE_ID, "123456"); int resourceId = (int) mDataBase.insert(SETTINGS_TABLE, null, resourceTable); if (resourceId &gt;= 0) { return resourceId; } } catch (Exception e) { Log.d(TAG, "InsertConfigInitialData failed : " + e.toString()); e.printStackTrace(); } return -1; } public boolean UpdateConfigData() { Log.d(TAG, "UpdateConfigData"); try { String condition = SETTINGS_COLUMN_ID + "=" + SETTINGS_ID; ContentValues resourceTable = new ContentValues(); resourceTable.put(SETTINGS_COLUMN_PROFILE_ID, "34567"); int totalUpdated = mDataBase.update(SETTINGS_TABLE, resourceTable, condition, null); if (totalUpdated &gt; 0) { return true; } else { Log.d(TAG, "UpdateConfigData failed, no records updated"); } } catch (Exception e) { Log.d(TAG, "UpdateConfigData failed : " + e.toString()); } return false; } public SettingsObject getSettings() { Log.d(TAG, "LoadConfigs"); SettingsObject settings = new SettingsObject(); String condition = SETTINGS_COLUMN_ID + "=" + SETTINGS_ID; try { Cursor cursor = mDataBase.query(SETTINGS_TABLE, null, condition, null, null, null, null); if (cursor != null &amp;&amp; cursor.moveToFirst()) { SettingsObject.setProfile_id(cursor.getString(cursor .getColumnIndex(SETTINGS_COLUMN_PROFILE_ID))); cursor.close(); } } catch (Exception e) { e.printStackTrace(); Log.d(TAG, "LoadConfigs failed : " + e.getMessage()); } return settings; } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } } </code></pre> <p>from my activity class I am calling like this,</p> <pre><code> DataBaseHelper myDbHelper = new DataBaseHelper(this); myDbHelper = new DataBaseHelper(this); try { myDbHelper.createDataBase(); } catch (IOException ioe) { throw new Error("Unable to create database"); } try { myDbHelper.openDataBase(); } catch(SQLException sqle){ throw sqle; } SettingsObject object = myDbHelper.getSettings(); System.out.println("settings profile Id" + object.getProfile_id()); </code></pre> <p><strong>Edit1:</strong> </p> <p>Can I call creating database tables in the following method?</p> <pre><code> //Open the database, so we can query it public boolean openDataBase() throws SQLException { String mPath = DB_PATH + DB_NAME; //Log.v("mPath", mPath); mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY); //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); mDataBase = getWritableDatabase(); mDataBase.execSQL(SETTINGS_TABLE_CREATE); InsertSettingsData(); return mDataBase != null; } </code></pre>
    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.
    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