Home > Software design >  Why do i get error 1064 (42000) near : avatar VARCHAR(1028) DEFAULT `image.jpeg`,
Why do i get error 1064 (42000) near : avatar VARCHAR(1028) DEFAULT `image.jpeg`,

Time:01-10

This is my script for connecting to mysql

im trying to create a connector or middle man for mysql database and flask application

while im tyring to create a table i hit an error


#!/usr/bin/python


import mysql.connector
from mysql.connector import Error as SQLError

db_info = ['localhost', 'xxx', 'xxx', 'xxx']

# Trying to connect to server

try:
    connection = mysql.connector.connect(host=db_info[0],
                                         database=db_info[1],
                                         user=db_info[2],
                                         password=db_info[3])

    if connection.is_connected():
        db_Info = connection.get_server_info()
        print("Connected to MySQL Server version ", db_Info)
        cursor = connection.cursor() # CURSOR
        cursor.execute("select database();")
        record = cursor.fetchone()
        print("You're connected to database: ", record)

except SQLError as e:
    print("Error while connecting to MySQL", e)
    quit()


artist_table_create = """
CREATE TABLE artist (
    id INT NOT NULL,
    sid INT NOT NULL,
    name VARCHAR(32) NOT NULL,
    birthdate DATE NOT NULL,
    gender ENUM('0', '1') NOT NULL,
    code_melli VARCHAR(32) NOT NULL,
    phonenumber VARCHAR(16) NOT NULL,
    email VARCHAR(128) NOT NULL,
    location1 VARCHAR(32) NOT NULL,
    location2 VARCHAR(32) NOT NULL, 
    date_signedup DATE NOT NULL,
    verification_status ENUM('0', '1'),
    avatar VARCHAR(1028) DEFAULT `image.jpeg`,
    rank VARCHAR(256) NOT NULL,
    env1 VARCHAR(256) NOT NULL, 
    env2 VARCHAR(256) NOT NULL,
    env3 VARCHAR(256) NOT NULL,
    PRIMARY KEY (id, sid, phonenumber)
);"""

try:
    result = cursor.execute(artist_table_create)
except SQLError as e:
    print(e)

And this is my error in command line:

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 '`image.jpeg`,
    rank VARCHAR(256) NOT NULL,
    env1 VARCHAR(256) NOT NULL, 
 ' at line 14

I dont know what should i cant understand mysql errors

I need to fix this error

CodePudding user response:

The MySQL errror is already a pretty good indicator where the problem might be. It actually says the error is near `image.jpeg`. As you can see you are using backqoutes which look are these: ` `. But you have to use normal sinqle quotes, which are these: ' '. In order to fix your error you simple have to put single qoutes around image.jpeg instead of the backqoutes.

  • Related