-1

How can we execute pre-database sqlite statements in Android?

I have created my own database layer. This layer will create the database and even the queries. It has a method that will create the table by providing the table name, column names and column types as parameters to it and the table will be generated without writing the full length query. So, to implement this I have created a test application and inside the onCreate method I have used my method that is going to generate the CREATE TABLE query and execute it. But as the database is not yet created it's throwing a NullPointerException.

So how can I fix this exception?

Volo
  • 28,673
  • 12
  • 97
  • 125
Prachi
  • 994
  • 5
  • 23
  • 36
  • 3
    what are pre-database sql statements? – Axarydax Mar 03 '11 at 09:10
  • 1
    Please be descriptive while framing your questions. – Samuh Mar 03 '11 at 09:11
  • 1
    What are you trying to do? Without database on what you are trying to execute sql queries? Give some more details. – Tushar Vengurlekar Mar 03 '11 at 09:11
  • 1
    I think he is trying to execute statements when database is first open before every other SQL. – Vladislav Rastrusny Mar 03 '11 at 09:21
  • 1
    I have created my own database layer.This layer will creating the databse and even the queries. It has a method that will create the table by providing the table name, column names and column types as parameters to it and the table will be generated without writing the full length query . So, to implement this I have created a test application and inside the onCreate method I have used my method that is going to generate the 'Create table' query and execute it. but as the database is yet not created it's throwing a null exception. So how can I fix this exception? – Prachi Mar 03 '11 at 09:25
  • @FractalizeR: U got me correct..that's what exactly I am trying to do. – Prachi Mar 03 '11 at 09:26

1 Answers1

1

You need to use OpenHelper for this. Override it's OnOpen method to make actions on database open.

http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#onOpen%28android.database.sqlite.SQLiteDatabase%29

http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/

Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
  • I am already using the OpenHelper for this.I am overrriding its OnCreate method() and inside this method I am calling my own method to generate the table. Here the problem is that as the database is still null so I am not able to execute the Sql statements which I am using in my own method. – Prachi Mar 03 '11 at 09:36
  • 1
    What about OnOpen, not OnCreate? – Vladislav Rastrusny Mar 03 '11 at 09:49
  • I am using just the OnCreate.. i have the code but it is for BlackBerry. I am not able to convert it to android code.If u want then u can go though the BB code.I can send it to u. – Prachi Mar 03 '11 at 09:57
  • Well, I don't understand. Your question is marked as Android and you ask about Android. How come Blackberry is there also? – Vladislav Rastrusny Mar 03 '11 at 10:31
  • 1
    okk..can u just tell me how can I convert a query which is in String to a SQLStatement so that i can execute it. – Prachi Mar 03 '11 at 10:47
  • http://stackoverflow.com/questions/433392/how-do-i-use-prepared-statements-in-sqlite-in-android – Vladislav Rastrusny Mar 04 '11 at 09:04