0

您好,我正在对我的 ESP8266 进行编程,就像客户端发送 pH 传感器和流量计的数据一样。为了发送 ESP8266 的数据,我做了一个到服务器的套接字连接。但这里的问题是我发送的数据被拒绝了。查看错误:pymysql.err.ProgrammingError: (1064, "您的 SQL 语法有错误;请查看与您的 MariaDB 服务器版本相对应的手册,以了解在 '0.0'、'2' 附近使用的正确语法)'在第 1 行")

这是我的 client.py 代码:

import socket
import machine
import network
from machine import Pin, ADC
import time, sys
from time import sleep
import esp
esp.osdebug(None)
import gc
gc.collect()

ip = 0

def conectar_wifi():

    global ip
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('Conectando a la red...')
        sta_if.active(True)
        sta_if.connect('', '') 
        while not sta_if.isconnected():
            pass
    red = sta_if.ifconfig()
    ip = red[0]
    print('Configuracion de la red:', red[0:3])

conectar_wifi()
sensorph = ADC(0)
subirph = Pin(15, Pin.OUT, value= 0)
bajarph = Pin(14, Pin.OUT, value= 0)
flow = Pin(2, Pin.IN, Pin.PULL_UP)
led1 = Pin(4, Pin.OUT)
buzzer = Pin(16, Pin.OUT, value= 0)

count = 0
start_counter = 0
caudal = 0

def countPulse(p):
    global count
    global start_counter
    
    if start_counter == 1:
        count = count+1
        #flow = count / (60 * 7.5)

flow.irq(handler= countPulse, trigger= flow.IRQ_FALLING)

def phsensor():
    valor = sensorph.read ()
    tension = (valor * 3.3/1023)
    print ("Tension: ", tension)
    return tension

port = 8000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = socket.socket()
s.connect(("",port))

print('Puerto en', ip)


while True:
    ph=phsensor()
    start_counter = 1
    sleep (1)
    start_counter = 0
    caudal = (count * 60 * 2.25 / 1000)
    print("El caudal es: %.3f L/min" % (caudal))
    count = 0
    sleep (5)
    if caudal < 1:
        print("No esta llegando suficiente agua")
        led1.value(1)
    else:
        led1.value(0)
    if caudal > 2:
        print("El agua esta circulando bien")
    data=str(caudal).encode('utf-8')
    s.send(data)
    print(data)
    dataFromServer = s.recv(1024)

和 server.py

import socket
import pymysql
from pymysql import cursors

connection = pymysql.connect(
    host="localhost",
    user="root",
    password="",
    db="prueba",
)

cursor = connection.cursor()


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


s.bind(("",8000))

s.listen()
print("Todo en orden rey")

 
while(True):

    clientConnected, clientAddress = s.accept()

    print(F"Se aceptó una solicitud de conexión de {clientConnected}:{clientAddress}")

    dataFromClient = clientConnected.recv(1024)

    databuena = dataFromClient.decode('utf-8')
    print(databuena)
    
    datos = str(dataFromClient)
    sql = "INSERT INTO roberto (nivelph, caudal) VALUES('"+datos+"', '2')"

    cursor.execute(sql)

    connection.commit()

    clientConnected.send("hola cliente".encode())

我看到错误给了我一个撇号,我删除了它,但它也不起作用。 数据库 它有西班牙文 phpmyadmin 并将其翻译成英文,以便更好地理解。

4

1 回答 1

0

由于您的架构是 (int, int),因此您需要提供两个整数,而不是字符串。在您的代码中,datos 是 dataFromClient 的字符串版本,因此我将使用非字符串版本。此外,“2”是硬编码的,它也应该是一个 int,或者只是 2。

sql = "INSERT INTO roberto (nivelph, caudal) VALUES(?, ?)"
cursor.execute(sql, [dataFromClient, 2])
于 2021-10-14T12:10:37.940 回答