python
  • mysql
  • 2017-02-09 48 views 0 likes 
    0

    我想用python脚本创建一个sql表。使用python脚本创建SQL表时出错

    下面是代码:

    import MySQLdb 
    db1 = MySQLdb.connect(host="localhost",user="root",passwd="") 
    cursor = db1.cursor() 
    sql = 'use test' 
    cursor.execute(sql) 
    query='CREATE TABLE IF NOT EXISTS 3112017(service_area_code VARCHAR(100),phone_numbers VARCHAR(100),preferences VARCHAR(100),opstype VARCHAR(100),phone_type VARCHAR(100))' 
    cursor.execute(query) 
    db1.commit() 
    

    以下是我得到的错误:

    cursor.execute(query) 
    File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 226, in execute 
    self.errorhandler(self, exc, value) 
    File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler 
    raise errorvalue 
    _mysql_exceptions.ProgrammingError: (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 '3112017(service_area_code VARCHAR(100),phone_numbers VARCHAR(100),preferences VA' at line 1") 
    

    回答

    0

    正如回溯提到的,你有你的SQL语法错误。表名是错误的。

    SQL manual -

    标识符可以以数字开始,但除非报价可能不是由 完全的数字。

    相关问题