2012-03-20

Practice Midterm Q#9 Solution.

Originally Posted By: mcastagnolo
public class MySQLiteHelper extends SQLiteOpenHelper {
private Context context;

public MySQLiteHelper(Context context) {
super(context, "SimpleDB", null, 1);
this.context = context;
}

@Override
public void onCreate(SQLiteDatabase database) throws SQLException {
database.execSQL("create table SimpleTable(SimpleColumn integer not null)");
database.execSQL("insert into SimpleTable values(5)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This method isn't necessary to implement, but is required to be here.
}

}

Driver Code

MySQLiteHelper dbHelper = new MySQLiteHelper(context);
dbHelper.getWriteableDatabase(); //Will execute the code in the onCreate method, creating the database and running the onCreate method.
'''Originally Posted By: mcastagnolo''' public class MySQLiteHelper extends SQLiteOpenHelper {<br>private Context context;<br><br>public MySQLiteHelper(Context context) {<br>super(context, &quot;SimpleDB&quot;, null, 1);<br>this.context = context;<br>}<br><br>@Override<br>public void onCreate(SQLiteDatabase database) throws SQLException {<br>database.execSQL(&quot;create table SimpleTable(SimpleColumn integer not null)&quot;);<br> database.execSQL(&quot;insert into SimpleTable values(5)&quot;);<br><br>}<br><br>@Override<br>public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {<br>// This method isn't necessary to implement, but is required to be here.<br>}<br><br>}<br><br>Driver Code<br><br>MySQLiteHelper dbHelper = new MySQLiteHelper(context);<br>dbHelper.getWriteableDatabase(); //Will execute the code in the onCreate method, creating the database and running the onCreate method.
X