Sqlite onCreate () Problem I want my database to be created once and it will not change, but I cannot create data when I'm in onCreate ()

I want my database to be created once, and it will not change, but I can not 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); } } 
+4
source share
2 answers

Line

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

missing space around TABLE_NAME. If you execute this particular line, this will not work.

+4
source

1> Did you open the database?

2> Create another class that will have utilities such as openDatabase, createTable, deleteTable, insertValues, etc.

Syntax Example for INSERT STATEMENT

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

0
source

Source: https://habr.com/ru/post/1336768/


All Articles