我正在研究android上的应用程序,我目前需要从Web服务器提取数据并将其存储到db(sqlite db file)文件中,应用程序将进一步使用该文件向用户显示数据。我不知道我需要编写一个Web服务,它将从服务器为我提取数据,但我完全不知道如何将它存储在SQLite db文件中。
Uptil现在可以在ArrayList<HashMap<String, String>>InfoList;
中从服务器获取数据
现在我想存储ArrayList<HashMap<String, String>>InfoList; => to sqLite database
的所有数据,这些数据稍后将由应用程序在本地使用。如何才能做到这一点。
要求所有专家指导我。提前谢谢。
答案 0 :(得分:0)
你可以这样做,(xml解析并存储在db中)
SAXParserFactory saxPF = SAXParserFactory.newInstance();
SAXParser saxP = saxPF.newSAXParser();
XMLReader xmlR = saxP.getXMLReader();
URL url = new URL("your url"); // URL of the XML
XMLHandler myXMLHandler = new XMLHandler();
xmlR.setContentHandler(myXMLHandler);
xmlR.parse(new InputSource(url.openStream()));
data = XMLHandler.data;
db.execSQL("create table if not exists entries(subject_name varchar,subject_id varchar)");
for (int i = 0; i < data.getSubname().size(); i++) {
String subname=data.getSubname().get(i);
String subid=data.subid().get(i);
db.execSQL("insert or replace into entries values('"+subname+"','"+subid+"')");
}
}
为了检索数据,我们使用游标。
编辑: 这个问题已经解决了,您可以参考Insert JSON data into the SQLite database in android?
答案 1 :(得分:0)
我认为您应该使用JSON作为Web服务的输出格式。不是xml,因为很难在js中解析。
在javascript中使用JSON后,您可以使用sql.js https://github.com/kripken/sql.js 这是SQLite的javascript api。用它来将你的json导入sqlite。