Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口
不同的数据库你需要下载不同的DB API模块,例如你需要访问Oracle数据库和Mysql数据,你需要下载Oracle和MySQL数据库模块。DB-API 是一个规范. 它定义了一系列必须的对象和数据库存取方式, 以便为各种各样的底层数据库系统和多种多样的数据库接口程序提供一致的访问接口 。Python的DB-API,为大多数的数据库实现了接口,使用它连接各数据库后,就可以用相同的方式操作各数据库。Python DB-API使用流程:
# pip install pymysql
import pymysql
# 源码中有连接参数
db = pymysql.connect(host="127.0.0.1", user="root", password="root", database="test", port=3306, charset="utf8")
# 通过连接获取游标
cursor = db.cursor()
cursor.execute("select version()")
result = cursor.fetchone()
print(result)
cursor.close()
db.close()
# 创建一个连接访问数据库的工具类
class MysqlHelp(object):
# 初始化方法,来进行参数设置
def __init__(self, host, db, user, passwd, port=3306, charset='utf8'):
self.host = host
self.user = user
self.port = port
self.passwd = passwd
self.db = db
self.charset = charset
def open_conn(self):
self.conn = pymysql.connect(host=self.host, user=self.user, password=self.passwd, database=self.db,port=self.port, charset=self.charset)
self.cursor = self.conn.cursor()
def close(self):
self.cursor.close()
self.conn.close()
def get_db_version(self, sql):
try:
self.open_conn()
self.cursor.execute(sql)
return self.cursor.fetchone()
except Exception as error:
print(error)
finally:
self.close()
if __name__ == "__main__":
sqlHelp = MysqlHelp("127.0.0.1", "test", "root", "root")
print(sqlHelp.get_db_version("select version()"))
import pymysql
# 创建一个连接访问数据库的工具类
class MysqlHelp(object):
# 初始化方法,来进行参数设置
def __init__(self, host, db, user, passwd, port=3306, charset='utf8'):
self.host = host
self.user = user
self.port = port
self.passwd = passwd
self.db = db
self.charset = charset
def open_conn(self):
self.conn = pymysql.connect(host=self.host, user=self.user, password=self.passwd, database=self.db,
port=self.port, charset=self.charset)
self.cursor = self.conn.cursor()
def close(self):
self.cursor.close()
self.conn.close()
def get_db_version(self, sql):
try:
self.open_conn()
self.cursor.execute(sql)
return self.cursor.fetchone()
except Exception as error:
print(error)
finally:
self.close()
# 此方法完成update insert delete删除的功能
def update(self, sql, parmas):
try:
self.open_conn()
# insert update delete 返回的都是受影响的行数
res = self.cursor.execute(sql, parmas)
# 手动提交事务
self.conn.commit()
return res
except Exception as error:
print(error)
finally:
self.close()
if __name__ == "__main__":
sqlHelp = MysqlHelp("127.0.0.1", "test", "root", "root")
print(sqlHelp.get_db_version("select version()"))
res = sqlHelp.update("insert into article (title, content, avatar) values (%s,%s,%s)", ['标题a', '内容a', '昵称a'])
print(res)
res = sqlHelp.update("update article set title = %s, content=%s, avatar=%s where id=%s",('标题A', '内容A', '昵称A',1))
print(res)
res = sqlHelp.update("delete from article where id=%s", {2})
print(res)
import pymysql
# 创建一个连接访问数据库的工具类
class MysqlHelp(object):
def get_by_id(self, sql, id):
try:
self.open_conn()
self.cursor.execute(sql, id)
# 如果是查询则需要使用fetchone fetchall方法
return self.cursor.fetchone()
except Exception as error:
print(error)
finally:
self.close()
# 根据某个字段进行模糊查询
def query_by_name(self, sql, name):
try:
self.open_conn()
self.cursor.execute(sql, name)
# 如果是查询则需要使用fetchone fetchall方法
return self.cursor.fetchall()
except Exception as error:
print(error)
finally:
self.close()
def query_split_page(self, sql, keyword, currentPage=1, size=5):
try:
self.open_conn()
self.cursor.execute(sql, (f'%{keyword}%', (currentPage - 1) * size, size))
# 如果是查询则需要使用fetchone fetchall方法
return self.cursor.fetchall()
except Exception as error:
print(error)
finally:
self.close()
if __name__ == "__main__":
sqlHelp = MysqlHelp("127.0.0.1", "test", "root", "root")
print(sqlHelp.get_db_version("select version()"))
res = sqlHelp.get_by_id(sql="select * from article where id=%s", id=1)
print(res)
res = sqlHelp.query_by_name(sql="select * from article where title like %s", name='%%')
print(res)
res = sqlHelp.query_split_page(sql="select * from article where title like %s limit %s,%s", keyword='',currentPage=2,size=3)
print(res)