0

I'm attempting to create a SQLite db and populate it with data initially. When I run the program through the debugger in Eclipse, nothing appears on the screen but I get the following from LogCat:

sqlite returned: error code = 1, msg = no such table: Issues

Here's some of the code (I'll try to show only what's needed but if you need to see more, plz let me know):

MainActivity.java

public class MainActivity extends Activity {

//TAG for LogCat.
public static final String TAG = "SQLiteDebug";
private ListView mListViewIssues;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Load listview control.
    mListViewIssues = (ListView)findViewById(R.id.listIssues);

    //Create Data Creator.
    IssueInfoCreator creator = new IssueInfoCreator(this);

    creator.open();
    try {
        creator.insertRandomData();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //add data to listview through adapter.
    mListViewIssues.setAdapter(new IssueInfoAdapter(this, creator.queryAll()));

    //Close connection.
    creator.close();
}
}

Code for IssueInfoCreator:

public class IssueInfoCreator {

//DB Adapter.
private DBAdapter mDBAdapter;

//Constructor.
public IssueInfoCreator(Context c){
    mDBAdapter = new DBAdapter(c);
}

//Open DBAdapter.
public void open(){
    mDBAdapter.open();
}

//Insert Random Data to get started.
public void insertRandomData() throws ParseException{
    long i = 999;
    Date dtDate = new Date();
    dtDate.parse("1/12/2012");

    mDBAdapter.insertIssue(i++, dtDate, "This is my ticket.  I'm having trouble starting my sim.");
    mDBAdapter.insertIssue(i++, dtDate, "I can't my sim to turn on, please help.");
    mDBAdapter.insertIssue(i++, dtDate, "Aircraft is not responding to callsign.");
    mDBAdapter.insertIssue(i++, dtDate, "TTS is not recognising when we attempt to land an aircraft.");
}

//Get all Issues from DB.
public List<IssueInfo> queryAll(){
    return mDBAdapter.fetchAllIssues();
}

//close connection.
public void close(){
    mDBAdapter.close();
}
}

Code for DBAdapter:

public class DBAdapter {

//Database fields.
public static final String ISSUES_TABLE = "Issues";
public static final String COL_ISSUE_ID = "_id";
public static final String COL_DATE_RECEIVED = "DateReceived";
public static final String COL_ISSUE_SUMMARY = "IssueSummary";

//Declarations.
private Context mContext;
private SQLiteDatabase mDB;
private DBHelper mDBHelper;

//Constructor.
public DBAdapter(Context c){
    mContext = c;
}

//Open DB Connection.
public DBAdapter open() throws SQLException{
    mDBHelper = new DBHelper(mContext);
    mDB = mDBHelper.getWritableDatabase();
    return this;
}

//Close DB Connection.
public void close(){
    mDBHelper.close();
}
//remaining code omitted for brevity...
}

and finally code for DBHelper:

public class DBHelper extends SQLiteOpenHelper {

//Constants
private static final String DB_NAME = "dbOpenIssues";
private static final int DB_VERSION = 1;
private static final String CREATE_DB_SQL = 
        "CREATE TABLE Issues ( " +
                "_id int PRIMARY KEY, " +
                "DateReceived datetime NOT NULL, " +
                "IssueSummary text NOT NULL " +
                ");";
private static final String DROP_TABLE = 
        "DROP TABLE IF EXISTS Issues";

public DBHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_DB_SQL);
}

@Override   
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(DROP_TABLE);
    onCreate(db);
}
}

If I run it with an emulator attached, I can see that the DB gets created in the file explorer but if I pull it down locally and open it with SQLiteMan, it only shows the android_metadata table.

Any ideas? again, sorry for the long post, just didn't want to leave anything out and have to repost 10 different times.

UPDATE:

One thing I tried was to print the SQL statement in LogCat but I noticed the onCreate (in DBHelper)never gets called:

public void onCreate(SQLiteDatabase db){
    Log.e("DB ERROR -- ", CREATE_DB_SQL);
    db.execSQL(CREATE_DB_SQL);
}

3 Answers 3

2

There is no datetime type in SQLite. To store data you can use TEXT. So in your case query can be the following:

private static final String CREATE_DB_SQL = 
        "CREATE TABLE Issues (" +
                "_id integer PRIMARY KEY, " +
                "DateReceived text NOT NULL, " +
                "IssueSummary text NOT NULL)";

Here is the link to data types.

3
  • 1
    Try to delete database from filesystem and run your application once again.
    – Yury
    Jan 27, 2012 at 21:03
  • Deleting the DB and then running the program again resolved the issue. unbelievable, lol...thanks for the help, I was stuck on this for hours. The app is not diplaying data (but I verified that it's there through SQLiteMan) but I believe I can figure that out. Does the emulator cache the DB in memory or something?
    – Robert
    Jan 27, 2012 at 21:26
  • 1
    When you start your emulator there is a checkbox "Wipe user data". If it is unchecked your emulator stores user data in the image. Thus, you had the same version of db and you could not it created once again.
    – Yury
    Jan 27, 2012 at 21:30
0

Have you tried running your SQL Create statement in the Emulator using adb?

Here is more info on adb if you are not familiar : http://developer.android.com/guide/developing/tools/adb.html

When debugging sqlite problems on android, I find its easiest to try running the SQL commands manually from inside the emulator.

1
  • Not in the emulator but I did run it in SQLiteMan and it does create the table (that's after I changed the datetime to text) but it's still erroring out with the same error.
    – Robert
    Jan 27, 2012 at 21:01
0

onCreate() is called when the database is created for the first time. So, if the table failed to be created but the database has already been created, onCreate() will not be called again.

Delete the database and rerun the program with any changes you've made to see if it works now...

3
  • I am generating and inserting the _id so I can't have it autoincrement.
    – Robert
    Jan 27, 2012 at 20:47
  • I understand what you are saying but I believe it's being called through the IssueInfoCreator class. new IssueInfoCreator(this) calls new DBAdapter(c) and that creates an instance of DBHelper in the constructor and then calls the .getWritableDatabase() method. Can it not work that way?
    – Robert
    Jan 27, 2012 at 21:06
  • Actually, you are right. According to the Android docs onCreate() should be called. However, onCreate() is called when the database is created for the first time.
    – Luis
    Jan 27, 2012 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.