Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with SQLite onCreate() in Android
    primarykey
    data
    text
    <p>I'm extending the SQLiteOpenHelper class to help me connect and do my database work. According to the <a href="http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html" rel="nofollow" title="docs">documentation</a>, the OnCreate method should only be called if the database has not been created. Yet, my problem is that I am getting this error when I try to execute a query to insert a record.</p> <pre><code>ERROR/Database(214): Failure 1 (table Sample already exists) on 0x218688 when preparing 'CREATE TABLE Sample (RecId INT, SampleDesc TEXT);'. </code></pre> <p>The only place this Create query is used in code is the OnCreate method which looks like this.</p> <pre><code> @Override public void onCreate(SQLiteDatabase db) { db.execSQL(SAMPLE_TABLE_CREATE); } </code></pre> <p><strong>Note:</strong> I'm following a <a href="http://www.screaming-penguin.com/node/7742" rel="nofollow" title="Here">tutorial</a> - the only thing I've done different is make the SQLiteDatabase object public instead of private so that I could extend this class for each entity, letting the public SQLiteDatabase object from the inherited DataHelper class do all the work</p> <p>Here is the method that makes the call that fails.</p> <pre><code> //This method is in the class that extends DataHelper (See note on tutorial) public void createSample(Sample sample)//next action form { String id = sample.getId(); String name = sample.getSummary(); String query = "INSERT INTO " + SAMPLE_TABLE_NAME + "( " + SAMPLE_Id + "," + SAMPLE_NAME + ") " + " VALUES (" + id + "," + name + ")"; try{ data.rawQuery(query, null); } catch(SQLException e){ Log.i("Sample", "Errors: Sample LN60: " + e.getMessage()); } } </code></pre> <p>Can someone tell me what I'm doing wrong? Or maybe a hack (i.e. check if table exists before executing create statement)</p> <p>Please let me know what other code I can post to solve this...</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.
 

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