如何将 CSV 从 phone 存储导入到 SQLite?
How can I import CSV from phone storage to SQLite?
我有一种方法可以将 CSV 文件从文件夹“assets”导入到 SQLite table。
CSV 包含很多这样的行:France;Paris
但我不想从“assets”文件夹中获取 CSV 文件,我想修改该方法以从 phone 存储文件夹中导入 CSV 文件(/storage/emulated/0)但我不知道我必须改变什么才能实现它。有人可以帮帮我吗?提前致谢。
这是我要修改的方法:
public void importPlayersTable(Context context){
SQLiteDatabase db = this.getWritableDatabase();
AssetManager manager = context.getAssets();
InputStream inStream = null;
try {
inStream = manager.open("playersTable.csv");
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader buffer = new BufferedReader(new InputStreamReader(inStream));
String line = "";
db.beginTransaction();
try {
while ((line = buffer.readLine()) != null) {
String[] colums = line.split(";");
ContentValues cv = new ContentValues();
cv.put("QUE_QUE", colums[0].trim());
cv.put("ANS_QUE", colums[1].trim());
db.insert(MI_TABLA_PLAYERS, null, cv);
}
} catch (IOException e) {
e.printStackTrace();
}
db.setTransactionSuccessful();
db.endTransaction();
}
您可以使用 FileReader 导入 .csv 文件,然后将其保存到 SQLite。
检查此方法。
private void csvToSQLite(String filePath){
FileInputStream fs = new FileInputStream(filePath);
BufferedReader buffer = new BufferedReader(new InputStreamReader(fs));
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();
}
我 post 我的解决方案基于 @SweetD3v 的回答,以防万一它对其他人有用。
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();
}
我还必须在 class 中添加一个调用方法的 try-catch:
try {
d.importPlayersTable();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
我有一种方法可以将 CSV 文件从文件夹“assets”导入到 SQLite table。
CSV 包含很多这样的行:France;Paris
但我不想从“assets”文件夹中获取 CSV 文件,我想修改该方法以从 phone 存储文件夹中导入 CSV 文件(/storage/emulated/0)但我不知道我必须改变什么才能实现它。有人可以帮帮我吗?提前致谢。
这是我要修改的方法:
public void importPlayersTable(Context context){
SQLiteDatabase db = this.getWritableDatabase();
AssetManager manager = context.getAssets();
InputStream inStream = null;
try {
inStream = manager.open("playersTable.csv");
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader buffer = new BufferedReader(new InputStreamReader(inStream));
String line = "";
db.beginTransaction();
try {
while ((line = buffer.readLine()) != null) {
String[] colums = line.split(";");
ContentValues cv = new ContentValues();
cv.put("QUE_QUE", colums[0].trim());
cv.put("ANS_QUE", colums[1].trim());
db.insert(MI_TABLA_PLAYERS, null, cv);
}
} catch (IOException e) {
e.printStackTrace();
}
db.setTransactionSuccessful();
db.endTransaction();
}
您可以使用 FileReader 导入 .csv 文件,然后将其保存到 SQLite。 检查此方法。
private void csvToSQLite(String filePath){
FileInputStream fs = new FileInputStream(filePath);
BufferedReader buffer = new BufferedReader(new InputStreamReader(fs));
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();
}
我 post 我的解决方案基于 @SweetD3v 的回答,以防万一它对其他人有用。
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();
}
我还必须在 class 中添加一个调用方法的 try-catch:
try {
d.importPlayersTable();
} catch (FileNotFoundException e) {
e.printStackTrace();
}