I have a code which creates database and insert data into table once when my app installs first time. I want to add "PRAGMA synchronous=OFF" in order to increase speed of insertion. Where in code bellow I should add "PRAGMA synchronous=OFF" ?
public class DataBaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "scheduleDB";
private static final int DB_VERSION = 1;
Context context;
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DB_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL("CREATE TABLE time (id INTEGER PRIMARY KEY AUTOINCREMENT, arrival VARCHAR(10), departure VARCHAR(10));");
insertTime(db,"city");
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
onCreate(db);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void insertTime(SQLiteDatabase database, String table) {
BufferedReader br = null;
String line;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
database.beginTransactionNonExclusive();
} else {
database.beginTransaction();
}
try {
br = new BufferedReader(new InputStreamReader(context.getAssets().open("time.txt")));
while ((line = br.readLine()) != null) {
String query = "INSERT INTO "+table+" (arrival, departure) VALUES (?,?)";
SQLiteStatement statement = database.compileStatement(query);
String[] time = line.split(",");
for(int i = 1, n = time.length; i < n; i+=2) {
statement.clearBindings();
statement.bindString(1,time[i-1]);//arrival
statement.bindString(2,time[i]);//departure
statement.execute();
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
database.setTransactionSuccessful();
database.endTransaction();
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}