【问题标题】:Android insert data in Db from file which have multiple lines for each Insert queryAndroid 在 Db 中从文件中插入数据,每个插入查询都有多行
【发布时间】:2016-07-22 07:12:48
【问题描述】:

我想预先填充我的数据库。所以我从浏览器生成 SQL 查询并编写代码将它们插入到数据库中。它适用于单行查询,但大多数插入查询都有多行,例如 `

INSERT INTO `poem` VALUES (780,'سلام القلب واشواقه 

من قلب يحب مجنونه 

ياليت هالقلب يحن 


ويقول هالدنيا 

ماتسوى دون *_*','0',NULL);`

我的要求是按原样插入它们。这就是我为多行编写的方法(非常适合单行查询)

public static int countLines(InputStream is) throws IOException {
    try {
        byte[] c = new byte[1024];
        int count = 0;
        int readChars = 0;
        boolean empty = true;
        int index = 0;

        while ((readChars = is.read(c)) != -1) {
            empty = false;
            String temp = new String(c);
            for (int i = 0; i < readChars; i++) {

                if (c[i] == ';' && index == 0) {
                    index++;
                } else if (c[i] == '\n' && index == 1) {
                    index++;
                } else if (c[i] == '\t' && index == 2) {
                    index++;
                } else if (c[i] == 'I' && index == 3) {
                    count++;
                    index = 0;
                } else {
                    i -= index;
                    index = 0;
                }
            }
        }
        return (count == 0 && !empty) ? 1 : count + 1;
    } finally {
        is.close();
    }
}

任何人都知道如何将此类查询从文件插入到数据库中。? 任何帮助都会很棒。谢谢

【问题讨论】:

标签: java android sqlite android-assets


【解决方案1】:

你问的是一种丑陋的做事方式。使用sqliteassethelper 创建预填充的数据库和表。它的工作原理与 SQLiteOpenHelper 非常相似,因此非常易于使用。

【讨论】:

  • 不是很丑。但这是要求。客户希望这样。同时感谢您的帮助,我已经解决了我的问题。
  • 如果你已经解决了,那就把解决方案粘贴到这里。它可能会帮助其他人
【解决方案2】:

抱歉,如果有人遇到相同问题,请及时回复

这里是一步一步的解决方案

我创建了一个 .sql 文件,其中包含插入查询(一些包含多行查询)以 : 结尾。

SQLiteOpenHelperonCreate() 方法中,我使用getFileLineCount() 计算文件的行数。此方法返回我的行数。这个行数从未使用过,但我有它,因为如果你所有的查询都是单行的,它可能会起作用。您可以跳过此方法。

 public void onCreate(SQLiteDatabase db) {
             db.execSQL("CREATE TABLE \"" + TABLE_POEM + "\" (\"" + POEM_LOCAL_ID + "\" INTEGER PRIMARY KEY AUTOINCREMENT , \"" + POEM_TEXT + "\" TEXT,\"" + POEM_ID + "\" INTEGER , \"" + POEM_IS_FAV + "\" TEXT);");

            int totalLineCount = getFileLineCount("poem.sql");
            int insertCount = insertFromFile(db, "poem.sql", totalLineCount);

            Log.d("DatabaseHelper", "------------onCreate(SQLiteDatabase db) >>>>>>> completed--------");
        }
    private int getFileLineCount(String assetFilePath) {
        InputStream insertStream = null;
        int totalCount = 0;
        try {
            insertStream = context.getAssets().open(assetFilePath);
            totalCount = FileUtil.countLines(new BufferedInputStream(insertStream));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (insertStream != null) {
                try {
                    insertStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return totalCount;
    }

通过使用这种方法,我从文件中插入查询。它处理单行查询和多行查询。首先,我检查行是否包含 ";" 这意味着该行已结束,否则未结束。最后很简单。

while ((currentLine = insertReader.readLine()) != null) {
                            if (currentLine.length() != 0) {
                                if (currentLine.charAt(currentLine.length() - 1) == ';') {
                                    insertInDb(db, insertStr + "\n" + currentLine);
                                    insertStr = "";
                                } else {
                                    insertStr += currentLine;
                                }
                            }
                        }




 private int insertFromFile(SQLiteDatabase db, String assetFilePath, int totalCount) {
            int result = 0;
            BufferedReader insertReader = null;
            try {
                InputStream insertStream = context.getAssets().open(assetFilePath);
                insertReader = new BufferedReader(new InputStreamReader(insertStream));

                db.beginTransaction();

                while (insertReader.ready()) {

                    // String insertStr = insertReader.toString();

                    String insertStr = "";
                    String currentLine;
                    while ((currentLine = insertReader.readLine()) != null) {
                        if (currentLine.length() != 0) {
                            if (currentLine.charAt(currentLine.length() - 1) == ';') {
                                insertInDb(db, insertStr + "\n" + currentLine);
                                insertStr = "";
                            } else {
                                insertStr += currentLine;
                            }
                        }
                    }

                }
                db.setTransactionSuccessful();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (insertReader != null) {
                    try {
                        insertReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                db.endTransaction();
            }

            return result;
        }

        private void insertInDb(SQLiteDatabase db, String assetFilePath) throws IOException {
            if (assetFilePath != null && assetFilePath.length() > 0) {
                SQLiteStatement statement = db.compileStatement(assetFilePath);
                statement.execute();

            }

        }

我使用这种技术在数据库中插入了 4000 条记录。它运行良好,没有太多滞后。如果有人有任何面糊解决方案。请张贴。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多