开发者

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

开发者 https://www.devze.com 2023-02-06 04:17 出处:网络
I want my database created once,and it will not changed,but i can\'t create data when i\'m in onCreate()...

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(SQL开发者_Python百科iteDatabase db, int oldVersion,
             int newVersion) {
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
          onCreate(db);
       }

    }


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.


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););

0

精彩评论

暂无评论...
验证码 换一张
取 消