tags:

views:

87

answers:

4

I am creating my SQLite database for my App at runtime if it does not exist and insert rows if it does. Since it is supposed to be created at runtime and I have implemented it by creating a subclass of SQLiteOpenHelper and overriding the onCreate() method -

"Do I need to put anything in the /assets folder of my project?"

I am not using any Content Provider "Do I need to add any tags in the AndroidManifest.xml?"

Here is what I have done. The strings have been defined properly and I do not get any runtime exceptions.

Implementation of the SQLiteOpenHelper subclass.

public class TooyoouDB extends SQLiteOpenHelper {

                   public TooyoouDB(Context context) {
                    super(context, DATABASE_NAME, null,  DATABASE_VERSION );
                   }

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

                @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int 
                newVersion) {
                    Log.w("Example", "Upgrading database, this will drop tables and
                    recreate.");
                    db.execSQL("DROP TABLE IF EXISTS " +  USERAUTH_TABLE_NAME);
                    db.execSQL("DROP TABLE IF EXISTS " +  USERPREF_TABLE_NAME);
                    onCreate(db);
               }

}

Here is where I create an instance of the TooyoouDB subclass of the SQLiteOpenHelper.

TooyoouDB tdb = new TooyoouDB(Activity.this); 
SQLiteDatabase db = tdb.getReadableDatabase();

Everything runs and when I go to the sqlite shell and write the following query

select * from table_name - it just tells me no such record exist. I set breakpoints and it seems after the getReadableDatabase() is called the @Override OnCreate() method is never executed which is where I execute the Create table SQLs. I have tried getWritableDatabase() as well.

I dont understand why the tables are not being created. If anyone can help that would be awesome.

Thanks.

Query Text String#1

private static final String USERAUTH_TABLE_CREATE = "CREATE TABLE " + USERAUTH_TABLE_NAME + " (" + "number INTEGER NOT NULL," + "dip TEXT NOT NULL," + "email TEXT NOT NULL," + "password TEXT NOT NULL," + "flag INTEGER" + ");" ;

Query Text String #2

private static final String USERPREF_TABLE_CREATE = 
 "CREATE TABLE " + USERPREF_TABLE_NAME + " (" +
 "tpd TEXT NOT NULL ," +
 "cat TEXT NOT NULL" + ");";
+1  A: 

"Do I need to put anything in the /assets folder of my project?"

No

"Do I need to add any tags in the AndroidManifest.xml?"

No

Your syntax is ok ... could you paste the query you are making for creating tables ?

success_anil
put my query create string construction in my post..thanks
Aakash
Does the table create SQL statements look correct? I checked LogCat and did not see any errors logged.
Aakash
The statements look correct. Trust me, LogCat would blow up if there was a problem in syntax -- I've run in to that plenty :)
McStretch
I have not inserted any records yet. But the sqlite says no such TABLE exist. Can there be any issue with the context I am passing to the constructor?
Aakash
Did you try removing your application in the emulator, or like quartz suggested, up the version number?
McStretch
I am going to try that and update what happens.
Aakash
I did remove the app and it did not work...
Aakash
+1  A: 

If onCreate() is not being called, then the database has already been created for your app. The quickest way to solve it is to delete your project on the emulator (Settings --> Applications --> Your application), and then restart your application. Alternatively you could use ADB to just drop your database -- it's up to you. Restarting the app after dropping the database will call onCreate() because the database does not exist, and then your table creation sql will be run. onCreate() is only called if your database DOES NOT exist (so pretty much the first time you call the database in your code.

McStretch
I will definitely try this..but if it has been created when I query the tables it should not say "no such table exist" right?
Aakash
If you accessed the DB at all through code before setting up your two Table create methods, then the DB would have been created and the onCreate would not be called again. If the first time you ran this code it included the two Table creates, then we're looking at a slightly different problem -- although if the tables weren't created correctly you would have seen a stacktrace.
McStretch
I did not access the DB anywhere else in my code. It was only here where I tried to create the DB tables.
Aakash
Have you inserted any records into the tables yet? I just realized that you got a "no record exists" problem, not table.
McStretch
It may very well be that Aakash had an empty version of onCreate and then tried to use the already-created, empty database. Clear the app's data, or bump up the database's version number and override the onUpgrade method.
Quartz
I have not inserted any records yet. But the sqlite says no such TABLE exist. Can there be any issue with the context I am passing to the constructor?
Aakash
So I deleted my app from the emulator and reinstalled it again. Same issue persists. Tried changing the DATABASE_VERSION value, didnt work as well.
Aakash
After you erased your app, did you put a breakpoint in your onCreate() method to see if it was getting hit now? If that's not getting hit on the first time you hit a newly-installed app, then there's a different issue altogether, and you should probably post some more of your code.
McStretch
A: 

This might be a silly question, but have you defined the DATABASE_NAME and DATABASE_VERSION variables?

Robert Nekic
yah I have actually. I have set them to Strings for the DATABASE_NAME and DATABASE_VERSION to 1. No comment is silly to me. Thanks
Aakash
A: 

Issue resolved. Code was working all the way once again. sqlite shell was not showing me the tables and the database. When I kept my app running on the emulator and navigated to data > data > your-package-name > databases > your-database-file using DDMS the system shows me the SQLite DB was created fine. I have checked the tables are there as well.

Thank you all guys!!

Aakash