Import CSV file into Android Sqlite database

I have a csv file exported from a Sqlite browser that contains a table and I put it in sdcard. I would like that I could import into my database of Android applications programmatically, I tried a lot but could not get any help. I have the same tab created in android, but I don’t want to do it by reading the linear contents from the csv file and doing it every time the request is inserted ... so the main point is import directly

Hey i went 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());
        }

sdcard, StringBuilder, , , , Sqlite-, , ,

+3
2

CSV: CSV . CSV ( OpenCSV), CSV . , .

+2

,

res > raw yourfile.csv .

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();
}

insertCSVData(Activity, getResources().openRawResource(R.raw.yourfile.csv), tableName);

, ...

+1

All Articles