【简介】
使用过java jdbc的相比都知道其中的繁琐程序,python连接数据库的异常简便简直让我眼前一亮,没有繁琐的配置,只用几行代码即可实现连接,简直清爽的八达鸟。
【实战动手】
数据库连接配置,并创建后一个后期实验需要使用的数据库
# 导入pymysql模块
import pymysql
# 连接database
conn = pymysql.connect(
host="127.0.0.1",
#你的主机
user="root",
#你的数据库用户名
password="123456",
#你的数据库密码
database="test",
#本次操作所要使用的数据库
charset="utf8"
)
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
# 定义要执行的SQL语句
sql = """
CREATE TABLE USER1 (
id INT auto_increment PRIMARY KEY ,
name CHAR(10) NOT NULL UNIQUE,
age TINYINT NOT NULL
)ENGINE=innodb DEFAULT CHARSET=utf8;
"""
try:
cursor.execute(sql,(username)
cur
except Exception as e:
#有异常,回滚事务
print(str(e))
conn.rollback()
# 关闭光标对象
cursor.close()
# 关闭数据库连接
conn.close()
增
# 导入pymysql模块
import pymysql
# 连接database
conn = pymysql.connect(
host="127.0.0.1",
user="root",
password="123456",
database="test",
charset="utf8"
)
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
# 定义要执行的SQL语句
sql = """
insert into user1(name,age) values(%s,%s);
"""
# 执行SQL语句
username="hehe"
age=66
data = [("aa",11),("bb",10),("cc",12)]
try:
# 单个数据插入
cursor.execute(sql,(username,age))
# 批量提交
cursor.executemany(sql,data)
# 提交事务
conn.commit()
last_id=cursor.lastrowid
print(last_id)
except Exception as e:
#有异常,回滚事务
print(str(e))
conn.rollback()
# 关闭光标对象
cursor.close()
# 关闭数据库连接
conn.close()
删
# 导入pymysql模块
import pymysql
# 连接database
conn = pymysql.connect(
host="127.0.0.1",
user="root",
password="123456",
database="test",
charset="utf8"
)
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
# 定义要执行的SQL语句
sql_del="""
delete from user1 where id=%s;
"""
try:
# 批量删除数据
cursor.executemany(sql_del,[(3),(9),(10),(11),(12),(16),(17),(18),(19)])
last_id=cursor.lastrowid
print(last_id)
except Exception as e:
#有异常,回滚事务
print(str(e))
conn.rollback()
# 关闭光标对象
cursor.close()
# 关闭数据库连接
conn.close()
改
# 导入pymysql模块
import pymysql
# 连接database
conn = pymysql.connect(
host="127.0.0.1",
user="root",
password="123456",
database="test",
charset="utf8"
)
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
# 定义要执行的SQL语句
sql_update="""
update user1 set age=%s where name=%s
"""
# 执行SQL语句
try:
#更新数据
cursor.execute(sql_update,(66,"Alex"))
last_id=cursor.lastrowid
print(last_id)
except Exception as e:
#有异常,回滚事务
print(str(e))
conn.rollback()
# 关闭光标对象
cursor.close()
# 关闭数据库连接
conn.close()
查
# 导入pymysql模块
import pymysql
# 连接database
conn = pymysql.connect(
host="127.0.0.1",
user="root",
password="123456",
database="test",
charset="utf8"
)
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()
# 定义要执行的SQL语句
sql_search="""
select * from user1
"""
sql_text="""
select * from user1 where id = 1;
"""
# 执行SQL语句
username="hehe"
age=66
data = [("aa",11),("bb",10),("cc",12)]
try:
# 查询信息
cursor.execute(sql_search)
search_rs=cursor.fetchall()
print("id","name","password")
for row in search_rs:
id=row[0]
name=row[1]
age=row[2]
print(id,name,age,row)
# 查询第一条数据
cursor.execute(sql_text)
ret=cursor.fetchone()
print(ret)
# 查询多条数据,但是不是一条数据
cursor.execute("select * from user1")
rs=cursor.fetchmany(2)
# fetchmany中的参数就是要获取的数据条数
for i in rs:
print(i)
print("查询到的数据条数:",len(rs))
last_id=cursor.lastrowid
print(last_id)
except Exception as e:
#有异常,回滚事务
print(str(e))
conn.rollback()
# 关闭光标对象
cursor.close()
# 关闭数据库连接
conn.close()