2012-04-24 151 views
3

我有一個從Sqlite瀏覽器導出的csv文件,其中包含一個表格,並且我將它放在了sdcard中。我希望我可以通過編程導入到我的android應用程序數據庫我試了很多 ,但未能取得任何幫助,將不勝感激。我有相同的Tabe在Android中創建,但我不想通過從csv文件逐行讀取內容並執行每次查詢插入...因此主要點是導入是直接將CSV文件導入到Android應用程序Sqlite數據庫

嘿我已經通過這代碼

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()); 
     } 

我有在SD卡文件的地方,我正在查詢的StringBuilder但錯誤或異常都正在添加,並沒有在表中獲取進入,但在調試時我在它的工作sqlite的程序運行相同的查詢 看到我在作出和錯誤,並進一步的建議,將不勝感激

+0

請發表您的logcat的異常 – Simon 2012-04-24 07:41:05

+0

@simon我得到任何異常,但沒有進入 – 2012-04-24 09:49:04

+0

好吧我已經發現問題,我使用的線路不合理mainDatabase.beginT ransaction();刪除線提出了我的問題,謝謝他們的貢獻 – 2012-04-24 10:20:38

回答

2

沒有內置的CSV導入功能可用:您需要逐行讀取CSV文件並插入它。有許多CSV解析程序(我個人使用OpenCSV),可以很好地讀取磁盤上的CSV文件並提取行。如果您在編寫有效插入函數時遇到問題,請提出問題幷包含您的代碼。

+0

謝謝你好,那麼如果我們有一個接一個地插入...還有一件事是有其他的方式來實現這一點 – 2012-04-24 06:13:38

+0

導入是從一種存儲格式(在這種情況下,CSV),並插入到另一個(在這種情況下,您的數據自定義SQLite格式)。在這種情況下,*方法*需要由您手動執行,因爲沒有內置的方法可以描述CSV文件中的行如何轉換爲Android平臺上的自定義SQLite格式。 – Femi 2012-04-24 07:04:19

+0

實際上在sqlite中有導入CSV文件的命令,但我不知道它是否存在於任何庫中。 'sqlite> create table test(id integer,datatype_id integer,level integer,meaning text);' 'sqlite> .separator「,」' 'sqlite> .import no_yes.csv test' [link](http:// www.sqlite.org/cvstrac/wiki?p=ImportingFiles) – kubo 2013-11-01 19:36:58

1

試試這個代碼,

創建資源>原始文件夾,並把你的yourfile.csv文件夾原料。

添加此methos插入在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); 

我希望這個代碼解決您的問題...

+0

你能告訴我你在做什麼Open(), – Sathish 2016-07-25 10:29:38

+0

@Sathish在打開和關閉im打開和關閉數據庫。 – 2016-07-25 10:44:09

+0

@Sathish看到我現在編輯我的答案...... !!! – 2016-07-25 10:46:16

相關問題