android sqlite 添加多个表,Android的 - 导入多个.CSV文件在SQLite数据库的多个表(Android - Impo...

我在我的Android设备有多个.CSV文件的文件夹。

我想所有的人都导入到我的SQLite数据库,但每个文件必须是不同的表。

所有的.csv文件中简单。 他们只有一个列。

例:

FILE.CSV

12345

123

00000000

AnotherFile.CSV

XXXXX

ZZZZZZZZZZ

FFFF

这里是我的方法,它是行不通的。 我不明白为什么:

@TargetApi(Build.VERSION_CODES.M)

public void importaTabelas() {

//Check the read permission

if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

try {

//Check if the folder exists

File importDir = new File (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/ENEL/IMPORTADOS/");

if (!importDir.exists())

{

importDir.mkdirs();

}

//Read all file names

for (File f : importDir.listFiles()) {

if (f.isFile()) {

//Put the files names into variable nomeArq

nomeArq = f.getName();

//Take off the file extension .csv

if (nomeArq.indexOf(".") > 0)

nomeArq = nomeArq.substring(0, nomeArq.lastIndexOf("."));

SQLiteDatabase db = this.banco.getWritableDatabase();

try {

//Create table with the name of the .csv file

String criaTab = "CREATE TABLE IF NOT EXISTS " + nomeArq + " (id integer PRIMARY KEY AUTOINCREMENT, codigo varchar (50))";

db.execSQL(criaTab);

db.close();

} catch (SQLException e) {

e.printStackTrace();

}

//String for the file location

String fn = importDir + "/" + nomeArq + ".csv";

//Reads the file

FileReader fileReader = new FileReader(fn);

BufferedReader buffer = new BufferedReader(fileReader);

//ContentValues contentValues = new ContentValues();

String line = "";

//db.beginTransaction();

while ((line = buffer.readLine()) != null) {

//String[] colums = line.split("\t");

//String[] colums = line.split(";");

Toast.makeText(this, line, Toast.LENGTH_SHORT).show();

//contentValues.put("codigo", line);

//db.insert(nomeArq, null, contentValues);

db.execSQL("INSERT INTO " + nomeArq + " (codigo) VALUES ('" + line + "')");

}

//db.setTransactionSuccessful();

//db.endTransaction();

}

}

} catch (Exception e) {

Toast.makeText(this, "Catch!", Toast.LENGTH_SHORT).show();

e.printStackTrace();

}

}

else {

requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);

}

}

你能帮我做它的工作原理? 谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值