Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
6.4k views
in Technique[技术] by (71.8m points)

如何在 mysql 中保存 markdown 文本

我想自己搭建一个博客系统,使用 markdown 来写博客,就像思否的编辑器。

我的存储思路是同时保存我的 markdown 源文本和html,html用于在前端显示,源文本用来在更新博客时调取接口使用。

在保存进数据库的时候我遇到了一个问题---- markdown 源文本在 sql 语句中报错

Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'T', ' ')
}
time() // "2020-05-09 18:25:54"

我知道是反引号和单引号的问题,但是不知道该如何解决,google了半天没有找到解决方案

我的接口参数
image.png

后台 node 接口

image.png

存储进 mysql 的语句

image.png

请教各位!!!

我是不是应该使用某个库来转义,或者使用另一种 sql 语句


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

是的,最常见的你可以用ORM库来操作数据库(比如 sequelize),这样不仅避免了手写 SQL 语句的繁琐,还可以忽略掉转义和注入的安全风险。

当然如果你要手写 SQL,也可以用诸如 mysql2 这类库里自带的占位符 ? 来处理插入语句。

mysql.query('INSERT INTO blogs SET ?', {
    title: xss(postData.title),
    content: xss(postData.content),
    html: xss(postData.html),
    author: postData.author,
    createtime: Date.now()
});

具体的使用看文档吧,另外下次提问的时候请尽量不要直接截图代码,而是把代码贴出来。这样方便回答者在调试的时候直接复制你的代码,而不是照着图片一个字一个字的去敲。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...