public class MyDBHelper extends DefaultHandler{
public static final String DATABASE_NAME = "sales.db"; // The DB name
public static final String TABLE_NAME = "sales"; //The table within the DB
public static final int DATABASE_VERSION = 1; // The DB version for upgrade proposes
public static final String S_ID = "_id"; // The DB id, every DB need an ID (SQLite demand)
public static final String S_NAME = "name"; //The DB name and price
public static final String S_PRICE = "price";
public MyDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Method is called during creation of the database
// The execSQL will execute SQL code for creating a table the template is: create table TABLE_NAME ( FIELD_1 text
// primary key, FIELD_2 int)
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL("create table " + TABLE_NAME + " (" + S_ID
+ " int primary key, " + S_NAME + " text, " + S_PRICE + " text)");
}
// Method is called during an upgrade of the database
// The upgrade SQL code varies with the database and structure
// so for example purposes i will destroy and create a new table every time.
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); //Destroys table
onCreate(database); //Creates a new one
}
}
Be the first to comment