Search code examples
pythonmysqlmysql-connector-python

What is the error in syntax in python code for MySQL connector?


This is a python code to create table in MySQL through mysql-connector. But it is showing ProgrammingError while assigning datetime datatype to field.

import mysql.connector as sqltor
from datetime import datetime
mycon = sqltor.connect(host='localhost',database='linkbook',user='root',passwd='****')
cursor = mycon.cursor()

cursor.execute("CREATE TABLE IF NOT EXISTS account\
               (UserID varchar(10) Primary Key, \
               Firstname varchar(15), \
               Lastname varchar(15), \
               Password varchar(10), \
               Links_Posted int DEFAULT 0)")

cursor.execute("CREATE TABLE IF NOT EXISTS linkbook \
               (UserID varchar(10), \
               Links varchar(50), \
               Description varchar(100), \
               Tag varchar(15), \
               date_time datetime)")

cursor.execute("CREATE TABLE IF NOT EXISTS edu_links\
               (UserID varchar(10) \
               Link varchar(50), \
               Description varchar(100), \
               date_time datetime)")   #Error in this line
Exception has occurred: ProgrammingError
1064 (42000): 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 'Link varchar(50),                Description varchar(100),                date_t' at line 1

Kindly help in getting the right syntax. Thank You.


Solution

  • If you will look closely at the last cursor.execute method call, then you will see that you are missing a comma after UserID varchar(10). This then changes you final cursor.execute call with the following parameter value:

        cursor.execute("CREATE TABLE IF NOT EXISTS edu_links\
               (UserID varchar(10), \
               Link varchar(50), \
               Description varchar(100), \
               date_time datetime)")