最近在编写一个SQL过程中遇到了如下报错:
ERROR 1064 (42000): You have an error in your SQL syntax
我的SQL语句为:
insert into t_topic(category, title, desc, content) values(2, "Hello World", 'This is a beautiful world.', 'blabla...');
这是一个很简单的语句,就是往表t_topic里插入一条记录。但是一直不成功。
首先检查了一下各字段数据类型,都是匹配的。
然后注意到报错中提示是在’desc’字段出了错。
从网上查了一下MySQL这个错误码。恍然大悟。
原来语句中使用到了MySQL的关键字desc!
虽然我经常用desc查看表结构,但没意识到这个表结构竟然用它作为字段名称了!
这种表设计真是太不应该了。
解决办法:在sql语句中,用到MySQL关键字的地方,加上反引号,也就是键盘上数字那一排最右侧那个(“)。如下所示:
insert into t_topic(category, title, ·desc·, content) values(2, "Hello World", 'This is a beautiful world.', 'blabla...');
LNMP 环境是指在 Linux 系统下,由 Nginx + MySQL/MariaDB + PHP 组成的网站服务器架构。
本文档介绍如何在腾讯云云服务器(CVM)上手动搭建 LNMP 环境。
进行手动搭建 LNMP 环境,您需要熟悉 Linux 命令,例如 CentOS 环境下通过 YUM 安装软件 等常用命令,并对所安装软件的使用及版本兼容性比较了解。
我们知道Python3和Python2是不兼容的,版本由2升级到3之后对MySQL数据库的访问也需要使用新的模块。此前的文章中已介绍过MySQLdb在Python2中的用法,今天介绍PyMySQL在Python3中的用法。
- 安装
- 直接通过pip安装:
pip install PyMySQL
- 示例
import pymysql
try:
conn = pymysql.connect(ip, 3306, user, password, db, charset='utf8', cursorclass=pymysql.cursors.DictCursor)
except Exception as e:
print("Exception throwed when connect to db: " + str(e))
- 插入数据
try:
with conn.cursor() as cursor:
sql = "insert into t_student(id, name) values (%d, %s)"
rows=cursor.execute(sql, (s.id, s.name))
conn.commit()
if rows == 1:
print("Inserted.")
else:
print("Failed")
except Exception as e:
print("Exception throwed when insert data: " + str(e))