Python操作MySQL数据库模块pymysql

Python操作MySQL数据库模块pymysql

pymysql用法

import pymysql

#创建MySQL链接对象
conn = pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    password='',
    charset='utf8',
    db='database')
#得到一个可以执行sql语句的光标对象
cursor = conn.cursor()
#定义要执行的sql语句
sql = 'select * from table_name where id=1'
#执行sql语句
ret=cursor.execute(sql)
#关闭光标对象
cursor.close()
#关闭数据库链接
conn.close()

#ret返回的不是具体的MySQL数据,如果查询到数据,返回值为受影响的行数,如果没查询到,返回值为0

返回字典格式数据

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

插入操作

import pymysql

conn = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='',
        db='info',
        charset='utf8')

name = input('name:')
pwd = input('password:')

cursor = conn.cursor()

sql = 'insert into table_name(name,pwd) values (%s,%s);'

try:
    cursor.execute(sql,[name,pwd])
    #提交事务
    conn.commit()
    #操作单条数据获取被插入数据的id方法
    last_id = cursor.lastrowid
except Exception as e:
    #有异常回滚事务
    conn.rollback()

cursor.close()
conn.close()

批量执行

...
sql = "insert into table_name values (%s,%s);"
data = [("name1","pwd1"),("name2","pwd2"),("name3","pwd3"),...]
#使用executmany()批量执行sql语句
cursor.executemany(sql,data)
...

删除操作

import pymysql

conn = pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    password='',
    charset='utf8',
    db='info'
)

cursor=conn.cursor()
sql='delete from userinfo WHERE id=%s'

cursor.execute(sql,id)
conn.commit()
cursor.close()
conn.close()

更改操作

import pymysql

conn = pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    password='',
    charset='utf8',
    db='info'
)

cursor=conn.cursor()

sql = "UPDATE table_name SET pwd=%s WHERE name=%s;"

try:
    # 执行SQL语句
    cursor.execute(sql, [pwd, username])
    # 提交事务
    conn.commit()
except Exception as e:
    # 有异常,回滚事务
    conn.rollback()
cursor.close()
conn.close()

查询操作

# 导入pymysql模块
import pymysql
# 连接database
conn = pymysql.connect(
        host='你的数据库地址',
        user='用户名',
        password='密码',
        database='数据库名',
        charset='utf8')
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
# 查询数据的SQL语句
sql = "SELECT id,name,age from USER1 WHERE id=1;"
# 执行SQL语句
cursor.execute(sql)
# 获取单条查询数据
ret = cursor.fetchone()
cursor.close()
conn.close()
# 打印下查询结果
print(ret)

查询多条数据

# 导入pymysql模块
import pymysql
# 连接database
conn = pymysql.connect(
        host='你的数据库地址',
        user='用户名',
        password='密码',
        database='数据库名',
        charset='utf8')
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
# 查询数据的SQL语句
sql = "SELECT id,name,age from table_name;"
# 执行SQL语句
cursor.execute(sql)
# 获取多条查询数据
ret = cursor.fetchall()
cursor.close()
conn.close()
# 打印下查询结果
print(ret)

进阶用法

# 可以获取指定数量的数据
cursor.fetchmany(3)
# 光标按绝对位置移动1
cursor.scroll(1, mode='absolute')
# 光标按照相对位置(当前位置)移动1
cursor.scroll(1, mode='relative')

转载于:https://my.oschina.net/dongchunlei/blog/1681691

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值