4

I am new on react native. I am facing issue when storing json data into database table. I tried to store a json object into table but got error.

Here is my json data:

let formData = {"inspection":{
    "date":"23-11-2018",
    "client name" :"John Doe",
    "locations":[
        {
            "location_id":"23",
            "scoreSheets":[
              {
               "name":"EHA",
               "categories":[
                  {
                    "id":"1",
                    "observations":[
                        {
                         "option_id":"56",
                         "option":"lorem ipsum dolor",
                         "marks":"2"
                        }
                    ],
                   "violations":[
                       {
                         "option_id":"59",
                         "option_4":"this is label of option 4",
                         "marks":"-2",
                         "correctiveActions":[
                            {
                              "comment":"lorem ipsum",
                              "image":"errror1.jpg"
                            },
                            {
                              "comment":"dolor set amet",
                              "image":"errror2.jpg"
                            }
                         ]
                       }
                   ],
                   "subscore":"4"
                  }
                ],
              "foodInspections":[
                {
                  "collection_site":"3",
                  "hazardousItems":"4",
                  "non-compliant_food":"lorem ipsum",
                  "potential_cause":"lorem ipsum"
                }
              ],
              "refrigerations":[
                 {
                   "unit_identification":"lskdre",
                   "unit temprature":"34"
                 }
              ],
          "totalScore":"75"
            }
        ]

        }
    ],
    "finalScore":"100"
}}; 

Here is my code :

db = SQLite.openDatabase(database_name, database_version, database_displayname, database_size, this.openCB, this.errorCB);
 db.transaction((tx) => {
    tx.executeSql('CREATE TABLE IF NOT EXISTS Inspections( '
     + 'id INTEGER PRIMARY KEY NOT NULL, '
     + 'inspector_id INTEGER NOT NULL, '
     + 'inspection_data TEXT ); ', [], this.successCB, this.errorCB);

     tx.executeSql('INSERT INTO Inspections (inspector_id, inspection_data) VALUES ('+decoded.uid+','+formData+');', []);

});

I want to insert json data in inspection table. I also used with json.strigify() function but not getting any solution.

1 Answer 1

2

Try this:

 db = SQLite.openDatabase(database_name, database_version, database_displayname, database_size, this.openCB, this.errorCB);
 db.transaction((tx) => {
    tx.executeSql('CREATE TABLE IF NOT EXISTS Inspections( '
     + 'id INTEGER PRIMARY KEY NOT NULL, '
     + 'inspector_id INTEGER NOT NULL, '
     + 'inspection_data TEXT ); ', [], this.successCB, this.errorCB);

     tx.executeSql('INSERT INTO Inspections (inspector_id, inspection_data) VALUES (?,?);', [decoded.uid,formData]);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.