为什么我的未经编辑的脚本突然失败?

时间:2017-10-30 14:28:22

标签: python mysql

所以我有一个python脚本,过去工作完美,现在开始失败。 该脚本采用mbox文件并将内容传输到mysql。

这是失败的段,由于某种原因它与mysql语法联系在一起 错误:

File "mb_sql.py", line 165, in <module>
    main()
  File "mb_sql.py", line 82, in main
    db.query(database_string)
_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 'call (\n  id INT NOT NULL AUTO_INCREMENT,\n  subject VARCHAR(200) ,\n  sender VARCH' at line 1")

这是基于上述错误而失败的部分。

def main():   #line29 
#some sanity test
  if len(sys.argv) != 3:
    usage()
    exit(-2)   
  mbox = sys.argv[1]   
  db_name = sys.argv[2]
  if mbox[-5:] == '.mbox': db_table=mbox[:-5].lower().replace('/','').replace('-','')
  else:
    print "Please give an mbox file.\nQuitting..."
    exit(-2)
#mysql table creation string
#if you change the string, you'll have to change the related entries on the several mysql queries that take place bellow
#text2 is true when is_multipart() is True
  database_string = """
  CREATE TABLE %s (
  id INT NOT NULL AUTO_INCREMENT,
  subject VARCHAR(200) ,
  sender VARCHAR(100) ,
  reply_to VARCHAR(100) ,
  cc VARCHAR(100) ,
  date VARCHAR(100),
  text LONGTEXT,
  text2 LONGTEXT,
  PRIMARY KEY(id)
  );
  """ % db_table


  try:
    db = _mysql.connect("localhost","root","")
  except _mysql_exceptions.OperationalError: 
    print "Check that you gave the correct credentials to access the db. Quitting...\n"
    exit(-2)

  table_exists = 0
  existing_max_id = 0

#try to create database and/or table, if one or both exist try to use them
  try:
    db.query("create database %s" % db_name)
    db.query("use %s" % db_name)
    db.query(database_string)
  except _mysql_exceptions.ProgrammingError: 
    print "Can't create database, seems that database %s exists. We'll use it then " % db_name
    db.query("use %s" % db_name)
    try:
        db.query(database_string)    #Line 82
    except _mysql_exceptions.OperationalError:  
        print "Can't create table, seems that table %s exists. We'll use it then " % db_table
        table_exists = 1
        db.query("select max(id) from %s" % db_table)
        r=db.store_result()
        existing_max_id = r.fetch_row()[0][0]
  except _mysql_exceptions.OperationalError:
      print "Access denied: User can't create the database. Quitting...\n" 
      exit(-2)

1 个答案:

答案 0 :(得分:1)

callreserved keyword in MySQL。使用反引号来转义名称或使用其他名称。

create table `call` ...