尝试使用日期作为python中的输入并使用mysql连接器更新mysql表中的值

问题描述 投票:0回答:1

我的目标是使用用户的更新语句的所有变量作为用户输入来更新mysql数据库中的现有表。我正在使用mysqlconnector与python进行接口。

    coltbu=input("Now enter the name of the column whose value for that row/condition is to be updated")
    if coltbu=="dob":
        y1=input("Enter the value to be set for date of joining in yyyy-mm-dd")    
    elif col=="id":
        nval=int(input("Enter the value to be set for salary"))  
    else:
        nval=input("Enter the value to be set for this column")
    x='%d-%m-%Y'
    tup=(coltbu,y1,x,col,val)
    print(tup)
    cursor1.execute("Update student set %s=str_to_date(%s,%s) where %s=%s"%tup)
    con1.commit()
    con1.close()

在互联网上苦苦寻找解决方案之后,我有很多不同之处,但是我似乎找不到任何可行的方法。某些错误或其他总是出现在这种情况下,我正在使用的表是image of table

我尝试执行的命令是update student set dob='2003-09-12' where id=6;

我也尝试使用datetime来工作。

    coltbu=input("Now enter the name of the column whose value for that row/condition is to be updated")
    if coltbu=="dob":
        y=int(input("Enter the value to be set for dob in yyyy/mm/dd. First enter year and hit enter"))
        m=int(input("Now enter month"))
        d=int(input("Now enter date"))
        nval=datetime.date(datetime(y,m,d))    
    elif col=="id":
        nval=int(input("Enter the value to be set for id"))  
    else:
        nval=input("Enter the value to be set for this column")
    tup=(int(coltbu,nval,col,val)
    print(tup)
    cursor1.execute("Update student set %s=%s where %s=%s"%tup)
    con1.commit()
    con1.close()

但是这引发了一个非常奇怪的错误。它会说

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 489, in cmd_query
    raw_as_string=raw_as_string)
_mysql_connector.MySQLInterfaceError: Incorrect date value: '1993' for column 'dob' at row 4

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/aliziamojiz/Documents/Alizia/12/prac1.py", line 118, in <module>
    f4()
  File "/Users/aliziamojiz/Documents/Alizia/12/prac1.py", line 113, in f4
    cursor1.execute("Update student set %s=%s where %s=%s"%tup)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 266, in execute
    raw_as_string=self._raw_as_string)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 492, in cmd_query
    sqlstate=exc.sqlstate)
mysql.connector.errors.DataError: 1292 (22007): Incorrect date value: '1993' for column 'dob' at row 4

尽管我在代码或输入中都没有输入1993。

我现在被困住了。如何将日期作为python程序的输入,并在mysql表的更新命令中使用它。请帮忙。在此先感谢:)

python mysql datetime str-to-date
1个回答
0
投票

您使用日期时间获取日期的方式似乎很奇怪。如果日期格式不符合预期或日期无效,则strptime将引发ValueError。通常这样使用:

dateInput = input("Enter the value to be set for dob in the format yyyy/mm/dd: ")
try:
    dateObject = datetime.strptime(dateInput, "%Y/%m/%d")
except ValueError:
    raise ValueError("WRONG DATE FORMAT!")

然后,您可以使用输入日期用dateObject.date()更新表格。另外,我还建议您在使用execute时,请像这样在SQL查询中分隔值(以防止SQL注入):

queryValues = (str(dateObject.date()). idInput)
cursor1.execute("UPDATE student SET dob=%s WHERE id=%s", queryValues)
© www.soinside.com 2019 - 2024. All rights reserved.