3

我有一个从 Sqlite 浏览器导出的 csv 文件,其中包含一个表,我已将其放在 sdcard 中。我希望我可以以编程方式导入我的 android 应用程序数据库我尝试了很多但未能获得任何帮助将不胜感激。我在android中有相同的tabe creatd,但我不想通过从csv文件中逐行读取内容并执行每次查询插入......所以重点是直接导入

嘿,我已经浏览了这段代码

public static void InsertCSVFile(String FilePath, String filename,
        String TableName) {
    try {
        FileReader fr = new FileReader(new File("mnt/sdcard/",
                "olinecsv.csv"));
        BufferedReader br = new BufferedReader(fr);
        String data = "";
        String tableName = "Test";
        String columns = "ID,NAME,TYPE";
        String InsertString1 = "INSERT INTO " + tableName + " (" + columns
                + ") values(";
        String InsertString2 = ");";

        mainDatabase.beginTransaction();
        while ((data = br.readLine()) != null) {
            StringBuilder sb = new StringBuilder(InsertString1);
            String[] sarray = data.split(",");
            sb.append(sarray[0] + ",");
            sb.append( sarray[1] + ",");
            sb.append(sarray[2]);
            sb.append(InsertString2);
            if(sarray[0].contains("ID"))
                continue;
            mainDatabase.rawQuery(sb.toString(), null);
            //mainDatabase.execSQL(sb.toString());
        }

我已将该文件放在 sdcard 中,并且我正在获取查询 StringBuilder,但出现错误或异常时,表中没有输入任何内容,但是,在调试我在 Sqlite 程序中运行的相同查询时,它可以正常工作,看到我正在犯错误和进一步建议将不胜感激

4

2 回答 2

2

There is no built-in CSV import function available for you: you will need to read the CSV file line by line and insert it. There are a number of CSV parsing programs (I personally use OpenCSV) that will do an excellent job reading from a CSV file on disk and extracting rows. If you have issues writing a valid insert function ask a question and include your code.

于 2012-04-24T06:04:54.917 回答
1

试试这个代码,

在 res>raw 中创建一个文件夹,并将 yourfile.csv 文件放在 raw 文件夹中。

添加此方法以在 sqlite 中插入数据,

  public void insertCSVData(Activity activity, InputStream is, String tableName) {

    String colunmNames = null, str1 = null;
    open();

    try {

        BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
        String line = "";

        String str2 = ");";

        db.beginTransaction();

        int i = 0;
        while ((line = buffer.readLine()) != null) {
            i++;
            if (i == 1) {
                colunmNames = line;
                str1 = "INSERT INTO " + tableName + " (" + colunmNames + ") values (";
            } else {

                StringBuilder sb = new StringBuilder(str1);
                String[] str = line.split(",");

                for (int h = 0; h < str.length; h++) {

                    if (h == str.length - 1) {
                        sb.append("'" + str[h] + "'");
                    } else {
                        sb.append("'" + str[h] + "',");
                    }
                }

                sb.append(str2);
                db.execSQL(sb.toString());
            }
        }

        db.setTransactionSuccessful();
        db.endTransaction();

    } catch (Exception e) {

        close();

        e.printStackTrace();
    }

    close();
}
   public void open() {
    db = this.getWritableDatabase();
}

public void close() {
    db.close();
}

调用此方法

insertCSVData(Activity, getResources().openRawResource(R.raw.yourfile.csv), tableName);

我希望这段代码能解决你的问题......

于 2016-05-20T06:54:58.133 回答