Home > Database > Mysql Tutorial > mysql操作时遇到的小问题_MySQL

mysql操作时遇到的小问题_MySQL

WBOY
Release: 2016-06-01 13:13:27
Original
964 people have browsed it

mysql数据库在程序中执行sql语句时,或者在执行sql时,数据库表可能会有一些特殊的字符,比如说#,.等,这样在执行时

可能会遇到问题如以下的表名,backup_2014.2.22, 这个表在查询时会有问题因为存在这个.的缘故,会报错table_name=“backup_2014.2.22"如python中写self.db.query("show create table %s" % table_name)[0]会报错(1064, "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 '.22' at line 1”)Traceback (most recent call last):  File "/Users/xuxiaodong/work/db_table_to_Wiki.py", line 120, in     main(sys.argv)  File "/Users/xuxiaodong/work/db_table_to_Wiki.py", line 117, in main    db.get_db_tables_to_wiki()  File "/Users/xuxiaodong/work/db_table_to_Wiki.py", line 31, in get_db_tables_to_wiki    create_table = self.db.query("show create table %s" % table_name)[0]TypeError: 'NoneType' object has no attribute ‘__getitem__' 从报的错误来看是因为sql语句的语法错误,而原因就是将带有特殊字符的表名直接作为字符串进行sql时会有语法错误 而在sql语句中字段和表名应该是有`table`,这两个符号来包括,就可以了self.db.query("show create table `%s" % table_name + "`”) 这是sql语句中的标准写法,养成这个习惯比较好

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template