43 pythonl操作pymysql

pythonl操作pymysql

1.先导入pymysql

(1)
import pymysql

# 连接mysql服务器

conn=pymysql.connect(host='locahost',user='root',password='123',databases='db2',charset='utf8')
# host='主机' ,user='用户', password='密码 ',databases='表',charset='utf8'

# 获取游标
cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)

# 创建数据库

sql='create table db1'

# 插入数据
cursor.execute(sql)


#取出一个
res = cursor.fetchone()


#取来全部 结果为列表里面套字典
# res=cursor.fetchall()

#取出 10个
# res = cursor.fetchmany(10)


print(res)

# 关闭游标
cursor.close()
# 关闭连接
conn.close()

(2)
import pymysql

conn=pymysql.connect(host='locahost',user='root',password='123',database='db1',charset='utf8')

cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)


sql = 'create datle db1'

cursor.execute(sql)

# 当需要删除和更新的时候,需要食物移交
conn.commit()

cursor.close()
conn.close()


# 参数列表:
#
# 参数 描述
# host 数据库服务器地址,默认 localhost
# user 用户名,默认为当前程序运行用户
# password 登录密码,默认为空字符串
# database 默认操作的数据库
# port 数据库端口,默认为 3306
# bind_address 当客户端有多个网络接口时,指定连接到主机的接口。参数可以是主机名或IP地址。
# unix_socket unix 套接字地址,区别于 host 连接
# read_timeout 读取数据超时时间,单位秒,默认无限制
# write_timeout 写入数据超时时间,单位秒,默认无限制
# charset 数据库编码
# 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
# ssl A dict of arguments similar to mysql_ssl_set()’s parameters. For now the capath and cipher arguments are not supported.
# read_default_group Group to read from in the configuration file.
# compress Not supported
# named_pipe Not supported
# 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)
# server_public_key SHA256 authenticaiton plugin public key value. (default: None)
# db 参数 database 的别名
# passwd 参数 password 的别名
# binary_prefix Add _binary prefix on bytes and bytearray. (default: False)

# 设置游标类型
# 查询时,默认返回的数据类型为元组,可以自定义设置返回类型。支持5种游标类型:
#
# Cursor: 默认,元组类型
#
# DictCursor: 字典类型
#
# DictCursorMixin: 支持自定义的游标类型,需先自定义才可使用
#
# SSCursor: 无缓冲元组类型
#
# SSDictCursor: 无缓冲字典类型
#
# 无缓冲游标类型,适用于数据量很大,一次性返回太慢,或者服务端带宽较小时




猜你喜欢

转载自www.cnblogs.com/komorebi/p/11025523.html
43