Geht:
Python
#!/usr/bin/env python3
import datetime
import mysql.connector
from mysql.connector import errorcode
TEXT = "text.txt"
def mariadb():
datumzeit=(datetime.datetime.now().strftime('%y-%m-%d %H:%M:%S'))
try:
with open(TEXT, "r") as v:
text = v.read()
db = mysql.connector.connect(user='user', password='passwort', host='host', database='database')
cursor = db.cursor(prepared=True)
sql = "INSERT INTO daten (text, datumzeit) VALUES (%s, %s)"
val = (text, datumzeit)
cursor.execute(sql,val)
db.commit()
cursor.close()
db.close()
except Exception as error:
print(error)
mariadb()
Display More
Geht nicht:
Python
#!/usr/bin/env python3
import datetime
import mysql.connector
from mysql.connector import errorcode
TEXT = "text.txt"
def mariadb():
datumzeit=(datetime.datetime.now().strftime('%y-%m-%d %H:%M:%S'))
try:
with open(TEXT, "r") as v:
text = v.read()
db = mysql.connector.connect(user='user', password='passwort', host='host', database='database')
cursor = db.cursor(prepared=True)
sql = "INSERT INTO daten (text) VALUES (%s)"
val = (text)
cursor.execute(sql,val)
db.commit()
cursor.close()
db.close()
except Exception as error:
print(error)
mariadb()
Display More
Fehlermeldung:
Incorrect type of argument: str(Texteins11.11), it must be of type tuple or list the argument given to the prepared statement
Warum funktioniert das mit Text und Datum, aber nicht nur als Text ?
Die Fehlermeldung bringt mich bei der Suche auch nicht weiter.