2016-02-12 30 views
0

我搜索了一下填充SQLite數據庫,所以我要問在這裏找不到任何東西:從網站

這將會是巨大的,如果有一個教程或示例項目,我可以看一下。到目前爲止,我只找到如何使用本地生成的數據填充數據庫,這是由程序員的輸入。

問題: 我有一個帶有一些數據的.txt網址,如何解析它,將它填充到我將在Android應用程序中創建的SQLite數據庫中?

編輯: 這是格式:

Item1 <newline> 
Description <newline> 
LocalFilePathToPicture1<newline> 
Item2 <newline> 
Description <newline> 
LocalFilePathToPicture2<newline> 
... 
+0

您的數據採用哪種格式?這是一個JSON格式的數據在鏈接或其他東西,請在這個問題明確。 –

+0

@ShreeKrishna我在編輯中添加了格式。一個字符描繪了每一條信息串。瓦片名稱的本地路徑與文件位於同一臺服務器上,並位於同一位置。 –

+0

這裏有一個廣泛的問題。 1)文件的網絡請求2)解析文件3)將分析的信息存儲在sqlite數據庫中。我建議你將你的研究分解成這些單獨的部分 –

回答

0

確定爲您的意見和最新的修改,可以說你的文本文件的URL是這樣的

http://www.example.com/myTextFile.txt 

你可以做什麼像這

StringBuffer myString = new StringBuffer(""); 
try { 
    // Create a URL 
    URL url = new URL("http://www.example.com/myTextFile.txt"); 

    // Read the text 
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 

    String string; 
    while ((string = in.readLine()) != null) { 
     // string is one line of text; readLine() reads the newline 
    myString.append("__"+string); 
    } 
    in.close(); 


} 
catch (Exception e) { 
} 

return myString; 

這裏是你的doInBackground()方法的示例r AsyncTask類您將分割字符串並將它們插入到sqlite中

@Override 
    protected String doInBackground(String... params) { 
     String myString = params[0]; 
     String[] splitedString = myString.split("__"); 

    ContentValues cv=new ContentValues(); 

     cv.put("firstline", splitedString[0]); //where firstline is your column name 
     cv.put("secondline", splitedString[1]); 

    db.insert(yourTable, null, cv); //where db is your instance of writable database 
    db.close(); //Close the connection 
    } 
+0

你錯過了這一事實,這需要一個AsyncTask和整個sqlite的東西 –

+0

我假設我解析字符串並將它們存儲到while循環內的SQLite? –

+0

@ cricket_007然後我們如何編寫所有的sqlite代碼和AsyncTask來插入數據,這只是閱讀文本的指導原則。 –