python
  • mysql
  • 2017-02-09 79 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 -

    標識符可以以數字開始,但除非報價可能不是由 完全的數字。

    相關問題