
Res = sb.append("ID="+ID+"\n Name="+Name+"\n Address="+Address) įollowing is the content of the modified MainActivity.

String columns = Ĭursor cursor=db.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null) Long result = db.insert(DBHelper.TABLE_NAME, null, values) Publiclong addUser(String id, String name, String address)ĬontentValues values=new ContentValues() Toast.makeText(c, "onUpgrade method is called",Toast.LENGTH_LONG).show() Toast.makeText(c, "onCreate method is onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) Toast.makeText(c, "Constructor is onCreate(SQLiteDatabase db) Privatestaticfinal String DROP_TABLE = "drop table if exists "+TABLE_NAME Privatestaticfinal String CREATE_TABLE = "create table "+TABLE_NAME+"("+UID+" integer,"+NAME+" varchar,"+ADDRESS+") " Privatestaticfinal String ADDRESS = "Address" Privatestaticfinal String TABLE_NAME = "MyTable" Privatestaticfinal String DATABASE_NAME = "MyDatabase" Publicclass DBHelper extends SQLiteOpenHelper Suppose that database name is MyDatabase and table name is MyTable that has three columns as above.įirst we will declare the following string and integer variable.įollowing is the content of the modified DBHelper.java fileĬreate a sub class that will extend from SQLiteOpenHelper Define the database name, table name, columns name and its type, triggers if exists, aggregate function if exists and so on. Multiple statements separated semicolons are not supported.ĭatabase schema means, defining the structure of database and table. If SQL statement is not correct, then it will throw SQLException. This method works fine with statements like Create table, Drop table, Alter table, etc. So, this method does not work for those SQL queries, which return some records or value.įor example: select query statement will not work with execSQL method. It contains methods to create table, and execute SQL commands.įor example: execSQL(String SQL) Return type of this method is void.
#DBHELPER CONTEXT ANDROID#
SQLiteDatabase is another important class that is provided by Android frame work. Always remember that each time you have to change the version number. Use this method to drop tables, add tables, add or delete any column in the table. OnUpgrade() method is called when your database or table needs to be upgraded.
#DBHELPER CONTEXT CODE#
The code for creation of database table should be written in onCreate() method.


OnCreate() method will be called for the first time when the Android application is run. The constructor call super (context, name, factory, version) is used to setup the database. It opens the database if exists and upgrades it, as necessary. SQLiteOpenHelper class creates the new database according to your schema. TODO Auto-generated method db, int oldVersion, int newVersion) Public DBHelper(Context context, String name, CursorFactory factory, Publicclass DBHelper extendsSQLiteOpenHelper public DBHelper(Context context, String name, CursorFactory factory,int.
