pymysql返回令人困惑的值

2024-10-03 19:20:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图编写一个简单的python脚本,将文本文件导入mysql数据库,但遇到了一个令人困惑的错误

Windows 10、Mysql 5.7.18、Python 3.6、pymysql

文本文件的内容:

nickname|fullname|cell|email|updatedt
andrew|Andrew Jones|+12395551172|arj@domain.com|2017-05-04 13:26:10
laurelai|Laurelai Smith||lsmith@domain.net|2017-05-04 13:27:47

我读入数据以构造一个sql字符串: 插入到联系人(昵称、全名、单元格、电子邮件、updatedt)值(%、%、%、%、%、%)

要插入的字段值如下所示: ['andrew'、'andrew Jones'、'12395551172'、'arj@domain.com', '2017-05-04 13:26:10'] 这当然是一个Python列表对象。我试过把它转换成一个元组,但结果是一样的

将行插入表的例程如下所示:

def insert(sql, values):
#insert a single row of data from the input file
    connection = getconn()
    with connection.cursor() as cursor:
        try:
            cursor.execute(sql, values)
        #except ValueError:
            #print('Value error from pymysql')
        finally:
            cursor.close()

返回以下ValueError:

ValueError:索引69处不支持格式字符“,”(0x2c)

但是,如果提取数据值并通过串联将其插入到sqlstring中,则得到:

insert into contacts (nickname,fullname,cell,email,updatedt) values('laurelai','Laurelai Smith','','lsmith@domain.net','2017-05-04 13:27:47')

这将无错误地插入行

是什么导致了ValueError?你知道吗


Tags: sqlemaildomain错误nicknamecellcursorinsert