Pyodbc & SQL Server-TypeError: not all arguments converted during string formatting

Pyodbc & SQL Server-TypeError: not all arguments converted during string formatting

我正在尝试从一个数据库中读取,然后写入另一台机器上的另一个数据库。我收到 TypeError: not all arguments converted during string formatting

这是我的代码:

import pyodbc
#read from db
read_db = pyodbc.connect(driver='{SQL Server}', host='hostname', databse='DBa', user='user', password='pwd')
cur = read_db.cursor()
cur.execute("SELECT * FROM origin_table")

#write to db
write_db = pyodbc.connect(driver='{SQL Server}', host='hostname', databse='DBb', user='user', password='pwd')

#get rows from read  
rows = cur.fetchall()

#make list, init
read_list = []
i=0

#add row, write
for row in rows:
    read_list.append(row)
    params = ['%s' for item in read_list]
    sql = "INSERT INTO destination_table (col1, col2, col3, col4, col5, col6, col7) VALUES (%%s,%%s,%%s,%%s,%%s,%%s,%%s);" % ','.join(params)
    cur2 = write_db.cursor()
    cur2.execute(sql, params)

读取的输出如下所示:

('Fname', 'Lname', u'THIS', 'THAT', Decimal('0'), datetime.datetime(2015, 6, 20, 3, 26, 47), u'THE OTHER')

我已经尝试了很多不同的解决方案,但我发现只有 TypeError: 是最接近的。 我需要做一些显式类型 conversion/specification 吗?

当我三重引用 SQL 并删除转义符 % 时,如下所示:

"""INSERT INTO destination_table (col1, col2, col3, col4, col5, col6, col7) VALUES (%s,%s,%s,%s,%s,%s,%s);""" % ','.join(params)

我得到的错误是TypeError: not enough arguments for format string

更新:','.join(params) 更改为 tuple(params)(如下所示),我仍然得到 TypeError: not enough arguments for format string

您遇到的问题是参数应该是一个元组。

"""INSERT INTO destination_table (col1, col2, col3, col4, col5, col6, col7) VALUES (%s,%s,%s,%s,%s,%s,%s);""" % tuple(params)

使用 join 函数将尝试将 "params" 转换为一个字符串变量。因此它会尝试将其全部推送到您的第一个“%s”中。

要验证您的输入 "params" 是否有足够的元素,请尝试将其打印出来(或使用调试器)。要打印:

for row in rows:
    read_list.append(row)
    params = ['%s' for item in read_list]
    print(params)
    query = """INSERT INTO destination_table (col1, col2, col3, col4, col5, col6, col7) VALUES (%s,%s,%s,%s,%s,%s,%s);""" % tuple(params)
    print(query)

不需要整个 ... % tuple(params) 部分。在遇到一些 ...not enough parameters... 错误后,我只是将参数列表放入 cur2.excecute

这是固定块:

for row in rows:
    read_list.append(row)
    sql = "INSERT INTO destination_table (col1, col2, col3, col4, col5, col6, col7) VALUES (?,?,?,?,?,?,?);"
    cur2 = write_db.cursor()
    cur2.execute(sql, read_list[i])

以及在控制台中使用临时日志记录的全部内容:

import time
import pyodbc

read_list = []
i=0

read_db = pyodbc.connect(driver='{SQL Server}', host='read_host', databse='read_db', user='read_user', password='read_pw')
cur = read_db.cursor()
cur.execute("SELECT * FROM read_table")
rows = cur.fetchall()

write_db = pyodbc.connect(driver='{SQL Server}', host='write_host', databse='write_db', user='write_user', password='write_pw')

while True:
    start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    print(10*"/-/")
    print("Start " + str(start_time))
    for row in rows:
        read_list.append(row)
        sql = """INSERT INTO write_table (col1, col2, col3, col4, col5, col6, col7) VALUES (?,?,?,?,?,?,?);"""
        cur2 = write_db.cursor()
        cur2.execute(sql, read_list[i])
        cur2.commit()
        i+=1
    print("End, wait 30 minutes")
    time.sleep(1800)
    i=0