Python3操作MySQL8.XX创建表|CRUD基本操作
Python3操作SQLite3创建表主键自增长|CRUD基本操作
一: Python3操作Mysql数据库建表
import pymysql ''' Python3操作Mysql创建表: ''' # 打开数据库连接 db = pymysql.connect(host="localhost", user="您的用户名", password="您的密码", database="数据库名称", charset='utf8' ) conn = pymysql.connect(user="root", password="123456", host="localhost", database="tope-pay-user", port=3306, charset='utf8', autocommit=False) cursor = conn.cursor() cursor.execute("DROP TABLE IF EXISTS sys_books;") try: sql = """ CREATE TABLE sys_books ( id BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID', NAME VARCHAR (50) NOT NULL COMMENT '名称', category VARCHAR (50) NOT NULL COMMENT '类别', price DECIMAL (10, 2) DEFAULT '0.00' COMMENT '价格', publish_time datetime DEFAULT NULL COMMENT '出版日期', create_time datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', update_time datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间', deleted INT (1) DEFAULT '0' COMMENT '逻辑删除:0--正常 1--删除', PRIMARY KEY (id) ) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = COMPACT COMMENT = '数据信息表'; """ cursor.execute(sql) print("表[sys_books]", "创建成功") except pymysql.Error as err: print("建表异常: ", err) finally: cursor.close() conn.close()
二: Python3操作Mysql8.xx查看数据库版本信息
import pymysql ''' Python3操作mysql数据 ''' # 打开数据库连接 db = pymysql.connect(host="localhost", user="您的用户名", password="您的密码", database="数据库名称", charset='utf8' ) conn = pymysql.connect(user="root", password="123456", host="localhost", database="tope-pay-user", port=3306, charset='utf8', autocommit=False) # 创建游标对象 cursor = conn.cursor() try: # 执行查询 cursor.execute("select version()") # 获取单条数据 resultData = cursor.fetchone() print("resultData: ", resultData) except pymysql.Error as err: print("系统异常: ", err) finally: print("释放资源!") cursor.close() conn.close()
三: 执行结果
D:\program_file_worker\anaconda\python.exe D:\program_file_worker\python_source_work\SSO\grammar\ClassGrammarMysqlExecutorQuery.py
resultData: ('8.0.15',)
释放资源!
四: 批量插入数据
import pymysql import datetime ''' Python3操作Mysql批量插入数据: ''' # 打开数据库连接 db = pymysql.connect(host="localhost", user="您的用户名", password="您的密码", database="数据库名称", charset='utf8' ) conn = pymysql.connect(user="root", password="123456", host="localhost", database="tope-pay-user", port=3306, charset='utf8', autocommit=False) # 获取游标 cursor = conn.cursor() try: day = datetime.datetime.now() # 数据列表 paramData = [('零基础学Python3', 'Python', '79.00', day, day, day, 0), ('零基础学Java', 'Java', '90.00', day, day, day, 0), ('零基础学C++', 'C++', '70.00', day, day, day, 0), ('零基础学Python数据可视化', 'Python', '49.00', day, day, day, 0), ('零基础学C语言', 'C', '39.00', day, day, day, 0), ('零基础学android', 'Android', '89.00', day, day, day, 0)] sql = ("insert into sys_books(name,category,price,publish_time,create_time,update_time,deleted) value" "(%s,%s,%s,%s,%s,%s,%s)") cursor.executemany(sql, paramData) # 手动提交数据 conn.commit() print("批量插入数据成功") except pymysql.Error as err: print("批量插入异常: ", err) conn.rollback() finally: print("释放系统资源") cursor.close() conn.close()
五: 查询数据
import pymysql ''' Python3操作mysql数据 ''' # 打开数据库连接 db = pymysql.connect(host="localhost", user="您的用户名", password="您的密码", database="数据库名称", charset='utf8' ) conn = pymysql.connect(user="root", password="123456", host="localhost", database="tope-pay-user", port=3306, charset='utf8', autocommit=False) # 创建游标对象 cursor = conn.cursor() try: # 执行查询 cursor.execute("select version()") # 获取单条数据 resultData = cursor.fetchone() print("resultData: ", resultData) cursor.execute("select * from sys_books") resultList = cursor.fetchall() for item in resultList: print("数据记录: ", item) except pymysql.Error as err: print("系统异常: ", err) finally: print("释放资源!") cursor.close() conn.close()
六: 执行结果
D:\program_file_worker\anaconda\python.exe D:\program_file_worker\python_source_work\SSO\grammar\ClassGrammarMysqlExecutorQuery.py
resultData: ('8.0.15',)
数据记录: (1, '零基础学Python3', 'Python', Decimal('79.00'), datetime.datetime(2023, 9, 26, 16, 58, 34), datetime.datetime(2023, 9, 26, 16, 58, 34), datetime.datetime(2023, 9, 26, 16, 58, 34), 0)
数据记录: (2, '零基础学Java', 'Java', Decimal('90.00'), datetime.datetime(2023, 9, 26, 16, 58, 34), datetime.datetime(2023, 9, 26, 16, 58, 34), datetime.datetime(2023, 9, 26, 16, 58, 34), 0)
数据记录: (3, '零基础学C++', 'C++', Decimal('70.00'), datetime.datetime(2023, 9, 26, 16, 58, 34), datetime.datetime(2023, 9, 26, 16, 58, 34), datetime.datetime(2023, 9, 26, 16, 58, 34), 0)
数据记录: (4, '零基础学Python数据可视化', 'Python', Decimal('49.00'), datetime.datetime(2023, 9, 26, 16, 58, 34), datetime.datetime(2023, 9, 26, 16, 58, 34), datetime.datetime(2023, 9, 26, 16, 58, 34), 0)
数据记录: (5, '零基础学C语言', 'C', Decimal('39.00'), datetime.datetime(2023, 9, 26, 16, 58, 34), datetime.datetime(2023, 9, 26, 16, 58, 34), datetime.datetime(2023, 9, 26, 16, 58, 34), 0)
数据记录: (6, '零基础学android', 'Android', Decimal('89.00'), datetime.datetime(2023, 9, 26, 16, 58, 34), datetime.datetime(2023, 9, 26, 16, 58, 34), datetime.datetime(2023, 9, 26, 16, 58, 34), 0)
数据记录: (7, '零基础学Python3', 'Python', Decimal('79.00'), datetime.datetime(2023, 9, 26, 17, 9, 41), datetime.datetime(2023, 9, 26, 17, 9, 41), datetime.datetime(2023, 9, 26, 17, 9, 41), 0)
数据记录: (8, '零基础学Java', 'Java', Decimal('90.00'), datetime.datetime(2023, 9, 26, 17, 9, 41), datetime.datetime(2023, 9, 26, 17, 9, 41), datetime.datetime(2023, 9, 26, 17, 9, 41), 0)
数据记录: (9, '零基础学C++', 'C++', Decimal('70.00'), datetime.datetime(2023, 9, 26, 17, 9, 41), datetime.datetime(2023, 9, 26, 17, 9, 41), datetime.datetime(2023, 9, 26, 17, 9, 41), 0)
数据记录: (10, '零基础学Python数据可视化', 'Python', Decimal('49.00'), datetime.datetime(2023, 9, 26, 17, 9, 41), datetime.datetime(2023, 9, 26, 17, 9, 41), datetime.datetime(2023, 9, 26, 17, 9, 41), 0)
数据记录: (11, '零基础学C语言', 'C', Decimal('39.00'), datetime.datetime(2023, 9, 26, 17, 9, 41), datetime.datetime(2023, 9, 26, 17, 9, 41), datetime.datetime(2023, 9, 26, 17, 9, 41), 0)
数据记录: (12, '零基础学android', 'Android', Decimal('89.00'), datetime.datetime(2023, 9, 26, 17, 9, 41), datetime.datetime(2023, 9, 26, 17, 9, 41), datetime.datetime(2023, 9, 26, 17, 9, 41), 0)
释放资源!Process finished with exit code 0