Search code examples
python-3.xpymysql

why i can't create the database automatically?


import pymysql
con = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='******')
db_name=['Cinfos', 'Holders', 'Capitals','Dividends','Fi_Dis','Finances']   
for db in db_name:
    cur=con.cursor()
    cur.execute("create database  if not exit %s" %db)
    con.commit()
    cur.close()

I got the error :
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; chec k the manual that corresponds to your MySQL server version for the right syntax to use near 'exit Cinfos' at line 1")
What is the matter with my pymysql code?


Solution

  • You have an error in your query.

    cur.execute("create database  if not exit %s" %db)
    

    should be:

    cur.execute("create database  if not exists %s" %db)