通过file中的字段查询MySQL内容

# -*- coding: utf-8 -*-

import MySQLdb


with open(r"./user.txt", "r") as f:

    f.readline()
    for line in f:
        userid, user_name, user_password = line.split()
        userid=int(userid)

        # 打开数据库连接
        db = MySQLdb.connect("localhost", "root", "123456", "hotel")
        # 使用cursor()方法获取操作游标
        cursor = db.cursor()
        sql = "SELECT * from user where user_id=%d" % userid
        # 使用execute方法执行SQL语句
        cursor.execute(sql)
        # 使用 fetchone() 方法获取一条数据库。
        result = cursor.fetchone()

        id, user_name, user_password = result

        print id, user_name, user_password

        # 关闭数据库连接
        db.close()
原文地址:https://www.cnblogs.com/hanyu258/p/7737888.html