错误使用Python在MySQL中创建表

发布于 2025-01-19 16:18:44 字数 1087 浏览 3 评论 0原文

我正在尝试使用 python 创建 MySQL 表,但它一直给我这个错误:连接到 MySQL 1064 (42000) 时出错:您的 SQL 语法中有错误;检查与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行的“close FLOAT、high FLOAT、low FLOAT、open FLOAT、volume FLOAT、instrument CHA...”附近使用的正确语法

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")

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")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2025-01-26 16:18:44

这是因为没有这样的数据类型为smalldateTime。您可能是要说:

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)")

评论后续行动。

要使用MariaDB连接器,您将执行类似的操作:

import mariadb as mdb

dbconfig = {
    'host': 'localhost',
    'user': 'username',
    'password': 'password',
    'database': 'dbname'
}

try:
    db = mdb.connect(**dbconfig)
except mdb.Error as e:
    raise(f'error connecting: {e}')

# A matter of personal preference, I like queries to be returned as dictionaries, and I like auto-commits to the database:
cursor = db.cursor(dictionary=True)
db.autocommit = True

此时,cursor.execute()行应该正常工作。

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)")

Comment follow-up.

To use mariadb connector, you would do something like this:

import mariadb as mdb

dbconfig = {
    'host': 'localhost',
    'user': 'username',
    'password': 'password',
    'database': 'dbname'
}

try:
    db = mdb.connect(**dbconfig)
except mdb.Error as e:
    raise(f'error connecting: {e}')

# A matter of personal preference, I like queries to be returned as dictionaries, and I like auto-commits to the database:
cursor = db.cursor(dictionary=True)
db.autocommit = True

At this point the cursor.execute() line should work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文