# import pymysql # # db = pymysql.connect( # host="localhost", #本机的数据库,是localhost。如果连接虚拟机的数据库,就用虚拟机的IP地址 # user="root", # port=3306, # password="", #我的数据库密码是为空的 # database="class14" # ) #连接到数据库 # #创建一个用于操作数据库的游标对象 # cursor = db.cursor() # # cursor.execute("show databases;") # cursor.execute("select * from book") # result = cursor.fetchall() # print(result) #打印SQL语句返回的结果 #一般在实际项目来说,我们会把这些封装成一个专门操作数据库的类进行操作 ''' 创建一些数据库常用的类''' from test.dao import DAO #创建连接 DAO.create_conn('localhost',3306,'root','','class14') #查询SQL操作,dql操作 # sqlstr = "select * from book " # result = DAO.exec_dql(sqlstr) # print(result) #插入dml语句 sqlstr = "insert into dept values('50','运营','cd')" if DAO.exec_dml(sqlstr): query_str = "select * from dept where deptno=50" result = DAO.exec_dql(query_str) print(result)
Python操作数据库
最新推荐文章于 2024-05-11 08:31:42 发布