I am trying to create a table using python in MySQL workbench but everytime that I run this I get an error saying: "Error reading data from MySQL table 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 '' at line 1"
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='localhost',
database='project2',
user='root',
password='123')
sql_select_Query = ("CREATE TABLE customer (custID int,custName VARCHAR(25), zip int, city VARCHAR(25), state VARCHAR(25)")
cursor = connection.cursor()
cursor.execute(sql_select_Query)
print("customer Table is Created in the Database")
except mysql.connector.Error as e:
print("Error reading data from MySQL table", e)
finally:
if connection.is_connected():
connection.close()
cursor.close()
print("MySQL connection is closed")
I have been trying everything different ways to do it but I keep getting errors
CodePudding user response:
sql_select_Query = ("CREATE TABLE customer (custID int,custName VARCHAR(25), zip int, city VARCHAR(25), state VARCHAR(25)")
you missed )
at the end of query command:
sql_select_Query = ("CREATE TABLE customer (custID int,custName VARCHAR(25), zip int, city VARCHAR(25), state VARCHAR(25))")
CodePudding user response:
You missed simply missed a ")" in your sql, try this :
sql_select_Query = ("CREATE TABLE customer (custID int,custName VARCHAR(25), zip int, city VARCHAR(25), state VARCHAR(25))")
please be more thorough in reading your own code. here's a simple trick if you find that error again:
- Open your DBMS
- Use your raw SQL there
- Make sure it works
- put it in your code
CodePudding user response:
Close the parenthesis at the very end:
CREATE TABLE customer (custID int,custName VARCHAR(25), zip int, city VARCHAR(25), state VARCHAR(25));