python 与 mysql 连接(增删改查)

1、

查询数据库的某一张表

#!/usr/bin/python
# -*- coding: UTF-8 -*-
#  查询数据库的某一张表
import MySQLdb

db = ""


def mysql_conn():
    global db
    try:
        # 打开数据库连接
        db = MySQLdb.connect("localhost", "root", "dong2025", "mysql8", charset='utf8')
        # 使用cursor()方法获取操作游标
        cursor = db.cursor()
        # SQL 查询数据库中的某一张表
        sql = "SELECT * FROM BOOK WHERE BOOK_ID > 3"
        # 执行sql语句
        cursor.execute(sql)
        # fetchall():接收全部的返回结果行.
        # alldata = cursor.fetchall()
        # return alldata
        # fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
        onedate = cursor.fetchone()
        return onedate


    except:
        # Rollback in case there is any error
        db.rollback()
    finally:
        db.close()


my_con = mysql_conn()
print(my_con)

2、

SQL 插入语句

#!/usr/bin/python
# -*- coding: UTF-8 -*-
#  SQL 插入语句
import MySQLdb

db = ""


def mysql_conn():
    global db
    try:
        # 打开数据库连接
        db = MySQLdb.connect("localhost", "root", "dong2025", "mysql8", charset='utf8')
        # 使用cursor()方法获取操作游标
        cursor = db.cursor()
        # SQL 插入语句
        sql = """
                 INSERT INTO BOOK(book_id, SORT, book_name, writer, OUTPUT, price)
                 VALUES (11, 'T8988', '数据科学家', '作者_李克强', '深圳出版社', 666.00)
              """
        # 执行sql语句
        cursor.execute(sql)
        # 提交到数据库执行
        db.commit()
    except:
        # Rollback in case there is any error
        db.rollback()
    finally:
        db.close()


mysql_conn()


3、

 SQL update更新语句

#!/usr/bin/python
# -*- coding: UTF-8 -*-
#  SQL update更新语句
import MySQLdb

db = ""


def mysql_conn():
    global db
    try:
        # 打开数据库连接
        db = MySQLdb.connect("localhost", "root", "dong2025", "mysql8", charset='utf8')
        # 使用cursor()方法获取操作游标
        cursor = db.cursor()
        # SQL update更新语句
        sql = "UPDATE BOOK SET book_name = 'python算法导论' WHERE book_id= 9"
        print(sql)
        # 执行sql语句
        cursor.execute(sql)
        # 提交到数据库执行
        db.commit()
    except:
        # Rollback in case there is any error
        db.rollback()
    finally:
        db.close()


mysql_conn()


4、

SQL 删除语句

#!/usr/bin/python
# -*- coding: UTF-8 -*-
#  SQL 删除语句
import MySQLdb

db = ""


def mysql_conn():
    global db
    try:
        # 打开数据库连接
        db = MySQLdb.connect("localhost", "root", "dong2025", "mysql8", charset='utf8')
        # 使用cursor()方法获取操作游标
        cursor = db.cursor()
        # SQL delete语句
        str_num = 8
        sql = F"DELETE FROM BOOK  WHERE book_id= {str_num}"
        print(sql)
        # 执行sql语句
        cursor.execute(sql)
        # 提交到数据库执行
        db.commit()
    except:
        # Rollback in case there is any error
        db.rollback()
    finally:
        db.close()


mysql_conn()


5、

# cursorclass=pymysql.cursors.DictCursor
# 与
# cursor=pymysql.cursors.DictCursor
# 的用法及区别

#!/usr/bin/python
# -*- coding: UTF-8 -*-
#  查询数据库的某一张表

# cursorclass=pymysql.cursors.DictCursor
# 与
# cursor=pymysql.cursors.DictCursor
# 的用法及区别
import pymysql

db = ""


def mysql_conn():
    global db
    try:
        # 打开数据库连接
        # db = pymysql.connect("localhost", "root", "dong2025", "mysql8", charset='utf8')
        db = pymysql.connect(
            host="localhost",
            port=3306,
            user="root",
            password="dong2025",
            database="mysql8",
            charset="utf8",
            # cursorclass=pymysql.cursors.DictCursor # 可以返回字典数据(包含字段名字),

        )
        # 使用cursor()方法获取操作游标
        # cursor = db.cursor()
        cursor = db.cursor(cursor=pymysql.cursors.DictCursor)  # 可以返回字典数据(包含字段名字)

        # SQL 查询数据库中的某一张表
        sql = "SELECT * FROM BOOK WHERE BOOK_ID > 3"
        # 执行sql语句
        cursor.execute(sql)
        # fetchall():接收全部的返回结果行.
        # alldata = cursor.fetchall()
        # return alldata
        # fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
        onedate = cursor.fetchone()
        return onedate


    except:
        # Rollback in case there is any error
        db.rollback()
    finally:
        db.close()


my_con = mysql_conn()
print(my_con)

5.1,pymysql,pandas,mysql一起来操作数据库

import pandas as pd
import pymysql

db = ""


def mysql_conn():
    global db
    try:
        # 打开数据库连接
        db = pymysql.connect(
            host="localhost",
            port=3306,
            user="root",
            password="dong2025",
            database="mysql8",
            charset="utf8",

        )
        sql = "SELECT * FROM BOOK"
        df_sql = pd.read_sql(sql, db)

        # return df_sql  # 展示所有结果
        # return df_sql.head()  # 默认展示前5行
        # return df_sql.head(2)  # 展示前2行
        # return df_sql.shape   # 统计行和列数。
        return df_sql.info()  # info()方法可以查看数据表中的数据类型

    except:
        # Rollback in case there is any error
        db.rollback()
    finally:
        db.close()


my_con = mysql_conn()
print(my_con)


6、更新 pymysql_pymysql操作mysql_weixin_39753616的博客-CSDN博客https://blog.csdn.net/weixin_39753616/article/details/112628169

7、Python连接MySQL数据库方法介绍(超详细!手把手项目案例操作) - 哔哩哔哩https://www.bilibili.com/read/cv3418619

8、Python3下不同MySQL驱动的性能对比_Python小屋-CSDN博客

9、

mysql客户端pymysql在python下性能比较_得救之道就在其中-CSDN博客

10、

python3 操作mysql数据库(mysql.connector 和 pymysql )_whatday的专栏-CSDN博客_pymysql和mysqlconnectorhttps://blog.csdn.net/whatday/article/details/102796789

11、

python的mysql-connector和pymysql - chengxuyonghu - 博客园https://www.cnblogs.com/chengxuyonghu/p/13533629.html

12、

Python:连接mysql数据库的三种方式,mysql.connector, pymysql, MYSQLdb_JacksonKim的博客-CSDN博客https://blog.csdn.net/qq_40765537/article/details/105876121

13、

14、

15、

16、

17、

18、

19、

猜你喜欢

转载自blog.csdn.net/weixin_54217632/article/details/121078913