Android,使用InsertHelper和Assets创建并填充SQLite数据库
我是Android开发新手,希望使用Assets文件夹中的csv文件创建并预填充SQLite数据库。我正在使用数据库适配器类,在创建数据库后的onCreate方法中,我希望从assets文件夹中读取csv文件,并使用DatabaseUtils.InsertHelper将其用于插入行Android,使用InsertHelper和Assets创建并填充SQLite数据库,android,database,sqlite,Android,Database,Sqlite,我是Android开发新手,希望使用Assets文件夹中的csv文件创建并预填充SQLite数据库。我正在使用数据库适配器类,在创建数据库后的onCreate方法中,我希望从assets文件夹中读取csv文件,并使用DatabaseUtils.InsertHelper将其用于插入行 import android.database.DatabaseUtils.InsertHelper; //... public void onCreate(SQLiteDatabase db) { d
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();
}
}
//...
}
我在getAssets()行上得到一个eclipse错误,它说:
-The method getAssets() is undefined for the type DatabaseHelper
我似乎根本无法让上下文正常工作,任何帮助都将不胜感激
更新:
我应该包括所有代码,即:
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....
如果我在上下文中添加以下内容,我会得到一个错误:
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 method onCreate(SQLiteDatabase, Context) of type DQDbAdapter.DatabaseHelper must override or implement a supertype method
请你帮我解决这个错误。谢谢您需要在应用程序中填充数据库,还是可以提前准备?请看这里: 如果你真的想在你发布的代码中做你想做的事情,你可以这样做:
public class DatabaseHelper extends SQLiteOpenHelper {
...
private Context context;
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
...
context.getAssets()...
}
您需要在应用程序中填充数据库,还是可以提前准备?请看这里: 如果你真的想在你发布的代码中做你想做的事情,你可以这样做:
public class DatabaseHelper extends SQLiteOpenHelper {
...
private Context context;
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
...
context.getAssets()...
}