错误“TypeError:不是在字符串格式化期间转换的所有参数”MySQLdb

时间:2013-04-28 11:34:42

标签: mysql python-2.7 mysql-python

我正在尝试通过Python 2.7更新MySQL表。我已阅读并尝试了许多解决方案,并不断收到错误消息:TypeError:并非在字符串格式化期间转换所有参数

import MySQLdb
import jellyfish

db = MySQLdb.connect('localhost','root','12badger12','group2')
cursor = db.cursor()

sql_com_dept = "SELECT id, bus_name FROM com_dept"

sql_tex = "SELECT company_name FROM tex"

score = 0

cursor.execute(sql_tex)

company_count = 10

while company_count >= 1:

    tex_tup = cursor.fetchone()
    company_name = tex_tup
    company = str(company_name)

    cursor.execute(sql_com_dept)

    dept_count = 10

    while dept_count >= 1:

        dept_tup = cursor.fetchone()
        dept_name = dept_tup
        dept = str(dept_name)

        lev = jellyfish.levenshtein_distance(dept, company)
        jar = jellyfish.jaro_distance(dept, company)
        dam = jellyfish.damerau_levenshtein_distance(dept, company)

        score_in = (lev + jar + dam) / 3

        if score_in >= score:
            base_name = dept
            base_id = dept_id
            score = score_in                      
        else:
            pass 

        dept_count = dept_count - 1

    cursor.execute("UPDATE tex SET base_name = $s WHERE company_name = $s",
    (base_name, company_name,))
    db.commit()

    company_count = company_count - 1
    print company_count

cursor.close()
db.close()
print "Matching Complete!!!"

如何更新字段base_name?
我是编程新手,非常感谢你的帮助。

感谢。

1 个答案:

答案 0 :(得分:2)

我很确定mysqldb期望%s而不是$s占位符......

cursor.execute("UPDATE tex SET base_name = %s WHERE company_name = %s",
               (base_name, company_name,))