Home > OS >  Error creating table in MYSQL using Python
Error creating table in MYSQL using Python

Time:04-08

I am trying to create a MySQL table using python but it keeps giving me this error: Error while connecting to MySQL 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' close FLOAT, high FLOAT, low FLOAT, open FLOAT, volume FLOAT, instrument CHA...' at line 1

import mysql.connector
from mysql.connector import Error

#connecting to database
try:
    connection = mysql.connector.connect(host = "localhost",
                                         user = "root",
                                         database="hindalco"
                                         )

    #cursor method used to perform SQL operations
    cursor = connection.cursor()
    cursor.execute("CREATE TABLE tickersymbol (id INT AUTO_INCREMENT PRIMARY KEY, datetime smalldatetime, close FLOAT, high FLOAT, low FLOAT, open FLOAT, volume FLOAT, instrument CHAR(10)")


#print error messages using error as object
except Error as e:
    print("Error while connecting to MySQL", e)

#close open connections after work is complete
finally:
    if connection.is_connected():
        connection.close()
        print("MySQL connection is closed")

CodePudding user response:

It is because there is no such data type as smalldatetime. You probably meant to say:

cursor.execute("CREATE TABLE tickersymbol (id INT AUTO_INCREMENT PRIMARY KEY, smalldatetime datetime, close FLOAT, high FLOAT, low FLOAT, open FLOAT, volume FLOAT, instrument CHAR(10)")

CodePudding user response:

Just an additional note that it is good to put your code in the following format:

conn= (connection string)
qry=()
cursor.execute(qry, conn)
  • Related