I am new to Android development and would like to create and pre-populate a SQLite database using csv files from the Assets folder. I am using a database adapter class and in the onCreate method after creating the database I want to read the csv file fromt the assets folder and use it to insert rows using DatabaseUtils.InsertHelper.
import android.database.DatabaseUtils.InsertHelper;
//...
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
// need to read the path csv data here containing columns "_id" and "path"
try {
InputStream is = getAssets().open("pathcsv");
int size = is.available();
// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a string.
String text = new String(buffer);
} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}
// Create a single InsertHelper to handle this set of insertions.
InsertHelper ih = new InsertHelper(db, "path_table");
// Get the numeric indexes for each of the columns that we're updating
final int id_Column = ih.getColumnIndex("_id");
final int path_Column = ih.getColumnIndex("path");
while (moreRowsToInsert) {
// ... Create the data for this row (not shown) ...
// Get the InsertHelper ready to insert a single row
ih.prepareForInsert();
// Add the data for each column
ih.bind(id_Column, idData);
ih.bind(path_Column, pathData);
// Insert the row into the database.
ih.execute();
}
}
//...
}
I am getting an eclipse error on the getAssets() line saying :
-The method getAssets() is undefined for the type DatabaseHelper
I can't seem to get context working at all, any help would be appreciated.
Update:
I should have included all of the code, which is :
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(PATH_TABLE_CREATE);
try {
InputStream is = getAssets().open("@string/path");
// the rest is as in my first post above....
If I add in a context as follows I get an error:
private static class DatabaseHelper extends SQLiteOpenHelper {
private Context myContext;
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);}
@Override
public void onCreate(SQLiteDatabase db, Context context) {
db.execSQL(PATH_TABLE_CREATE);
myContext = context;
try {
InputStream is = myContext.getAssets().open("@string/path");
// the rest is as in my first post above....
The error I get is:
The method onCreate(SQLiteDatabase, Context) of type DQDbAdapter.DatabaseHelper must override or implement a supertype method
Please can you help me to resolve this error. Thanks