在MySQL表中保存JSON字符串(来自画布)

问题描述 投票:0回答:1

我想在MySQL表中保存一个json字符串。 json字符串来自画布(使用fabricjs)。

var jsonCanvas = JSON.stringify(canvas.toDatalessObject());

我的第一个问题是,在MySQL中保存json字符串的最佳数据类型是什么? Canvas将包含图像以及文本和其他对象。你认为MEDIUMTEXT就够了吗?

另一个问题。是否可以使用此预准备语句保存json字符串。我怀疑使用关键字“s”作为字符串。

try {
  $sql = "insert into layout (lt_author, lt_date, lt_category, lt_json_string)
   values (?,?,?,?)";
  $stmt = $mysqli->prepare($sql);
  $stmt->bind_param("ssss",$lt_author, $lt_date, $lt_category, $lt_json_string);
  $stmt->execute();
  $stmt->close();

谢谢你的帮助:) Greetings Max

php mysql json prepared-statement fabricjs
1个回答
0
投票

我假设你的SQL是正确的。 我总是选择TEXT来存储我的画布的json字符串表示。 但在选择如何保存JSON表示之前,有一些事情需要考虑。 在这里看到这个讨论:Loading JSON in canvas with fabric.js 它解释了你需要观察的要点。

  • 使用.toDatalessObject()而不是.toJson()方法:
  • - Pros: As kangax explained in the discussion, toDatalessObject will save just the url source of your svg, so, less data to be saved in your database. By this way, you can maybe use other type to store your json canvas representation (maybe you can use a TINYTEXT type)
    - Cons: You need have the files in your server, because you will reference it when load the canvas again.

  • 画布中有多少物体?这个对象是什么类型的?更多svg或图像和文字?
  • You need know because, if you use .toDatalessJson() method and you will have more svg loaded in your canvas, this svg will be converted in Path and PathGroup object, and will be save in your database.
    So, if you have many, many svg in your canvas, maybe the final json string can overflow the size allowed for types TEXT in database (if this happen, there some solutions, like split your json string representation and save in two or more entries in your database).
    But, if this happen frequently, maybe will better chose other type, like MEDIUMTEXT, as you suggest.
    © www.soinside.com 2019 - 2024. All rights reserved.