Sorry for late response in case of anyone having same issue
Here is the solution step by step
I create a .sql
file which contain insert queries (some Include multiline queries) ends with :.
In my onCreate()
method of SQLiteOpenHelper
I m counting lines of my file using getFileLineCount()
. This method return me the count of line. This line count is never used , but I had it because if all your queries are single line, it might comes into play. You can skip this method.
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE \"" + TABLE_POEM + "\" (\"" + POEM_LOCAL_ID + "\" INTEGER PRIMARY KEY AUTOINCREMENT , \"" + POEM_TEXT + "\" TEXT,\"" + POEM_ID + "\" INTEGER , \"" + POEM_IS_FAV + "\" TEXT);");
int totalLineCount = getFileLineCount("poem.sql");
int insertCount = insertFromFile(db, "poem.sql", totalLineCount);
Log.d("DatabaseHelper", "------------onCreate(SQLiteDatabase db) >>>>>>> completed--------");
}
private int getFileLineCount(String assetFilePath) {
InputStream insertStream = null;
int totalCount = 0;
try {
insertStream = context.getAssets().open(assetFilePath);
totalCount = FileUtil.countLines(new BufferedInputStream(insertStream));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (insertStream != null) {
try {
insertStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return totalCount;
}
By using this method I insert queries from file. It handles both single line queries and multi line queries. First I check if line contain ";"
it means the line is ended else not ended. Pretty simple it was at the end.
while ((currentLine = insertReader.readLine()) != null) {
if (currentLine.length() != 0) {
if (currentLine.charAt(currentLine.length() - 1) == ';') {
insertInDb(db, insertStr + "\n" + currentLine);
insertStr = "";
} else {
insertStr += currentLine;
}
}
}
private int insertFromFile(SQLiteDatabase db, String assetFilePath, int totalCount) {
int result = 0;
BufferedReader insertReader = null;
try {
InputStream insertStream = context.getAssets().open(assetFilePath);
insertReader = new BufferedReader(new InputStreamReader(insertStream));
db.beginTransaction();
while (insertReader.ready()) {
// String insertStr = insertReader.toString();
String insertStr = "";
String currentLine;
while ((currentLine = insertReader.readLine()) != null) {
if (currentLine.length() != 0) {
if (currentLine.charAt(currentLine.length() - 1) == ';') {
insertInDb(db, insertStr + "\n" + currentLine);
insertStr = "";
} else {
insertStr += currentLine;
}
}
}
}
db.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (insertReader != null) {
try {
insertReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
db.endTransaction();
}
return result;
}
private void insertInDb(SQLiteDatabase db, String assetFilePath) throws IOException {
if (assetFilePath != null && assetFilePath.length() > 0) {
SQLiteStatement statement = db.compileStatement(assetFilePath);
statement.execute();
}
}
I used this technique to insert 4000 records in db. And it was working fine without much of the lag. If anyone have any batter solution for it. Kindly post it.