简体   繁体   中英

TypeError: not all arguments converted during string formatting using MySQL and Python

I am importing text file to mysql database by using python script, but I get a weird error and search for it over internet but could'nt find exact solution. I want to create columns and some columns will store data in decimal with negative sign like this

Alarmdata    fuAlarm
-1585.4       -35.3
-343.32       -54.3

I get the following error

_mysql_exceptions.ProgrammingError: (1064, "You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near '                 Alarmdata,
fuel_in_l DECIMAL(5,2), fuAlarm DECIMAL(4,3),        ' at line 1")

I solved this issue by giving data type to blank data but now I am facing this error.

TypeError: not all arguments converted during string formatting

My code is below

import os
import sys
import csv
import MySQLdb as mdb

con = mdb.connect('localhost' , 'root' , 'kami' , 'tempdat')

cursor = con.cursor()
cursor.execute('CREATE TABLE Datatmp (id INT NOT NULL PRIMARY KEY     AUTO_INCREMENT, \
            timestamp INT(12), pomode INT(3), modeAlarm INT(3), \
            temperature_C DECIMAL(2,1), temperatureAlarm INT(3), \
            switch INT(3), Powerswitch INT(3), SIM_dollar INT , \
            Alarmdata INT, fuel_in_l DECIMAL(5,2), fuAlarm DECIMAL(4,3), \
            next_maint DECIMAL(5,2), next_maintenanceAlarm INT(2) )');

con.commit()             

file = open('//home/mysql/kami.txt', 'rb')
creader = csv.reader(txtfile.readlines()[3:], delimiter='\t')

for t in creader:
    cursor.execute('INSERT INTO Datatmp(timestamp, pomode, modeAlarm,\
                    temperature_C, temperatureAlarm, \
                    switch, Powerswitch, SIM_dollar, \
                    Alarmdata, fuel_in_l, fuAlarm \   
                    next_maint,  next_maintenanceAlarm)\
                    Values(%s,%s,%s,%s,%s,%s,NULL,NULL,%s,%s,%s,\
                    %s,%s,%s,%s,%s,%s)', t) 

file.close()
con.commit()
con.close()

Type error mean that I didn't pass enough %s , but I passed the same number and it should not display this error. I am new to MySQL and python as well, so I didn't solve.

In MySQL, INT datatype can have the value ranges from -2147483648 to 2147483647 . So you can simply put the datatype INT there. Rest everything will be by taken care by MySQL.

(Posted on behalf of the OP) .

I solved this problem. The issue is that blank spaces are read by Python if we convert them to type of None.

import os
import sys
import csv
import MySQLdb as mdb


con = mdb.connect('localhost' , 'root' , 'kami' , 'tempdat')

cursor = con.cursor()
cursor.execute('CREATE TABLE Datatmp (timestamp INT(12), pomode INT(3), \
            modeAlarm INT(3), temperature_C DECIMAL(2,1),temperatureAlarm INT(3), \
            switch INT(3), Powerswitch INT(3),SIM_dollar INT(3), \
            Alarmdata INT(5),fuel_in_l DECIMAL(5,2), fuAlarm DECIMAL(4,3), \
            next_maint DECIMAL(5,2), next_maintenanceAlarm INT(2))');




con.commit()             

file = open('//home/mysql/kami.txt', 'rb')
creader = csv.reader(txtfile.readlines()[3:], delimiter='\t')



# now insert values in database
i = 0;

# read and store the values in database and also replace empty data 

for t in creader:
    print t
    for idx, v in enumerate(t):
        if v == ' ':
            t[idx] = None                   
    cursor.execute('INSERT INTO Data_121_RAPI_txt VALUES(%s,%s,%s,%s,%s,%s, \
                %s,%s,%s,%s,%s,%s, %s)', t)                
 i = i + 1



file.close()
con.commit()
con.close()

Here I face two problems. 1) if there are empty columns like Alarmdata, I still need to pass datatype and here I pass INT datatype so this error can easily be removed.

 2)   TypeError: not all arguments converted during string formatting     

Here the problem was that I pass NULL in cursor.execute and it gives error, so I make code which turn every empty space and write there None. and in palceholder I didn't provide NULL but simply normal palceholder, and also providing correct number of placeholders , the issue is solved.

I hope it will help people.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM