zoukankan      html  css  js  c++  java
  • Python与MySQL的交互

    Python与MySQL的交互

    1 、安装mysql模块

    pip3 install pymysql

    2 、connection 对象

    用于建立与数据库的连接

    2.1  创建对象

    conn = connection(参数列表)

    2.1.1 参数列表

    1. host:连接的mysql主机,如果本机是'localhost'
    2. port:连接的mysql主机的端口,默认是3306
    3. db:数据库的名称
    4. user:连接的用户名
    5. password:连接的密码
    6. charset:通信采用的编码方式,默认是'gb2312',要求与数据库创建时指定的编码一致,否则中文会乱码 对象的方法
    import pymysql
    # 获取一个数据库连接
    connection =  pymysql.connect(host='192.168.242.128',port=3306,user='root',password='123456',db='test1',charset='utf8')
    #获取和数据库交互的对象
    cursor = connection.cursor()
    # 要执行的sql
    sql = 'select * from emp'
    # 获取Ssql 执行后的数据
    cursor.execute(sql)
    result = cursor.fetchall()
    for res in result:
        print(res)
  • 相关阅读:
    jQuery 核心
    Js实现内容向上无缝循环滚动
    浅析CSS postion属性值用法
    JS原生Ajax请求
    详解SQL集合运算
    Windows上开启IIS
    poj 4618 暴力
    hdu 4614 线段树
    poj 3468 线段树
    hdu 1698 线段树成段更新
  • 原文地址:https://www.cnblogs.com/whm1012/p/8494895.html
Copyright © 2011-2022 走看看