0

我正在尝试通过 Python 2.7 更新 MySQL 表。我已经阅读并尝试了许多解决方案并不断收到错误消息:TypeError: not all arguments convert during string formatting

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?
我是编程新手,非常感谢您的帮助。

谢谢。

4

1 回答 1

2

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

cursor.execute("UPDATE tex SET base_name = %s WHERE company_name = %s",
               (base_name, company_name,))
于 2013-04-28T11:37:40.960 回答