python:连接时输入错误

时间:2014-05-31 10:33:24

标签: python mysql sql

enter image description here 我刚刚在这个代码上遇到错误,比如

    if rows[0] in line:
TypeError: 'in <string>' requires string as left operand, not tuple

我的代码:

import MySQLdb

db = MySQLdb.connect(host="localhost", # your host, usually localhost
                         user="root", # your username
                          passwd="mysql", # your password
                          db="sakila") # name of the data base
cursor = db.cursor()

    # execute SQL select statement
cursor.execute("SELECT student FROM myclass")
rows=cursor.fetchall() 


with open('qwer.txt','r') as file:
    for line in file:
        for row in rows:
            if rows[0] in line:
               stmt="select english from marks where student = :student"
               cursor.execute(stmt,dict(student=row[0]))
               print "english marks:",cursor.fetchall()[0][0]

请帮助,因为它没有为我提供任何没有错误的输出

1 个答案:

答案 0 :(得分:1)

您正在使用rows[0],而您可能打算使用row[0]

for row in rows:
    if row[0] in line:

rows是元组列表,因此rows[0]是第一个这样的元组。 row[0]是当前行中的第一列(例如student中的SELECT列。)

相关问题