【问题标题】:Why do i get error 1064 (42000) near : avatar VARCHAR(1028) DEFAULT `image.jpeg`,为什么我在 avatar VARCHAR(1028) DEFAULT `image.jpeg` 附近收到错误 1064 (42000),
【发布时间】:2023-01-08 23:58:34
【问题描述】:

这是我连接到 mysql 的脚本

我正在尝试为 mysql 数据库和烧瓶应用程序创建连接器或中间人

当我试图创建一个表时,我遇到了一个错误


#!/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)

这是我在命令行中的错误:

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

我不知道我应该不能理解 mysql 错误

我需要修复这个错误

【问题讨论】:

  • 默认值 image.jpeg 应该用单引号 ' 括起来,而不是反引号。

标签: python mysql database mysql-connector


【解决方案1】:

MySQL 错误已经很好地指示了问题所在。它实际上说错误在 `image.jpeg` 附近。如您所见,您正在使用看起来像这样的反引号:``。但是你必须使用正常的单引号,它们是:' '。为了修复您的错误,您只需在 image.jpeg 周围放置单个引号而不是反引号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    相关资源
    最近更新 更多