python 使用 pymysql 數(shù)據(jù)庫操作基本使用

python 使用 pymysql 操作mysql 初體驗.

import pymysql


def create_table(bg_data_name, table_name, sql, drop):
    """ 創(chuàng)建數(shù)據(jù)庫表

    :param bg_data_name: str: 連接數(shù)據(jù)庫的名稱
    :param table_name:   str: 表名
    :param sql:          str: 創(chuàng)建數(shù)據(jù)庫語句
    :param drop:         bool: 如果表存在,是否是刪除舊的表,重新創(chuàng)建
    :return:
    """
    print("創(chuàng)建表的語句 = ", sql)
    # 打開數(shù)據(jù)庫連接
    db = pymysql.connect("127.0.0.1", "root", "chunyan", bg_data_name)

    # 使用 cursor() 方法創(chuàng)建一個游標對象 cursor
    cursor = db.cursor()

    # 判斷數(shù)據(jù)庫中是否存在表
    exist = cursor.execute("show tables like '%s'" % table_name)
    print("如果表存在則刪除 exist = ", exist)

    if exist:
        print("表已存在")
        if drop:
            print("刪除表,重新創(chuàng)建")
            # 使用 execute() 方法執(zhí)行 SQL,如果表存在則刪除
            cursor.execute("DROP TABLE IF EXISTS %s" % table_name)
            print("創(chuàng)建表")
            cursor.execute(sql)
            db.commit()
    else:
        print("創(chuàng)建表")
        cursor.execute(sql)

    print("關(guān)閉數(shù)據(jù)庫連接")
    cursor.close()
    # 關(guān)閉數(shù)據(jù)庫連接
    db.close()


def del_table(bg_data_name, table_name):
    """ 刪除數(shù)據(jù)庫中的表

    :param bg_data_name:    str: 連接數(shù)據(jù)庫的名稱
    :param table_name:      str: 表名
    :return:
    """
    # 打開數(shù)據(jù)庫連接
    db = pymysql.connect("127.0.0.1", "root", "chunyan", bg_data_name)

    # 使用 cursor() 方法創(chuàng)建一個游標對象 cursor
    cursor = db.cursor()

    # 判斷數(shù)據(jù)庫中是否存在表
    exist = cursor.execute("show tables like '%s'" % table_name)
    print("判斷數(shù)據(jù)庫中是否存在表 exist = ", exist)

    if exist:
        print("表存在 刪除表")
        # 使用 execute() 方法執(zhí)行 SQL,如果表存在則刪除
        cursor.execute("DROP TABLE IF EXISTS %s" % table_name)
    else:
        print("數(shù)據(jù)庫中沒有該表")

    print("關(guān)閉數(shù)據(jù)庫連接")
    cursor.close()
    # 關(guān)閉數(shù)據(jù)庫連接
    db.close()


def add_table_row(bg_data_name, table_name, field_name, row):
    add_table_rows(bg_data_name, table_name, field_name, [row])


def add_table_rows(bg_data_name, table_name, field_name, rows):
    """ 向數(shù)據(jù)庫添加數(shù)據(jù)

    :param bg_data_name: str: 連接數(shù)據(jù)庫的名稱
    :param table_name:   str: 表名
    :param field_name:   str: 字段名 多個逗號隔開
    :param rows:         list(tuple): 添加的數(shù)據(jù)
    :return:
    """
    # 打開數(shù)據(jù)庫連接
    db = pymysql.connect("127.0.0.1", "root", "chunyan", bg_data_name)

    # 使用 cursor() 方法創(chuàng)建一個游標對象 cursor
    cursor = db.cursor()

    # 判斷數(shù)據(jù)庫中是否存在表
    exist = cursor.execute("show tables like '%s'" % table_name)
    print("數(shù)據(jù)庫中是否存在表 exist = ", exist)

    if exist:
        print("表存在")
        string = str(rows)
        string1 = string.replace("[", "")
        value = string1.replace("]", "")
        sql = """INSERT INTO %s(%s) VALUES %s""" % (table_name, field_name, value)
        print("插入表的操作語句 = ", sql)
        cursor.execute(sql)
        db.commit()
    else:
        print("插入失敗 不存在表")

    print("關(guān)閉數(shù)據(jù)庫連接")
    cursor.close()
    # 關(guān)閉數(shù)據(jù)庫連接
    db.close()


def sql_table(bg_data_name, table_name, sql):
    """ 操作數(shù)據(jù)庫

    :param bg_data_name:    str: 連接數(shù)據(jù)庫的名稱
    :param table_name:      str: 表名
    :param sql:             str: 數(shù)據(jù)庫操作語句
    :return:
    """
    # 打開數(shù)據(jù)庫連接
    db = pymysql.connect("127.0.0.1", "root", "chunyan", bg_data_name)

    # 使用 cursor() 方法創(chuàng)建一個游標對象 cursor
    cursor = db.cursor()

    # 判斷數(shù)據(jù)庫中是否存在表
    exist = cursor.execute("show tables like '%s'" % table_name)
    print("數(shù)據(jù)庫中是否存在表 exist = ", exist)
    cursor.execute(sql)
    db.commit()

    print("關(guān)閉數(shù)據(jù)庫連接")
    cursor.close()
    # 關(guān)閉數(shù)據(jù)庫連接
    db.close()


def find_table(bg_data_name, table_name, sql):
    """ 操作數(shù)據(jù)庫

    :param bg_data_name:    str: 連接數(shù)據(jù)庫的名稱
    :param table_name:      str: 表名
    :param sql:             str: 數(shù)據(jù)庫操作語句
    :return: tuple
    """
    # 打開數(shù)據(jù)庫連接
    db = pymysql.connect("127.0.0.1", "root", "chunyan", bg_data_name)

    # 使用 cursor() 方法創(chuàng)建一個游標對象 cursor
    cursor = db.cursor()

    # 判斷數(shù)據(jù)庫中是否存在表
    exist = cursor.execute("show tables like '%s'" % table_name)
    print("數(shù)據(jù)庫中是否存在表 exist = ", exist)
    cursor.execute(sql)
    # 獲取所有記錄列表
    results = cursor.fetchall()

    print("關(guān)閉數(shù)據(jù)庫連接")
    cursor.close()
    # 關(guān)閉數(shù)據(jù)庫連接
    db.close()
    return results


def del_table_row(bg_data_name, table_name, sql):
    # 打開數(shù)據(jù)庫連接
    db = pymysql.connect("127.0.0.1", "root", "chunyan", bg_data_name)

    # 使用 cursor() 方法創(chuàng)建一個游標對象 cursor
    cursor = db.cursor()

    # 判斷數(shù)據(jù)庫中是否存在表
    exist = cursor.execute("show tables like '%s'" % table_name)
    print("數(shù)據(jù)庫中是否存在表 exist = ", exist)

    if exist:
        print("執(zhí)行刪除語句")
        cursor.execute(sql)
        # 提交修改
        db.commit()
    else:
        print("刪除失敗 不存在表")

    print("關(guān)閉數(shù)據(jù)庫連接")
    cursor.close()
    # 關(guān)閉數(shù)據(jù)庫連接
    db.close()


def temp_call():
    table_name = "temp_table6"
    bg_name = "crawler"
    # 創(chuàng)建一個表
    # sql_str = """CREATE TABLE %s (
    #              NAME  CHAR(20) NOT NULL,
    #              AGE INT,
    #              SEX CHAR(1)
    #              )""" % table_name
    # 創(chuàng)建一個主鍵自增長
    # sql_str = """CREATE TABLE %s (
    #              id INT AUTO_INCREMENT,
    #              name CHAR(20) NOT NULL,
    #              age INT,
    #              sex CHAR(1),
    #              PRIMARY KEY (id)
    #              )""" % table_name

    # 創(chuàng)建表
    # create_table(bg_name, table_name, sql_str, True)
    # 刪除表
    # del_table(bg_name, name)
    # 向表中添加數(shù)據(jù)
    # add_table_rows(bg_name, table_name, "name, age, sex", [("主角", 9, "男"), ("配角", 2, "女")])
    # sql = ""
    # insert_table_row(bg_name, table_name, sql)
    # 刪除表中的數(shù)據(jù)
    # sql = "DELETE FROM %s WHERE AGE = %d" % (table_name, 10)
    # print(sql)
    # del_table_row(bg_name, table_name, sql)
    # 更新數(shù)據(jù) 向 ID 為2的那一行數(shù)據(jù),更新 age 為3.
    # sql = "UPDATE %s SET AGE = 3 WHERE ID = %d" % (table_name, 2)
    # sql_table(bg_name, table_name, sql)
    # 查詢表中所有數(shù)據(jù)
    sql = "SELECT * FROM %s" % table_name
    obj = find_table(bg_name, table_name, sql)
    print(obj)

temp_call()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容