zoukankan      html  css  js  c++  java
  • python 基础 9.9 查询数据

     
    #/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(" ")
            cus.close()
            cnx.commit()
        except Exception as e:
            cnx.rollback()
            raise e
        finally:
            cnx.close()
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    【转】CTF-Born2root's-WriteUP
    Win10 64位+VS2015+Opencv3.3.0安装配置
    C++ bitset 用法
    未来的一个要参加蓝桥杯,在这里记录下笔记
    一些漏洞测试利用脚本
    Linux下抓取登陆用户密码神器mimipenguin
    免费在线验证码接收平台
    kali linux 安装 Mysql Can't read from messagefile 报错解决方案
    FPGA实现UHS的一些资料
    CYPRESS USB芯片win10驱动
  • 原文地址:https://www.cnblogs.com/lzcys8868/p/7888752.html
Copyright © 2011-2022 走看看