簡體   English   中英

Python 通過 mysql.connector 向 mysql 插入變量

[英]Python insert variable to mysql via mysql.connector

我正在嘗試創建一個 python 腳本來將數據插入到 mysql 中,但是當我嘗試對其進行測試時出現錯誤。

這是我創建表的方式:

CREATE TABLE aaa (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     data CHAR(30) NOT NULL,
     PRIMARY KEY (id)
);

這是我的python腳本:

import mysql.connector
from time import strftime, gmtime, sleep

cnx = mysql.connector.connect(
    user='root', password='abcd', database='test_db'
    )
cur = cnx.cursor()
# get current timestamp
curr_time = strftime("%Y%m%d_%H%M%S", gmtime())
cur.execute(
            "INSERT INTO aaa(data) VALUES (%s)", (curr_time)
            )
cnx.commit()
cnx.close()

錯誤是這樣的:

mysql.connector.errors.ProgrammingError: 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 '%s)' at line 1

誰能幫我解決這個問題?

替換,% in "INSERT INTO aaa(data) VALUES (%s)", (curr_time)它應該是"INSERT INTO aaa(data) VALUES (%s)"%(curr_time) @tsh 是正確的,使用(curr_time) -> (curr_time,)代替

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM