java - 为什么读取文件时多行没有保存在数据库中?

标签 java android

我这里有这个 fragment :

    try {

        Uri uri = Uri.parse("file:///storage/emulated/0/Download/information.csv");
        File file = new File(uri.getPath());

        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        boolean firstLine = true;

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

            System.out.println("Print the contents from the file :" + line);

            if (firstLine) {
                firstLine = false;
                continue;

            } else {

                    //deserialize the string to Object
                    TransferData tdBack = TransferData.fromString(line));

                    //Create a new map of values, where column names are the keys
                    ContentValues values = new ContentValues();

                    for (Collection collection : tdBack.getCollections()) {

                        values.put("reminders", collection.getReminders());
                        values.put("region", collection.getRegion().getId());
                        values.put("collection", collection.getCollection());
                        values.put("collection_interval", collection.getCollectionInterval().getId());

                    }

                    //insert the new row
                    sqLiteDatabase.insert(COLLECTION_TABLE, null, values);
                    Cursor cursor = sqLiteDatabase.rawQuery("Select * FROM collection", null);
                    Log.d("MainActivity", DatabaseUtils.dumpCursorToString(cursor));

但是,即使我的文件中至少有两行数据,它也只保存文件中的第一行信息。我不知道为什么它不读取我的整个文件并将两行数据保存到数据库中?

最佳答案

insert() 移动到迭代集合的 for 循环中。

当前您仅插入循环最后一次迭代中的值。

关于java - 为什么读取文件时多行没有保存在数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26720190/

相关文章:

java - android studio更新到3.0.1版本后报错

java - 缓冲区溢出 (vs) 缓冲区溢出 (vs) 堆栈溢出

java - 是否可以以编程方式将 Eclipse RCP 按钮设置为 'checked' ?

java - 错误 ORA-01000 : maximum open cursors exceeded

java - 将 T4CConnection 转换为 OracleConnection 时出错

java - 我不明白监听器中 lambda 表达式的参数传递

java - 从 ArrayList 中删除具有相似值的对象

android - 为什么在 Android 中使用 sqlite 数据库?

JavaFX 2 组合框在 win xp 中不起作用

java - 使用倒数计时器