前体:
MySQL Table created via:
CREATE TABLE table(Id INT PRIMARY KEY NOT NULL, Param1 VARCHAR(50))
Run Code Online (Sandbox Code Playgroud)
功能:
.execute("INSERT INTO table VALUES(%d,%s)", (int(id), string)
Run Code Online (Sandbox Code Playgroud)
输出:
TypeError: %d format: a number is required, not a str
Run Code Online (Sandbox Code Playgroud)
我不确定这里发生了什么,为什么我无法执行命令.这是在Python中使用MySQLdb..execute是在游标对象上执行的.
编辑:
主题:Python MySQLdb问题(TypeError:%d格式:需要一个数字,而不是str) 表示必须对所有字段使用%s.为什么会这样?为什么这个命令有效?
.execute("INSERT INTO table VALUES(%s,%s)", (int(id), string)
Run Code Online (Sandbox Code Playgroud) 这是代码:
import MySQLdb
from socket import *
import time
address = ( '192.168.1.177', 5000)
client_socket = socket(AF_INET, SOCK_DGRAM) #Set Up the Socket
client_socket.settimeout(30) # wait 30 seconds for a response
while(1): #Main Loop
data = "send" #Set data to Blue Command
client_socket.sendto(data, address) #send command to micro
rec_data, addr = client_socket.recvfrom(1024) #Read response from micro
print rec_data #Print the response from micro
sdata = rec_data.split(',') #csv string split
Vrms = 123.1 #float(sdata[0]) #
Irms = 213.45 #float(sdata[1])
RP = 10.1 …Run Code Online (Sandbox Code Playgroud)