python *** 作mysql数据库

python *** 作mysql数据库,第1张

python *** 作mysql数据 一、安装pymysql
pip install pymysql
二、 *** 作思路

(1)导入包

(2)创建连接

(3)创建空白板

(4)输入sql语句,执行sql语句

(5)打印结果,关闭空白板和数据库连接

import pymysql,pprint

# 创建数据库的连接
db = pymysql.connect(
                     host="localhost",
                     port=端口号,
                     user="root",
                     password="密码",
                     db="库名",
                     cursorclass=pymysql.cursors.DictCursor)
cur = db.cursor()  # 创建空白版
sql = "SELECt * FROM student WHERe id = 1"
cur.execute(sql)  # 执行sql语句
res = cur.fetchall() # 查询所有结果
pprint.pprint(res)
cur.close()
db.close()
三、推荐方法
# 创建数据库连接
# 只需要用到一次是就用with open方法,自动关闭
with pymysql.connect(host="localhost",
                     port=3306,
                     user="root",
                     password="---",
                     db="----",
                     cursorclass=pymysql.cursors.DictCursor) as db:
    # 游标
    cur = db.cursor()
    sql = "SELECt * FROM t_patrol_article WHERe id = 1"
    cur.execute(sql)
    res = cur.fetchall()
    pprint.pprint(res)
    cur.close()
四、封装连接查询数据方法 注意点:

(1)创建连接数据库时,port参数必须时int整型,否则会报错

(2)创建、删除、修改一定要提交

(3) *** 作完数据库一定要记得关闭

(4)pymysql有个版本不支持with语法(踩过坑,升级到最新版本)

(5)字典:cursorclass=pymysql.cursors.DictCursor,否则默认返回数据类型为元组

额外科普的知识点:

“DML主要用来对数据库的数据进行 *** 作,DDL主要是用在定义或改变表的结构。

1.DML是SELECt、UPDATE、INSERT、DELETE,就像它的名字一样,这4条命令是用来对数据库。

2.DDL比DML要多,主要的命令有CREATE、ALTER、DROP等,DDL主要是用在定义或改变表(TABLE)的结构,数据类型。

import pymysql, pprint


class HandleDb:
    # 创建连接只创建一次,游标只创建一次,用完就要关闭
    def __init__(self):
        self.db = pymysql.connect(host="localhost",
                                  port=3306,
                                  user="root",
                                  password="-----",
                                  db="-----",
                                  autocommit=True,   # 设置自动提交
                                  cursorclass=pymysql.cursors.DictCursor)
        self.cur = self.db.cursor()

    # 返回所有的数据
    def get_all_data(self, sql):
        try:
            self.cur.execute(sql)
            return self.cur.fetchall()
        except Exception as e:
            print(e,traceback.print_exc(file=open("log.txt","a"))) #异常处理写入到文件中
        finally:
            self.db.close()

    # 返回指定的数据
    def get_many_data(self, sql, size):
        try:
            self.cur.execute(sql)
            return self.cur.fetchmany(size)
        except Exception as e:
            print(e)
        finally:
            self.db.close()

    # 返回第一条数据
    def get_one_data(self, sql):
        try:
            self.cur.execute(sql)
            return self.cur.fetchone()
        except Exception as e:
            print(e)
        finally:
            self.cur.close()


if __name__ == '__main__':
    cl = HandleDb()
    res = cl.get_all_data(sql="select * from member where id < 3;")
    res1 = cl.get_many_data(sql="select * from member where id < 3;", size=2)
    res2 = cl.get_one_data(sql="select * from member where id < 3;")
    pprint.pprint(res)
    pprint.pprint(res1)
    pprint.pprint(res2)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5679990.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存