I have a .sql file with alter Table statements in /sdcard/file.sql i need to execute into sqlite database in my android app using this .sql file.
how could i do this ?
I already have this code:
public void executeSQLScript(SQLiteDatabase database,String fileName) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
AssetManager assetManager = context.getAssets();
InputStream inputStream = null;
try{
inputStream = assetManager.open("sql/"+fileName);
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
String[] createScript = outputStream.toString().split(";");
for (int i = 0; i < createScript.length; i++) {
String sqlStatement = createScript[i].trim();
if (sqlStatement.length() > 0) {
database.execSQL(sqlStatement + ";");
}
}
} catch (IOException e){
System.out.println("Fail to load .sql file");
e.printStackTrace();
}
}
Aucun commentaire:
Enregistrer un commentaire