pymsql是Python中操作MySQL的模块,其使用方法和py2的MySQLdb几乎相同。

模块安装

1
pip install pymysql

执行sql语句

MYSQL进阶
import pymysql

#添加数据

conn
= pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='yyy')

cursor = conn.cursor()

# sql = """CREATE TABLE EMPLOYEE (

FIRST_NAME CHAR(20) NOT NULL,

LAST_NAME CHAR(20),

AGE INT,

SEX CHAR(1),

INCOME FLOAT )"""

cursor.execute(sql)

#row_affected = cursor.execute("create table t1(id INT ,name VARCHAR(20))")

#row_affected=cursor.execute("INSERT INTO t1(id,name) values (1,'alvin'),(2,'xialv')")

#cursor.execute("update t1 set name = 'silv2' where id=2")

#查询数据
row_affected=cursor.execute("select * from t1")
one
=cursor.fetchone()

# many=cursor.fetchmany(2)

all=cursor.fetchall()

#scroll

cursor.scroll(-1,mode='relative') # 相对当前位置移动

#cursor.scroll(2,mode='absolute') # 相对绝对位置移动

#更改获取数据结果的数据类型,默认是元组,可以改为字典等:conn.cursor(cursor=pymysql.cursors.DictCursor)

conn.commit()
cursor.close()
conn.close()

MYSQL进阶

事务

事务命令

事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功。

数据库开启事务命令     

--        start transaction 开启事务
--        Rollback 回滚事务,即撤销指定的sql语句(只能回退insert delete update语句),回滚到上一次commit的位置
--        Commit 提交事务,提交未存储的事务
-- 
--        savepoint 保留点 ,事务处理中设置的临时占位符 你可以对它发布回退(与整个事务回退不同)     

转账实例:

1
2
UPDATE account set balance=balance-5000 WHERE name=”yuan”;
UPDATE account set balance=balance+5000 WHERE name=”xialv”;
create table test2(id int PRIMARY KEY auto_increment,name VARCHAR(20)) engine=innodb;
INSERT INTO test2(name) VALUE ("alvin"),
                              ("yuan"),
                              ("xialv");

start transaction;
insert into test2 (name)values('silv');
select * from test2;
commit;

-- 保留点

start
transaction;
insert into test2 (name)values('wu');
savepoint insert_wu;
select * from test2;

delete from test2 where id=4;
savepoint delete1;
select * from test2;

delete from test2 where id=1;
savepoint delete2;
select * from test2;

rollback to delete1;

select * from test2;

savepoint

python中调用数据库启动事务的方式

import pymysql

添加数据

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='yyy')

cursor = conn.cursor()

try:
insertSQL0="INSERT INTO ACCOUNT2 (name,balance) VALUES ('oldboy',4000)"
insertSQL1
="UPDATE account2 set balance=balance-30 WHERE name='yuan'"
insertSQL2
="UPDATE account2 set balance=balance+30 WHERE name='xialv'"

</span><span style="color: #0000ff;">cursor</span> <span style="color: #808080;">=</span> conn.<span style="color: #0000ff;">cursor</span><span style="color: #000000;">()

</span><span style="color: #0000ff;">cursor</span>.<span style="color: #0000ff;">execute</span><span style="color: #000000;">(insertSQL0)
conn.</span><span style="color: #0000ff;">commit</span><span style="color: #000000;">()

</span><span style="color: #0000ff;">cursor</span>.<span style="color: #0000ff;">execute</span><span style="color: #000000;">(insertSQL1)
raise Exception
</span><span style="color: #0000ff;">cursor</span>.<span style="color: #0000ff;">execute</span><span style="color: #000000;">(insertSQL2)
</span><span style="color: #0000ff;">cursor</span>.<span style="color: #0000ff;">close</span><span style="color: #000000;">()
conn.</span><span style="color: #0000ff;">commit</span><span style="color: #000000;">()

except Exception as e:

conn.</span><span style="color: #0000ff;">rollback</span><span style="color: #000000;">()
conn.</span><span style="color: #0000ff;">commit</span><span style="color: #000000;">()

cursor.close()
conn.
close()

View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-01-21
  • 2021-07-24
猜你喜欢
  • 2021-12-27
  • 2021-10-18
  • 2022-02-21
相关资源
相似解决方案