【发布时间】:2023-01-08 20:16:25
【问题描述】:
我正在尝试使用 django 和 mysql 将一些数据行从 python 上传到 mysql,因为我们还想在我们的网页中显示数据库中的数据。
与服务器的连接正常工作,与数据库的连接也是如此。
当我们执行以下查询将数据从 python 插入到 mysql 时,问题就来了。
def addInfo(请求,T,P,A,Rx,Ry,Rz,Ax,Ay,Az,Fecha,Long,Lat,Vel): """conexion = mysql.connector.connect(user='root', password='admin', host='localhost', 数据库='数据',端口='3306') 打印(连接) 游标 = conexion.cursor()
print("T:"+str(T))"""
query = "use datos; INSERT INTO datos(Temperatura, Presion, Altitud, RotacionX, RotacionY, RotacionZ, AceleracionX, AceleracionY, AceleracionZ, Fecha, Longitud, Latitud, Velocidad) VALUES ('"+T+"','"+P+"','"+A+"','"+Rx+"','"+Ry+"','"+Rz+"','"+Ax+"','"+Ay+"','"+Az+"','"+Fecha+"','"+Long+"', '"+Lat+"', '"+Vel+"');"
print(query)
cursor.execute(query)
documento = """<!DOCTYPE html>
<html>
<head>
--HTML PART IS OMITTED--
</html>"""
print(cursor)
cursor.close()
return HttpResponse(documento)
当我们执行这段代码时,数据库不会更新行,但是当我们在 mysql 中手动执行时,它确实有效并且 id 自动递增,就像我们上传了之前的查询一样。 (但上一行没有出现)
【问题讨论】: