4

I want my database created once,and it will not changed,but i can't create data when i'm in onCreate()...

public class EventsData extends SQLiteOpenHelper {
       private static final String DATABASE_NAME = "events.db";
       private static final int DATABASE_VERSION = 1;


       public EventsData(Context ctx) {
          super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
       }

       @Override
       public void onCreate(SQLiteDatabase db) {

          db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + _ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TIME
                + " TEXT NOT NULL," + TITLE + " TEXT NOT NULL);");

    //This doesn't work..
          db.execSQL("INSERT INTO"+TABLE_NAME+"VALUES(null, 'hello', 'android');");
       }

       @Override
       public void onUpgrade(SQLiteDatabase db, int oldVersion,
             int newVersion) {
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
          onCreate(db);
       }

    }
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
stelios
  • 1,027
  • 3
  • 14
  • 19
  • take it out... Make a virtual class which holds your database, and with methods to start, modify, save, ... your database. Initialize that class in a global variable and call the methods of that class as needed. This way you also have more granular control over the database. – Mascarpone Jan 24 '11 at 11:06
  • whats ur prob?what error u r getting?pls paste logcat with error – chikka.anddev Jan 24 '11 at 11:09

2 Answers2

4

The line

 db.execSQL("INSERT INTO"+TABLE_NAME+"VALUES(null, 'hello', 'android');

is missing spaces around the TABLE_NAME. If you execute exactly this line, it can't work.

janos
  • 120,954
  • 29
  • 226
  • 236
gatnowurry
  • 344
  • 1
  • 7
0

1> Did you open the database ?

2> Also , create another class which will have the utility methods like openDatabase , createTable , deleteTable , insertValues , etc.

Sample syntax for INSERT STATEMENT

databaseDb.execSQL(INSERT INTO TABLE_NAME Values ('Chiranjib','Banerjee',1););

chiranjib
  • 5,288
  • 8
  • 53
  • 82