Learn, Share, Build

287
October 02, 2017, at 05:21 AM

Here is the code:

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 #float(sdata[2])
    AP = 1121.6 #float(sdata[3])
    PF =  1.02 #float(sdata[4])
    CPS =  60.41 #float(sdata[5])
    UP =  1154.1 #float(sdata[6])
    BV =  27.22 #float(sdata[7])
    BI = 78.66 #float(sdata[8])
    WI =  50.87 #float(sdata[9])
    EI =  23.83 #float(sdata[10])
    temp_BP =  78.2 #float(sdata[11])
    gridOK = 1 #int(sdata[12])
    genON = 0 #int(sdata[13])
    genOK =  0 #int(sdata[14])
    db = MySQLdb.connect("localhost","charlie","chab1541","PowerData")
    c = db.cursor ()
    c.execute("""INSERT INTO data
           VALUES (%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %d, %d,        %d )""",
          (Vrms, Irms, RP, AP, PF, CPS, UP,  BV, BI, WI, EI, temp_BP, gridOK, genON, genOK))
    db.commit ()
    c.close ()
    db.close ()

Here is the error:

Traceback (most recent call last): File "C:/Users/Charlie/PycharmProjects/Read Store Power Data/PowerParser.py", line 41, in ( Vrms, Irms, RP, AP, PF, CPS, UP, BV, BI, WI, EI, temp_BP, gridOK, genON, genOK)) File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 183, in execute query = query % db.literal(args) TypeError: float argument required, not str

I can't seem to get rid of the float error. I commented out the parsed list of float conversions, thinking that was the problem-no joy. Of course, taking the var names out and hard-coding floats works.

I am missing something! Now have mind-mush... Please show me the error of my ways!

Answer 1

Use "%s" instead of "%f" and "%d" in your insert statement. MySQL knows the data type that corresponds to each table column when you create the table - that datatype specific to each field does not need to match the variable type passed in your insert statement. MySQL likes string inserts and will convert your inserts to the corresponding data type for the respective field.

Answer 2

FIXED!

One needs to always use %s, no matter the actual type:

Changed code:

    c.execute("""INSERT INTO data (Vrms, Irms, RP, AP, PF, CPS, UP, BV, BI, WI, EI, temp_BP, gridOK, genON, genOK)
               VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s )""",
              (Vrms, Irms, RP, AP, PF, CPS, UP,  BV, BI, WI, EI, temp_BP, gridOK, genON, genOK))

Found answer way down on Python MySQL Parameterized Queries

Thanks to all!

Rent Charter Buses Company
READ ALSO
Learn, Share, Build

Learn, Share, Build

I am using Python 3x on Linux (Ubuntu) to read piped terminal content and save it to a database

282
Learn, Share, Build

Learn, Share, Build

I have a Scrapy project that need to be build, below the project structure:

260
Learn, Share, Build

Learn, Share, Build

In Python 3, I would like to check whether value is either string or None

229
Learn, Share, Build

Learn, Share, Build

I'm learning the basics of python and i want to know in this function, how to insert a new line? I know i could have in the function;

264