Reputation: 1451
I have a csv file exported from Sqlite browser that contain a table and i have placed it in sdcard . i wish i could import into my android apps data base programmatically I tried a lot but failed to achieve any help would be appreciated . I have the same tabe creatd in android but i dont want to do by reading line by line content from csv file and execute every time query to insert ... so main point is import is directly
hey I have gone through this code
public static void InsertCSVFile(String FilePath, String filename,
String TableName) {
try {
FileReader fr = new FileReader(new File("mnt/sdcard/",
"olinecsv.csv"));
BufferedReader br = new BufferedReader(fr);
String data = "";
String tableName = "Test";
String columns = "ID,NAME,TYPE";
String InsertString1 = "INSERT INTO " + tableName + " (" + columns
+ ") values(";
String InsertString2 = ");";
mainDatabase.beginTransaction();
while ((data = br.readLine()) != null) {
StringBuilder sb = new StringBuilder(InsertString1);
String[] sarray = data.split(",");
sb.append(sarray[0] + ",");
sb.append( sarray[1] + ",");
sb.append(sarray[2]);
sb.append(InsertString2);
if(sarray[0].contains("ID"))
continue;
mainDatabase.rawQuery(sb.toString(), null);
//mainDatabase.execSQL(sb.toString());
}
i have place that file in sdcard and i am getting the query StringBuilder but on error or exception are comming and nothing get entered in the table but ,while debugging the same query i ran in Sqlite programs it work see i am making and mistake and futher suggestion would be appreciated
Upvotes: 3
Views: 8184
Reputation: 440
Try this code,
create a folder in res>raw and put your yourfile.csv file in raw folder.
Add this methos to insert data in sqlite,
public void insertCSVData(Activity activity, InputStream is, String tableName) {
String colunmNames = null, str1 = null;
open();
try {
BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
String line = "";
String str2 = ");";
db.beginTransaction();
int i = 0;
while ((line = buffer.readLine()) != null) {
i++;
if (i == 1) {
colunmNames = line;
str1 = "INSERT INTO " + tableName + " (" + colunmNames + ") values (";
} else {
StringBuilder sb = new StringBuilder(str1);
String[] str = line.split(",");
for (int h = 0; h < str.length; h++) {
if (h == str.length - 1) {
sb.append("'" + str[h] + "'");
} else {
sb.append("'" + str[h] + "',");
}
}
sb.append(str2);
db.execSQL(sb.toString());
}
}
db.setTransactionSuccessful();
db.endTransaction();
} catch (Exception e) {
close();
e.printStackTrace();
}
close();
}
public void open() {
db = this.getWritableDatabase();
}
public void close() {
db.close();
}
Call this Method by
insertCSVData(Activity, getResources().openRawResource(R.raw.yourfile.csv), tableName);
I wish this code solve your problem...
Upvotes: 1
Reputation: 64700
There is no built-in CSV import function available for you: you will need to read the CSV file line by line and insert it. There are a number of CSV parsing programs (I personally use OpenCSV) that will do an excellent job reading from a CSV file on disk and extracting rows. If you have issues writing a valid insert function ask a question and include your code.
Upvotes: 2