import pymysql def insertsql(): # 建立数据库连接 connection_sql = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='root', db='5_10', charse='utf8' ) # 获取游标 cursor = connection_sql.cursor(pymysql.cursors.DictCursor) str1 = '1' str2 = '18' # sql = 'select * from staff where id> %s and age>%s' sql1 = 'select * from staff where id= %s' try: # cursor.execute(sql, [str1, str2]) cursor.execute(sql1, str1) a = cursor.fetchall() print(a) connection_sql.commit() except Exception as e: connection_sql.rollback() raise e # 关闭游标 cursor.close() # 关闭连接 connection_sql.close() insertsql()
import pymysql # username : adil # password : helloyyj class DataBaseHandle(object): ''' 定义一个 MySQL 操作类''' def __init__(self, host, username, password, database, port): '''初始化数据库信息并创建数据库连接''' # 下面的赋值其实可以省略,connect 时 直接使用形参即可 self.host = host self.username = username self.password = password self.database = database self.port = port self.db = pymysql.connect( host=self.host, user=self.username, password=self.password, db=self.database, port=self.port, charset="utf8" ) self.cursor = self.db.cursor(pymysql.cursors.DictCursor) def insertDB(self, sql): ''' 插入数据库操作 ''' try: # 执行sql self.cursor.execute(sql) # tt = self.cursor.execute(sql) # 返回 插入数据 条数 可以根据 返回值 判定处理结果 # print(tt) self.db.commit() # query = 'insert into 表名(列名1, 列名2, 列名3, 列名4, 列名5, 列名6) values(%s, %s, %s, %s, %s, %s)' # self.cursor.execute(sql) except: # 发生错误时回滚 self.db.rollback() finally: self.cursor.close() def deleteDB(self, sql): ''' 操作数据库数据删除 ''' try: # 执行sql self.cursor.execute(sql) # tt = self.cursor.execute(sql) # 返回 删除数据 条数 可以根据 返回值 判定处理结果 # print(tt) self.db.commit() except: # 发生错误时回滚 self.db.rollback() finally: self.cursor.close() def updateDb(self, sql): ''' 更新数据库操作 ''' try: # 执行sql self.cursor.execute(sql) # tt = self.cursor.execute(sql) # 返回 更新数据 条数 可以根据 返回值 判定处理结果 # print(tt) self.db.commit() except: # 发生错误时回滚 self.db.rollback() finally: self.cursor.close() def selectDb(self, sql): ''' 数据库查询 ''' try: self.cursor.execute(sql) # 返回 查询数据 条数 可以根据 返回值 判定处理结果 data = self.cursor.fetchall() # 返回所有记录列表 print(data) # 结果遍历 for row in data: name = row["name"] age = row["age"] # 遍历打印结果 print('age = %s, name = %s' % (age, name)) except: print('Error: unable to fecth data') finally: self.cursor.close() def closeDb(self): ''' 数据库连接关闭 ''' self.db.close() if __name__ == '__main__': DbHandle = DataBaseHandle('127.0.0.1', 'root', 'root', 'class202107', 3306,) # DbHandle.insertDB("insert into students (name,age,gender) values ('随便起名',19,'男')") # DbHandle.deleteDB("delete from students where name='随便起名'") DbHandle.selectDb("select * from students where id > 10") DbHandle.closeDb()
# 一个做查询,一个做非查询 import pymysql # username : adil # password : helloyyj class DataBaseHandle(object): ''' 定义一个 MySQL 操作类''' def __init__(self, host, username, password, database, port): '''初始化数据库信息并创建数据库连接''' # 下面的赋值其实可以省略,connect 时 直接使用形参即可 self.host = host self.username = username self.password = password self.database = database self.port = port self.db = pymysql.connect( host=self.host, user=self.username, password=self.password, db=self.database, port=self.port, charset="utf8" ) self.cursor = self.db.cursor(pymysql.cursors.DictCursor) def select_info(self, sql): try: self.cursor.execute(sql) data = self.cursor.fetchall() return data except: print('Error: unable to fecth data') finally: self.cursor.close() def no_select_info(self, sql): try: result = self.cursor.execute(sql) print(result) self.db.commit() return result except Exception as e: self.db.rollback() raise e finally: self.cursor.close() if __name__ == '__main__': DbHandle = DataBaseHandle('127.0.0.1', 'root', 'root', '5_10', 3306, ) DbHandle.no_select_info("insert into stu (name,age) values ('随便起名1',19)") DbHandle.select_info("select * from students where id < 10")