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()
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    TCP,UDP协议下的socket通信
    socket套接字
    野指针
    各种数据类型与零值比较规范
    【C++复习】1.1基本概念
    【转载】C++中的static关键字的总结
    Windows10下搭建汇编语言开发环境(利用DOSBOX和MASM32)
    初试ARM开发板
    命令行中编译和运行JAVA程序出现“错误: 找不到或无法加载主类”
    用c语言产生随机数的方法
  • 原文地址:https://www.cnblogs.com/lzcys8868/p/7888752.html
Copyright © 2011-2022 走看看