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

StackOverflow https://stackoverflow.com/questions/4781157

  •  23-10-2019
  •  | 
  •  

سؤال

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

    }
هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top