+
+
+ Cursor c = db.rawQuery("select id from tags limit 2", null);// do zmiany
+ Log.d("db", c.getCount()+"");
+
+ /* this is memory consuming and should be optimized. it is first-run only, though */
+ if(c.getCount() == 0) {
+ AssetManager assetManager = getAssets();
+ try {
+ Log.d("assets", assetManager.list("/assets").length+"");
+ for(String s: assetManager.list("/assets")){
+ Log.d("assets", s);
+ }
+ InputStream is = assetManager.open("catalogue.jpg", AssetManager.ACCESS_BUFFER);
+ String catalogueString = readInputStreamAsString(is);
+ JSONObject jObject = null;
+ try {
+ //new JSONOBj
+ jObject = new JSONObject(catalogueString);
+ } catch (JSONException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ Log.d("assets", catalogueString.length()+"");
+ JSONObject added = jObject.getJSONObject("added");
+ JSONArray books = added.getJSONArray("books"); // books
+ JSONArray tags = added.getJSONArray("tags"); // tags
+
+ Log.d("json array books length", books.length()+"");
+ Log.d("json array tags length", tags.length()+"");
+ Log.d("json array length", added.length()+"");
+
+ for(int i = 0; i < books.length(); i++){
+ JSONObject book = books.getJSONObject(i);
+ Log.d("json-test", book.get("id").toString() + book.get("title").toString() + book.get("slug").toString());
+ // JSONException
+ ContentValues bookMap = new ContentValues();
+ bookMap.put("id", book.get("id").toString());
+ bookMap.put("title", book.get("title").toString());
+ bookMap.put("slug", book.get("slug").toString());
+ bookMap.put("epub", "epub");
+ bookMap.put("html", "html");
+ bookMap.put("mp3", "mp3");
+
+ long id = db.insert("books", null, bookMap);
+ Log.d("sql", id+"");
+ }
+
+ for(int i = 0; i < tags.length(); i++){
+ JSONObject tag = tags.getJSONObject(i);
+ Log.d("json-test", tag.get("id").toString() + tag.get("name").toString() + tag.get("slug").toString()+ tag.get("sort_key").toString()+ tag.get("category").toString());
+ // JSONException
+
+ //String bookData[] = {book.get("id").toString(), book.get("title").toString(), book.get("slug").toString()};
+ ContentValues tagMap = new ContentValues();
+ tagMap.put("id", tag.get("id").toString());
+ tagMap.put("name", tag.get("name").toString());
+ tagMap.put("slug", tag.get("slug").toString());
+ tagMap.put("sort_key", tag.get("sort_key").toString());
+ tagMap.put("category", tag.get("category").toString());
+ tagMap.put("description", "description");
+ long id = db.insert("tags", null, tagMap);
+ Log.d("sql", id+"");
+ }
+
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (JSONException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+