pymysql 参数详解及使用

python使用pymysql库连接MySQL数据库

命令行安装:

conda install pymysql

或者直接在pycharm中,搜索pymysql库安装,装完之后就可以看其具体的参数了

user                #数据库登录用户名,默认为当前程序运行用户
password            #登录密码,默认为空字符串
host                #数据库服务器地址,默认 localhost
database            #操作数据库
port                #端口,默认 3306
unix_socket         #unix 套接字地址,区别于 host 连接
charset             #数据库编码,一般设置 utf8
sql_mode            #指定默认的 SQL_MODE
read_default_file   #Specifies my.cnf file to read these parameters from under the [client] section.
conv                #Conversion dictionary to use instead of the default one. This is used to provide custom marshalling and unmarshaling of types.
use_unicode         #Whether or not to default to unicode strings. This option defaults to true for Py3k.
client_flag         #Custom flags to send to MySQL. Find potential values in constants.CLIENT.
cursorclass         #设置默认的游标类型
init_command        #当连接建立完成之后执行的初始化 SQL 语句
connect_timeout     #连接超时时间,默认 10,最小 1,最大 31536000
read_default_group  #Group to read from in the configuration file.
autocommit          #是否自动提交,默认不自动提交,参数值为 None 表示以服务器为准
local_infile        #Boolean to enable the use of LOAD DATA LOCAL command.(default: False)
max_allowed_packet  #发送给服务器的最大数据量,默认为 16MB
defer_connect       #是否惰性连接,默认为立即连接
auth_plugin_map     #A dict of plugin names to a class that processes that plugin. The class will take the Connection object as the argument to the constructor. The class needs an authenticate method taking an authentication packet as an argument. For the dialog plugin, a prompt(echo, prompt) method can be used (if no authenticate method) for returning a string from the user. (experimental)
read_timeout        #读取数据超时时间,单位秒,默认无限制
write_timeout       #写入数据超时时间,单位秒,默认无限制
bind_address        #当客户端有多个网络接口时,指定连接到主机的接口。参数可以是主机名或IP地址。
binary_prefix       #Add _binary prefix on bytes and bytearray. (default: False)
ssl                 #A dict of arguments similar to mysql_ssl_set()’s parameters. For now the capath and cipher arguments are not supported.
db                  #database 的别名
passwd              #password 的别名

使用:

import pandas as pd
import pymysql as sq

#建立连接
coon = sq.connect(user="test", password="test123", host="192.168.1.1", database="chengji", port=3306, charset="utf8")
#连接完数据库,接着就是获取游标,之后才能进行执行、提交等操作
cur = coon.cursor()
sql = "select * from score202101 limit 100"
#执行SQL语句
cur.execute(sql)
#获取全部数据,"获取一定量数据:fetchmany(size),获取一条数据:fetchone()"
data = cur.fetchall()
da = pd.DataFrame(data)
print(da)

 

其中参数参考链接:https://www.jianshu.com/p/30b6d6f0b4d1

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旋转小马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值