#/usr/bin/python
#-*- coding:utf-8 -*-
#@Time   :2017/11/24 4:21
#@Auther :liuzhenchuan
#@File   :查询数据.py
 
# 查询数据库
import codecs
 
import MySQLdb
 
select_student = '''select * from student where stdname in (select stdname from student  group by stdname having count(1)>1) order by stdname;'''
def connect_mysql():
    db_config = {
        "host": "192.168.16.70",
        "port": 3306,
        "user": "root",
        "passwd": "123123",
        "db": "students",
        "charset": "utf8"
    }
    try:
        cnx = MySQLdb.connect(**db_config)
    except Exception as e:
        raise e
    return cnx
 
 
if __name__ == "__main__":
    cnx = connect_mysql()
    cus = cnx.cursor()
    try:
        cus.execute(select_student)
        result = cus.fetchall()
        print(result)
        with codecs.open("select.txt", "w") as f:
            for line in result:
                f.write(str(line))
                f.write("\n")
        cus.close()
        cnx.commit()
    except Exception as e:
        cnx.rollback()
        raise e
    finally:
        cnx.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

相关文章:

  • 2021-04-10
  • 2021-04-30
  • 2022-01-08
  • 2021-05-28
  • 2021-07-29
  • 2021-10-01
  • 2022-01-21
  • 2022-12-23
猜你喜欢
  • 2021-08-30
  • 2021-09-20
  • 2021-04-11
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-12-30
相关资源
相似解决方案