国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

格式化 MySql 的日期值
P粉627136450
P粉627136450 2024-02-26 20:50:13
0
1
463

我的計劃是將日期 (day_of_test) 插入 MySql 數(shù)據(jù)庫,其中數(shù)據(jù)格式被引用為日期。

我必須將語法保留為您在下面看到的格式。但不幸的是,我嘗試將日期專門存儲為語法 VALUES(%s...) 中的字符串似乎不起作用。

有人知道調(diào)整語法以成功插入 day_of_test 的最簡單方法嗎?

非常感謝大家

import mysql.connector

mydb = mysql.connector.connect(host="34.xxx.xxx.xxx", user="xxxx", password="xxxxx", database='btc-analysis-db')

c = mydb.cursor()
btc_est = 150.2
sap_close = 100.23
gold_close = 120.2
bonds_close = 210.12
btc_actual = 130.12
day_of_test = "2022-04-20"

c = mydb.cursor()

c.execute(
    "INSERT INTO `ML1`(`day_of_test`, `btc_est`, `sap_close`, `bonds_close`, `gold_close`, `btc_actual`) VALUES (%s, %d, %d, %d, %d, %d);" % (
        day_of_test, btc_est, sap_close, bonds_close, gold_close, btc_actual))

mydb.commit()

c.execute("select * from ML1")

result = c.fetchall()

P粉627136450
P粉627136450

全部回復(1)
P粉958986070

如果您使用 python 字符串格式,那么您在日期值周圍缺少引號,您的語法應該是:

c.execute(
    "INSERT INTO `ML1`(`day_of_test`, `btc_est`, `sap_close`, `bonds_close`,
    `gold_close`, `btc_actual`) VALUES ('%s', %d, %d, %d, %d, %d);" % (
        day_of_test, btc_est, sap_close, bonds_close, gold_close, btc_actual))

但你不應該那樣做。你應該使用準備好的語句(以避免 SQL 注入和其他問題),并且你的語法應該是這樣的:

c.execute(
    "INSERT INTO `ML1`(`day_of_test`, `btc_est`, `sap_close`, `bonds_close`, 
    `gold_close`, `btc_actual`) VALUES (%s, %s, %s, %s, %s, %s);", (
        day_of_test, btc_est, sap_close, bonds_close, gold_close, btc_actual))
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板