json - 如何在反应原生 sqlite 存储中存储 json 对象?
问题描述
我是反应原生的新手。将 json 数据存储到数据库表中时遇到问题。我试图将 json 对象存储到表中,但出现错误。
这是我的json数据:
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"
}};
这是我的代码:
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+');', []);
});
我想在检查表中插入 json 数据。我还使用了 json.strigify() 函数,但没有得到任何解决方案。
解决方案
尝试这个:
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]);
});
推荐阅读
- c# - 有没有办法使用 Windows 身份验证以其他用户身份注销和登录?【ASP.NET Core 3.1 MVC】
- python - 在 Flask 文档教程中面临数据库创建问题 Nonetype object has no attribute executescript
- android - 如何增加 Android 工具栏导航按钮的触摸目标大小?
- apache-kafka - 哪个 Path 适合存储 Kafka 的主题数据和日志?
- jupyter-notebook - 无法打开具有特定内核的新笔记本
- javascript - 如何在 React Native 中隐藏标题 - 功能组件
- c# - 将布尔数组转换为计数器
- c# - 如何避免消息进入“deadletter”队列 azure 服务总线
- python - 为什么python在opencv中为对象检测抛出断言错误?
- javascript - Google Analytics Gtag JS:设置自定义维度的代码不起作用?