sql语句中单引号和反引号的区别?
网上说Mysql中字段跟保留字相同时用反引号(键盘中ESC键下面那个)。但是我的字段跟保留字不一样,用单引号就报错,改成反引号就正确了,为什么啊?????
我把存储到数据库的完整代码贴出来(创建表非手动,用代码实现的),这个代码能正确运行,请高手看看为什么非要用反引号呢????
# wiki.py from urllib.request import urlopen from bs4 import BeautifulSoup import re import pymysql.cursors resp = urlopen("https://en.wikipedia.org/wiki/Main_Page").read().decode("utf-8") soup = BeautifulSoup(resp, "html.parser") listUrls = soup.findAll("a", href=re.compile("^/wiki/")) for url in listUrls: if not re.search("\.(jpg|JPG)$", url["href"]): urlname = url.get_text() urlhref = "https://en.wikipedia.org" + url["href"] print(urlname, "---> ", urlhref) # 获取数据库链接 connection = pymysql.connect(host="localhost", user="root", password="admin", db="wikiurl", charset="utf8mb4") try: # 获取会话指针 with connection.cursor() as cursor: # 如果不存在就创建表,字段用反单引号!!! sql_create_table = '''CREATE TABLE IF NOT EXISTS `urls` ( `id` int(11) NOT NULL AUTO_INCREMENT, `urlname` varchar(255) NOT NULL, `urlhref` varchar(1000) NOT NULL, PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1''' cursor.execute(sql_create_table) # 创建sql语句 sql_insert = 'insert into urls (urlname, urlhref) values (%s, %s)' # 执行sql 语句 cursor.execute(sql_insert, (urlname, urlhref)) # 提交 connection.commit() finally: connection.close()