Import .csv file to Sqlite in Android
Try following code,
FileReader file = new FileReader(fileName);
BufferedReader buffer = new BufferedReader(file);
String line = "";
String tableName ="TABLE_NAME";
String columns = "_id, name, dt1, dt2, dt3";
String str1 = "INSERT INTO " + tableName + " (" + columns + ") values(";
String str2 = ");";
db.beginTransaction();
while ((line = buffer.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();
Android - Import CSV File Into SQLite Database
you must read content of csv file and add content to database.
this is for read from csv file.
edited
you can using this for read any csv file from storage
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
if(requestCode == REQUEST_CODE){
Uri fileUri = data.getData();
Log.i(getClass().getName(), "fileUri" + fileUri);
}
}
}
}
How can I import CSV from phone storage to SQLite?
I post my solution based on @SweetD3v answer, just in case it is useful for someone else.
public void importPlayersTable() throws FileNotFoundException {
SQLiteDatabase db = this.getWritableDatabase();
String filepath = "/sdcard/playersTable.csv";
FileInputStream fs = new FileInputStream(filepath);
BufferedReader buffer = new BufferedReader(new InputStreamReader(fs));
String line = "";
db.beginTransaction();
try {
while ((line = buffer.readLine()) != null) {
String[] colums = line.split(",");
ContentValues cv = new ContentValues();
//cv.put("ID_PLAYER", colums[0].trim());
cv.put("QUE_QUE", colums[1].trim());
cv.put("ANS_QUE", colums[2].trim());
db.insert(MI_TABLA_PLAYERS, null, cv);
}
} catch (IOException e) {
e.printStackTrace();
}
db.setTransactionSuccessful();
db.endTransaction();
}
I also had to add a try-catch in the class that calls the method:
try {
d.importPlayersTable();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Related Topics
Creating a Directory in /Sdcard Fails
Android: Implementing Progressbar and "Loading..." for Endless List Like Android Market
Calling Setcontentview() Multiple Times
Simulate Low Network Connectivity for Android
The Android Emulator Is Not Starting, Showing "Invalid Command-Line Parameter"
Retaining Position in Listview After Calling Notifydatasetchanged
Source of Android's Lock Screen
In Android 7 (API Level 24) My App Is Not Allowed to Mute Phone (Set Ringer Mode to Silent)
How to Find MAC Address of an Android Device Programmatically
How to Scale/Resize Text to Fit a Textview
Android SQLite Issue - Table ... Has No Column Named
Full Screen Dialogfragment in Android
Adt Requires 'Org.Eclipse.Wst.Sse.Core 0.0.0' But It Could Not Be Found
Converting Yuv->Rgb(Image Processing)->Yuv During Onpreviewframe in Android