ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

python数据库连接--mysql(pymysql)

2019-08-28 11:02:12  阅读:190  来源: 互联网

标签:None python pymysql cursor book cursor1 connect mysql


--------安装

 pip install pymysql

 

-------主要方法

connect():
commit():事务提交,如果没有设为自动提交,则每次操作后必须提交事务,否则操作无效
rollback():操作出错时,可以用这个函数回滚到执行事务之前



--------------.简单示例:连接数据库

1.数据库中查询出的数据

SELECT book_id,book_name FROM t_book WHERE market_rule=1

 

 

2.连接数据库

import  pymysql
import types

#连接数据库
connect=pymysql.connect(host="192.168.6.41",user="lrtsaudio",password="2&Ty3DW75i!(vgo.l3Odp1fgWgEG",port=3306,db="audiobook")
#使用cursor()方法创建一个游标对象
cursor=connect.cursor()
#使用游标的execute()方法执行sql语句
cursor.execute("SELECT book_id,book_name FROM t_book WHERE market_rule=1")
#使用fetchall()获取全部数据
r1=cursor.fetchall()
print(r1)
print(type(r1))
#关闭游标连接
cursor.close()
#关闭数据库连接
connect.close()

结果如下:(返回结果为元组)

 

 

也可以通过字典来传递参数:效果是一样的

dbinfo={"host":"192.168.6.41",
        "user":"lrtsaudio",
        "password":"2&Ty3DW75i!(vgo.l3Odp1fgWgEG",
        "db":"audiobook"
}

sql="SELECT * FROM t_book WHERE market_rule=1"
connect1=pymysql.connect(**dbinfo)
cursor1=connect1.cursor()
cursor1.execute(sql)
r2=cursor1.fetchall()
print(r2)
cursor1.close()
connect1.close

 

 -------connect()各个参数代表的意思

    host=None,          # 要连接的主机地址
    user=None,          # 用于登录的数据库用户
    password='',        # 密码
    database=None,      # 要连接的数据库
    port=0,             # 端口,一般为 3306
    unix_socket=None,   # 选择是否要用unix_socket而不是TCP/IP
    charset='',         # 字符编码
    sql_mode=None,      # Default SQL_MODE to use.
    read_default_file=None, # 从默认配置文件(my.ini或my.cnf)中读取参数
    conv=None,          # 转换字典
    use_unicode=None,   # 是否使用 unicode 编码
    client_flag=0,      # Custom flags to send to MySQL. Find potential values in constants.CLIENT.
    cursorclass=<class 'pymysql.cursors.Cursor'>, # 选择 Cursor 类型
    init_command=None,  # 连接建立时运行的初始语句 
    connect_timeout=10, # 连接超时时间,(default: 10, min: 1, max: 31536000)
    ssl=None,           # A dict of arguments similar to mysql_ssl_set()'s parameters.For now the capath and cipher arguments are not supported. 
    read_default_group=None, # Group to read from in the configuration file.
    compress=None,      # 不支持
    named_pipe=None,    # 不支持
    no_delay=None,      # 
    autocommit=False,   # 是否自动提交事务
    db=None,            # 同 database,为了兼容 MySQLdb
    passwd=None,        # 同 password,为了兼容 MySQLdb
    local_infile=False, # 是否允许载入本地文件
    max_allowed_packet=16777216, # 限制 `LOCAL DATA INFILE` 大小
    defer_connect=False, # Don't explicitly connect on contruction - wait for connect call.
    auth_plugin_map={}, #
    read_timeout=None,  # 
    write_timeout=None, 
    bind_address=None   # 当客户有多个网络接口,指定一个连接到主机

 

 

------查询数据

import  pymysql

dbinfo={"host":"192.168.6.41",
        "user":"lrtsaudio",
        "password":"2&Ty3DW75i!(vgo.l3Odp1fgWgEG",
        "db":"audiobook"
}

sql1="SELECT book_id,book_name FROM t_book WHERE market_rule=1"
sql2="SELECT * FROM audiobook.w_activity_ticket_3 WHERE user_id=234739503"
connect1=pymysql.connect(**dbinfo)
cursor1=connect1.cursor()
cursor1.execute(sql1)  #返回值未为受影响的行数,如下:
"""
num=cursor1.execute(sql1)
print(num)  #结果:num=8
"""
r_all=cursor1.fetchall()#取出全部查询结果
r_one=cursor1.fetchone()#取出一行查询结果。从第一行开始取
r_many=cursor1.fetchmany(size=2)#取出其中几行查询结果
print(r_all)
print(r_one)
print(r_many)

cursor1.close()
connect1.close()

注释掉fetchall的代码后结果:

!!!也就是说:

如fetchall(),fetchmany(),fetchone()同时作用于同一个查询时,每个方法执行开头是上一个方法执行的结尾

 

标签:None,python,pymysql,cursor,book,cursor1,connect,mysql
来源: https://www.cnblogs.com/youzaijiang/p/11344933.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有