import pyMysqLimport array as arr'''开启数据库会话'''def CONNECT_sql(host, port, user_name, pwd): conn = pyMysqL.connect( host=host, # 指示host表明是本地MysqL还是远程 port=port, user=user_name, # 用户名 password=pwd, # 密码 charset="utf8mb4", # 指定字符集,可以解决中文乱码 cursorclass=pyMysqL.cursors.DictCursor # 固定写法,类似于jdbc里边的加载驱动 ) print(conn) return conn'''连接数据库'''#db 数据库名称def CONNECT_sqlDB(host, port, user_name, pwd,db): conn = pyMysqL.connect( host=host, # 指示host表明是本地MysqL还是远程 port=port, user=user_name, # 用户名 password=pwd, # 密码 db=db, # 所连接的数据库 charset="utf8mb4", # 指定字符集,可以解决中文乱码 cursorclass=pyMysqL.cursors.DictCursor # 固定写法,类似于jdbc里边的加载驱动 ) print(conn) return conn'''插入数据'''def insert(conn, table_name, sql_command): sql = sql_command conn = conn cursor = conn.cursor() sql = 'INSERT INTO' + ' ' + table_name + ' ' + 'VALUES(' + sql + ');' # 异常处理 try: print(sql) # 执行SQL语句 result = cursor.execute(sql) print(result) except: print('Unable to insert! Please check the insert statement') conn.commit() cursor.close() conn.close()'''查询'''# table_name表名# sql_command查询条件def SELECT(conn, table_name, sql_command): sql = sql_command conn = conn cursor = conn.cursor() sql = 'SELECT * FROM ' + ' ' + table_name + ' ' + 'WHERE' + ' ' + sql # 异常处理 try: # 执行SQL语句 result = cursor.execute(sql) # 获取所有的记录列表 results = cursor.fetchall() print(result) # 遍历列表 for row in results: # 打印列表元素 print(row) except: print('Unable to fetch data!') print(sql) conn.commit() cursor.close() conn.close()'''更新一条数据'''# conn 数据库连接# table_name 表名# column_name 列名# line_name 行名称# line_ID 行ID# ID 主键值def update(conn, table_name, column_name, column, line_name, line_ID): conn = conn cursor = conn.cursor() sql = 'UPDATE ' + table_name + ' SET ' + column_name + '=%s WHERE ' + column + ' = %s;' line = (line_name, line_ID) # 异常处理 try: # 执行SQL语句 result = cursor.execute(sql, line) conn.commit() print(sql) print(result) except: conn.rollback() # 错误时回滚 print('Unable to update!') cursor.close() conn.close()'''删除数据行'''def DELETE(conn, table_name, sql_command): sql = sql_command conn = conn cursor = conn.cursor() sql = 'DELETE FROM ' + ' ' + table_name + ' ' + 'WHERE' + ' ' + sql # 异常处理 try: # 执行SQL语句 result = cursor.execute(sql) conn.commit() print(result) print(sql) except: print('Unable to delete!') print(sql) cursor.close() conn.close()'''建数据库'''def CREATE_DATABASE(conn, DATABASE_name): conn = conn cursor = conn.cursor() sql = 'CREATE DATABASE' + ' ' + 'IF NOT EXISTS ' + DATABASE_name + ' '+'DEFAulT CHARSET utf8 ColLATE utf8_general_ci;' # 异常处理 try: # 执行SQL语句 result = cursor.execute(sql) print(sql) print(result) except: print(sql) print('Unable to create database') conn.commit() cursor.close() conn.close()'''建表'''# table_name 表名# table_command 表命令def CREATE_table(conn, table_name, table_command): conn = conn cursor = conn.cursor() DROP_sql = 'DROP table ' + table_name sql = 'CREATE table ' + table_name + '(' + table_command + ')' # 异常处理 try: # 执行SQL语句 result = cursor.execute(sql) conn.commit() print(sql) print(result) except: cursor.execute(DROP_sql) # 建表失败删除同名列表 conn.commit() print(DROP_sql) print("删除同名列表") cursor.close() conn.close()'''删除表'''# table_name 表名def DROP_table(conn, table_name): conn = conn cursor = conn.cursor() sql ='DROP table ' + table_name # 异常处理 try: # 执行SQL语句 result = cursor.execute(sql) conn.commit() print(sql) print("删除成功") except: print("删除列表失败") cursor.close() conn.close()'''自由执行语句'''def MysqL_command(conn, sql_command): sql = sql_command conn = conn cursor = conn.cursor() # 异常处理 try: # 执行SQL语句 result = cursor.execute(sql) conn.commit() print(sql) print(result) except: conn.rollback() # 错误时回滚 print('Unable to execute sql command! Please check the insert statement!') cursor.close() conn.close()# 示例if __name__ == '__main__': conn = CONNECT_sql('localhost', 3306, 'root', '123456') CREATE_DATABASE(conn, 'testdb') conn1 = CONNECT_sqlDB('localhost', 3306, 'root', '123456','1111') CREATE_table(conn1, "test", "ID int not null,name char(10),age int,address char(20),create_time datetime") # update(conn, "test", "class", "ID", "joint", 9) # SELECT(conn,"test","ID=3") # insert(conn,"test","1") # DELETE(conn,"test","ID=1")
封装较为粗糙,见笑,希望对各位有所帮助。
总结以上是内存溢出为你收集整理的基于Python的MySQL *** 作封装为函数全部内容,希望文章能够帮你解决基于Python的MySQL *** 作封装为函数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)