I want to write database from txt to database if it does not exist:
File database=getApplicationContext().getDatabasePath("commments.db");
if (!database.exists()) {
// Database does not exist so copy it from assets here
try {
txtToDb();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("Database", "Not Found");
} else {
Log.i("Database", "Found");
}
It's not working, and the database gets filled over and over again. What am I doing wrong?
UPDATE: txtToDb():
public void txtToDb () throws IOException {
InputStream is =getResources().openRawResource(R.raw.data);
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
byte[] myData = baf.toByteArray();
String dataInString = new String(myData);
String[] lines = dataInString.split("\n");
for (int i=0; i<lines.length; i++){
comment = datasource.createComment(lines[i]);
// adapter.add(comment);
}
writeDbToSd();
}
DbToSd() writes DB to SDCARD so that I can use adb pull to view it. Both functions work fine on their own. createComment adds parsed string to database in another function.