Search code examples
androiddatabasesqlitecsvsqliteopenhelper

Android database initialization with csv


I do the database,on my android application,and I want to initialize it with the value derived from a csv. here is my code :

@Override
public void onCreate(SQLiteDatabase database) {
    executeSQLScript(database, "bdd.sql");
    addComercialCsv(database,"commerciaux.csv");
}

public void addComercialCsv(SQLiteDatabase database, String filename){
    int id,code = 0,idAdress= 0, cp= 0, nom= 0, tel= 0, mail= 0, cplt1= 0, cplt2= 0, rue= 0, localite= 0, ville= 0, pays= 0;
    String insertAdresse = "INSERT INTO Adresse (cplt1,cplt2,rue,localite,cp,ville,pays) values(";
    String insertComm = "INSERT INTO Commercial (code,nom,prenom,tel,mail,adresse) values (";
    String close = ");";
    String prenom = "", noms = "";
    AssetManager assetManager = context.getAssets();

    InputStreamReader is = null;
    try {
        is = new InputStreamReader(assetManager.open(filename));
    } catch (IOException e) {
        e.printStackTrace();
    }

    BufferedReader reader = new BufferedReader(is);
    String line = null;
    try {
        line = reader.readLine();
    } catch (IOException e) {
        e.printStackTrace();
    }
    String[] header = line.split(";");

    int cpt = 0;
    for(String name : header){

        if (name.equals("Commercial")){
            code = cpt;
        }else if (name.equals("Nom")){
            nom = cpt;
        }else if (name.equals("Code postal")){
            cp = cpt;
        }else if (name.equals("Ville")){
            ville = cpt;
        }else if (name.equals("Adresse complement 1")){
            cplt1 = cpt;
        }else if (name.equals("Adresse complement 2")){
            cplt2 = cpt;
        }else if (name.equals("Rue")){
            rue = cpt;
        }else if (name.equals("Localite")){
            localite = cpt;
        }else if (name.equals("Telephone")){
            tel = cpt;
        }else if (name.equals("Mail")){
            mail = cpt;
        }else if (name.equals("Pays")){
            pays = cpt;
        }
        cpt += 1;
    }
    database.beginTransaction();
    try {
        while ((line = reader.readLine()) != null) {

            StringBuilder insertAdres = new StringBuilder(insertAdresse);
            String[] str = line.split(";");
            insertAdres.append("'"+str[cplt1]+"',");

            insertAdres.append("'"+str[cplt2]+"',");
            insertAdres.append("'"+str[rue]+"',");
            insertAdres.append("'"+str[localite]+"',");
            insertAdres.append("'"+str[cp]+"',");
            insertAdres.append("'"+str[ville]+"',");
            insertAdres.append("'"+str[pays]+"'");
            insertAdres.append(close);

            System.out.println(insertAdres);
            database.execSQL(insertAdres.toString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    database.endTransaction();
}

the problem is that my database does not create it when I execute my function "addCommercialCsv" and when I do not use this function my database is created.

what is the problem ?


Solution

  • You start a transaction with beginTransaction() but never set it as successful with setTransactionSuccessful(). Without it endTransaction() rolls back the changes instead of committing them.

    Also note that onCreate(SQLiteDatabase) is already running within a transaction and you should not start a nested transaction with the database passed in as an argument to onCreate().