【问题标题】:TypeError: not all arguments converted during string formatting using MySQL and PythonTypeError:在使用 MySQL 和 Python 进行字符串格式化期间并非所有参数都转换了
【发布时间】:2026-02-03 19:50:01
【问题描述】:

我正在使用 python 脚本将文本文件导入 mysql 数据库,但我得到一个奇怪的错误并通过互联网搜索它但找不到确切的解决方案。我想创建列,有些列将以十进制存储数据,带有负号,像这样

Alarmdata    fuAlarm
-1585.4       -35.3
-343.32       -54.3

我收到以下错误

_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")

我通过为空白数据提供数据类型解决了这个问题,但现在我遇到了这个错误。

TypeError: not all arguments converted during string formatting

我的代码在下面

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()

类型错误意味着我没有传递足够的 %s ,但我传递了相同的数字,它不应该显示此错误。我也是 MySQL 和 python 的新手,所以我没有解决。

【问题讨论】:

  • 在您的表定义中,您没有 Alarmdata 的数据类型
  • @Niagaradad 是的,我不知道在哪里。如果该列在表中但没有数据,则为空白。那么我应该显示什么数据类型? NULL 还是 INT?其次,如果我有像 -152.43 这样的值,我该如何分配这个数据类型? .如果有任何提示,我将非常感激。我花了一整天的时间。
  • OP - 添加了一个社区 wiki 作为对您最后陈述的回答,但没有意义。 Python 的 Nones 在 MySQL 中被翻译为 Nulls。此外,您的追加查询缺少逗号,并且为列出的字段传递了太多占位符。未来的读者可能会对您的发现感兴趣。请通过示例代码调整完全回答您自己的问题。
  • 我用代码示例和正确答案提供我的答案。我希望它能帮助人们。

标签: python mysql database


【解决方案1】:

在 MySQL 中,INT 数据类型的取值范围可以从-21474836482147483647。所以你可以简单地将数据类型 INT 放在那里。剩下的一切都将由 MySQL 处理。

【讨论】:

  • 你的意思是分配-153.33 我会给数据类型INT?因为对于我通过分配 INT 数据类型来解决的空白数据。虽然我得到一个新的错误。 TypeError:并非所有参数在字符串格式化期间都转换了,但我会尽快解决。实际问题是将数据类型分配给 -153.33 数字。
【解决方案2】:

(代表 OP 发布)

我解决了这个问题。问题是如果我们将空格转换为 None 类型,Python 会读取空格。

【讨论】:

    【解决方案3】:
    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()
    

    在这里我面临两个问题。 1)如果有像Alarmdata这样的空列,我仍然需要传递数据类型,这里我传递了INT数据类型,这样可以很容易地消除这个错误。

     2)   TypeError: not all arguments converted during string formatting     
    

    这里的问题是我在 cursor.execute 中传递了 NULL 并且它给出了错误,所以我编写了将每个空白空间都转为空并在那里写入 None 的代码。在占位符中,我没有提供 NULL 而只是普通的占位符,并且还提供了正确数量的占位符,问题就解决了。

    我希望它能帮助人们。

    【讨论】: