mercredi 3 février 2016

how to import CSV file in SQLite data in android?

   public class MainActivity extends Activity {
    Context context;
SQLiteDatabase db;
    public static final String CONTACTS_TABLE_NAME = "contacts";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        db.execSQL(
                "create table contacts " +
                        "(id integer primary key, name text,dt1 text,dt2 text, dt3 text,)"

        );

        String mCSVfile = "contacts.csv";
        Log.e("shashank ", "" + mCSVfile);
        AssetManager manager = context.getAssets();
        InputStream inStream = null;
        try {
            inStream = manager.open(mCSVfile);
            Log.e("shashank inStream ", "" + inStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(inStream));
        Log.e("shashank br ", "" + br);
        String line = "";
        String tableName = "contacts";
        String columns = "id, name, phone, email, discipline,place";
        String str1 = "INSERT INTO " + tableName + " (" + columns + ") values(";
        String str2 = ");";
        Log.e("shashank ", "" + str1);
        Log.e("shashank ", "" + str2);


        db.beginTransaction();
        try {
            while ((line = br.readLine()) != null) {
                StringBuilder sb = new StringBuilder(str1);
                String[] str = line.split(",");
                sb.append("'" + str[0] + "',");
                sb.append(str[1] + "',");
                sb.append(str[2] + "',");
                sb.append(str[3] + "'");
                sb.append(str[4] + "'");
                sb.append(str2);
                db.execSQL(sb.toString());
            }
            db.setTransactionSuccessful();
            db.endTransaction();
        } catch (IOException e) {
        }

    }
    }

  • beginner in android. I want to import CSV file and upload in SQLIte database .please can u suggest me.how should I create a database in this class. **

get this linkin this post how to create db.this SQLiteDatabase db;????

Aucun commentaire:

Enregistrer un commentaire