Save data to SQLite from json object using Hashmap in Android

Hello everyone I am new to Android, I need to parse a json object and display it in a listView and at the same time store this data in SQLite, but whenever I run a project exception, you end up ... a database is created without any data or values.

here is the code I'm trying:

Bundle bundle = getIntent().getExtras();
        String rfiItems = bundle.getString("allData");
        final ArrayList<HashMap<String, String>> mylist4 = new ArrayList<HashMap<String, String>>();

        try{
            JSONObject jObj = new JSONObject(rfiItems);
            JSONArray data4 = jObj.getJSONArray("data");
            //data4 = json4.getJSONArray("data");
                //Toast.makeText(getApplicationContext(), data4.toString(), Toast.LENGTH_LONG).show();

                    for(int i=0;i<data4.length();i++){                      
                        HashMap<String, String> map = new HashMap<String, String>();    
                        JSONObject e = data4.getJSONObject(i);

                        map.put("id", String.valueOf(i));
                        map.put("rfi_data1", "" + e.getString("country"));                          
                        map.put("rfi_data4", "" + e.getString("state"));
                        map.put("rfi_data5", "" + e.getString("city"));
                        map.put("rfi_data6", "" + e.getString("name"));
                        map.put("rfi_data7", "" + e.getString("about"));


                        DatabaseHelper databaseHelper = new DatabaseHelper(this);
                        SQLiteDatabase db = databaseHelper.getWritableDatabase();
                        ContentValues cv = new ContentValues();
                        cv.put(DatabaseHelper.COUNTRY, e.getString("country"));
                        cv.put(DatabaseHelper.CITY, e.getString("state"));
                        cv.put(DatabaseHelper.STATE, e.getString("city"));
                        cv.put(DatabaseHelper.NAME, e.getString("name"));
                        cv.put(DatabaseHelper.ABOUT, e.getString("about"));

                        db.insert("Baranzan", DatabaseHelper.COUNTRY, cv);
                        db.close();
                        mylist4.add(map);
                    }       
                }catch(JSONException e)        {
                    Log.e("log_tag", "Error parsing data "+e.toString());
                }

                ListAdapter adapter4 = new SimpleAdapter(this, mylist4 , R.layout.item_list4,
                                          new String[] { "rfi_data1", "rfi_data4","rfi_data5","rfi_data6","rfi_data7"}, 
                                     new int[] { R.id.rfi_item_type, R.id.rfi_status,R.id.rfi_title,R.id.rfi_change_date,R.id.rfi_responded_date });
                                          setListAdapter(adapter4); 

      

and the databaseclass code:

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "myonlydb.db";
    public static final String ID = "id";
    public static final String COUNTRY = "country";
    public static final String STATE = "state";
    public static final String CITY = "city";
    public static final String NAME = "name";
    public static final String ABOUT = "about";


    public static final String TAG = "Form"; // optional

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE mytable (_id INTEGER PRIMARY KEY AUTOINCREMENT, country TEXT, state TEXT, city TEXT, name TEXT, about TEXT, );");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        android.util.Log.v("mytable", "Upgrating database will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS mytable");
        onCreate(db);

    }

      

Any help would be really appreciated, THANKS A LOT

0


source to share


3 answers


I'm not sure what you are doing wrong, but I can help you with some debugging.

First, where do you announce your table? Is this consistent with your data to be inserted?



In the for loop where you create your ContentValue and insert the call, try to see which insert integer is returned, is it a line number or id that cannot be remembered. But it is -1 if not inserted. If you get it, the problem is probably some type of problem. Trying to store the string as int or ...?

Another great tool you can use is the sqlite3 command to check the structure of your database and tables. Sqlite3 is not installed on an android device, so use an emulator for that, or read this how to get it to work on a device: How to install sqlite3

0


source


this line is wrong

db.insert("Baranzan", DatabaseHelper.COUNTRY, cv);

      

try

db.insert(DatabaseHelper.COUNTRY, null, cv);

      



if you caught an exception here

catch(JSONException e)        {
                    Log.e("log_tag", "Error parsing data "+e.toString());
                }

      

then you should provide a log-katan log for more information

0


source


edit this line

db.insert ("Baranzan", DatabaseHelper.COUNTRY, cv);

to

db.insert ("mytable", null, cv);

0


source







All Articles