这是我第一次使用pre-populate数据库,这意味着必须在 oncreate方法中显示数据库。 我需要使用select查询来执行SQL查询我阅读了许多教程,其中显示了获取预填充数据库的步骤。
如果有人能帮助我,我会很感激。
这是我从几个教程中使用它的代码
private static class DatabaseHelper extends SQLiteOpenHelper {
// ******* to copy database prepopulate the application*************//
// ********************************************************************//
private static String DB_PATH = "/data/data/com.devleb.database/world_cup";
public static String DATABASE_NAME = "world_cup";
private SQLiteDatabase db;
private final Context myContext;
// ******* to copy database prepopulate the application*************//
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying Data");
}
}
}
private void copyDataBase() throws IOException {
// TODO Auto-generated method stub
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DATABASE_NAME;
// Open the empty db as the output stream
OutputStream myOutPut = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutPut.write(buffer, 0, length);
}
myOutPut.flush();
myOutPut.close();
myInput.close();
}
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
public synchronized void close(){
if(db != null){
db.close();
}
}
private boolean checkDataBase() {
// TODO Auto-generated method stub
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
@Override
public void onCreate(SQLiteDatabase _db) {
//_db.execSQL(DATABASE_CREATE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version "
+ oldVersion + " to " + newVersion
+ ", which will destroy all old data!");
// Destroy old database:
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
// Recreate new database:
onCreate(_db);
}
}
我需要的是添加一个选择查询来选择我需要的东西,因为我有多个表预先填充。
答案 0 :(得分:-1)
然后为每个表创建模型类(也称为Bean),为每个表创建这样的方法
public static ArrayList<ModelTable1> SelectAll() {
SQLiteDatabase sqldb = //initialize database here;
ArrayList<ModelTable1> arrModelList = null;
Cursor cursor = null;
String Query = "Select * from " + TableName;
cursor = sqldb.rawQuery(Query, null);
if (cursor != null && cursor.moveToFirst()) {
arrModelList = new ArrayList<ModelTable1>();
do {
ModelTable1 model = new ModelTable1();
model.Id = (cursor.getString(cursor
.getColumnIndex("ID")));
model.name = (cursor.getString(cursor
.getColumnIndex("NAME")));
arrModelList.add(model);
} while (cursor.moveToNext());
cursor.close();
}// end if(cursor!=null)
return arrModelList;
}
修改强>
class ModelTable1{
String Id;
String name;
}