使用JavaScript在SQLite中存储数组问题 [英] Issue storing arrays in SQLite using JavaScript

查看:307
本文介绍了使用JavaScript在SQLite中存储数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Cordova和SQLite的存储适配器。当我使用 JSON.stringify 存储对象并检索他们工作。我也有几个字段是数组,但是当我存储它们,它们只是存储逗号分隔。如果数组中只有一个项,则它只存储为一个字符串。

I've been using Cordova and the storage adapter for SQLite. When I store objects using JSON.stringify and the retrieve them they work. I also have several fields that are arrays, but when I store them they are just stored comma-delimited. If there is only one item in the array then it's just stored as a single string.

初始数组:

["test1", "test2", "test3"]

已储存为:

test1, test2, test3

当我从数据库中检索这些字段时,我可以拆分这些字段,但仍然想知道是否有更好的方法来存储它们。有没有一种优雅的方式来存储数组和使用JavaScript在SQLite中检索它?

I could just split those specific fields when I retrieve them from the database, but still wondering if there is a better way to store them. Is there a elegant way to store an array and retrieve it in SQLite using JavaScript?

推荐答案

我没有看到任何问题,您可以在解析后访问初始数组(JSON.parse(yourStringifiedArrayString))。

I don't see any problem ,you can access initial array after parsing (JSON.parse(yourStringifiedArrayString)).

  var yourarr=["test1", "test2", "test3"];
  var afterStringify = JSON.stringify(yourarr);
  console.log('After Stringify:'+afterStringify);
  var prevArr=JSON.parse(afterStringify);
  console.log('After Parsing:'+prevArr);
  //now access your array
   alert(prevArr[1]);//will show test2


b $ b

检查小提琴

这篇关于使用JavaScript在SQLite中存储数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆