【问题标题】:save json in HTML5 Web SQL Database在 HTML5 Web SQL 数据库中保存 json
【发布时间】:2016-03-31 02:14:27
【问题描述】:

我是 HTML5 Web SQL 数据库的新手,我想保存一个 JSON 字符串。

 tx.executeSql("INSERT INTO TA (id, name) VALUES (89,\"{   \"items\":[     {     \"order\": 1,     \"item_id\": 123123,     \"quantity\": 10,     \"price\": 1526896,     \"total\": 15268960   },   {     \"order\": 2,     \"item_id\": 113124,     \"quantity\": 10,     \"price\": 1526896,     \"total\": 15268960   },   {     \"order\": 3,     \"item_id\": 163125,     \"quantity\": 10,     \"price\": 1626896,     \"total\": 16268960   },   {     \"order\": 4,     \"item_id\": 1723165,     \"quantity\": 10,     \"price\": 1726896,     \"total\": 17268960   },   {     \"order\": 5,     \"item_id\": 183190,     \"quantity\": 10,     \"price\": 1826896,     \"total\": 18268960   } ],  \"other\":[           {         \"order\": 1,         \"item\": 123123,         \"price\": 10              },      {         \"order\": 2,         \"item\": 123123,         \"price\": 10              }      ,{         \"order\": 3,         \"item\": 123123,         \"price\": 10      }      ] }\")";

但它不接受引号的值。

我测试了这个样本

tx.executeSql("INSERT INTO TA (id, name) VALUES (90,\"a\")");

还有

tx.executeSql("INSERT INTO TA (id, name) VALUES (90,\"\"a\"\")"); 

得到同样的结果。

【问题讨论】:

  • 错误信息是什么?
  • 不要将 JSON 字符串直接放在查询中。改用占位符和参数:stackoverflow.com/a/23434271/1233508
  • @Karl Richter HTML5 Web SQL 数据库从不打印日志或错误
  • @DCoder 非常感谢您

标签: sql database html web


【解决方案1】:

感谢所有答案,特别是@DCoder

不要将 JSON 字符串直接放在查询中。改用占位符和参数:stackoverflow.com/a/23434271/1233508

var str  = '{   "items":[     {     "order": 1,     "item_id": 123123,     "quantity": 10,     "price": 1526896,     "total": 15268960   },   {     "order": 2,     "item_id": 113124,     "quantity": 10,     "price": 1526896,     "total": 15268960   },   {     "order": 3,     "item_id": 163125,     "quantity": 10,     "price": 1626896,     "total": 16268960   },   {     "order": 4,     "item_id": 1723165,     "quantity": 10,     "price": 1726896,     "total": 17268960   },   {     "order": 5,     "item_id": 183190,     "quantity": 10,     "price": 1826896,     "total": 18268960   } ],  "other":[           {         "order": 1,         "item": 123123,         "price": 10              },      {         "order": 2,         "item": 123123,         "price": 10              }      ,{         "order": 3,         "item": 123123,         "price": 10      }      ] }';

tx.executeSql('INSERT INTO TA (id, name) VALUES (?,?)',[94,str]);

【讨论】:

    猜你喜欢
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-26
    • 2015-08-15
    • 2021-05-14
    • 2012-06-21
    相关资源
    最近更新 更多