Python 增删改查

Posted 天空是我喜欢的颜色

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 增删改查相关的知识,希望对你有一定的参考价值。

from pymysql import cursors
from tools.config import config
import random,string

def createDatabase():
    db = config.db
    cur = db.cursor(cursor=cursors.DictCursor)
    cur.execute("drop database if EXISTS  py_test")
    cur.execute("create database py_test")
    cur.close()
    config.connect.close()
    print("已创建数据库py_test")

def createTbale():
    db = config.db
    cur = db.cursor(cursor=cursors.DictCursor)
    sql = ‘‘‘CREATE TABLE IF NOT EXISTS ty_test_info(
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(45) NOT NULL,
    age VARCHAR(45) NULL
    )
    ‘‘‘
    cur.execute(sql)
    print("已创建表fy_test")


def insertData(db,cursor,tablename,*key,**kwargs):
    # *key返回的是元组(),**返回的是字典
    values = []
    for value in kwargs.values():
        values.append(value)
    print(tuple(values))
    sql = insert into {} {}.format(tablename,key).replace("","")+ VALUES {}.format(tuple(values))
    print(sql)
    try:
        cursor.execute(sql)
        db.commit()
        print("成功添加数据")
        print("插入数据的ID:",cursor.lastrowid)
    except Exception as e:
        print(e)
        # 发生错误时候回滚
        db.rollback()



def deleteRecord(db,cursor,table,key,value):
    sql = delete from {} where {} = {}.format(table,key,value)
    cursor.exectue(sql)
    db.commit()
    print("成功删除%d条数据" %cursor.rowcount)


def updateRecord(db,cursor,table,key1,value1,key2,value2):
    sql = update {} set{}="{}" where {} = "{}".format(table,key2,value2,key1,value1)
    cursor.exectue(sql)
    db.commit()
    print("成功修改%d条数据" %cursor.rowcount)

# 查询这张表的所有数据
def findRecord(cursor,table):
    sql = select * from {}.format(table)
    num = cursor.exectue(sql)
    result = cursor.fetchall()
    print("查找到%d条数据" %num)
    for row in result:
        print(row[0],row[1],row[2])

    return result

# 有条件的查询语句
def findRecord_where(cursor,table,key,value):
    sql = select * from {} where {} = {}.format(table,key,value)
    num = cursor.execute(sql)
    result = cursor.fetchall()
    print("查找到%d条数据" %num)
    if num > 0:
        return result
    else:
        pass


# 有条件的模糊查询语句
def findRecord_like(cursor,table,key,value):
    sql = select * from {} where {} like  "%%{}%%".format(table,key,value)
    print(sql)
    num = cursor.execute(sql)
    result = cursor.fetchall()
    print("查找到%d条数据" %num)
    if num > 0:
        return result
    else:
        pass

# 在一段时间范围内查找数据
def findRecord_between(cursor,table,key,value,value2):
    sql = select * from {} where {} BETWEEN "{}" AND "{}".format(table,key,value,value2)
    print(sql)
    num = cursor.execute(sql)
    result = cursor.fetchall()
    print("查找到%d条数据" %num)
    if num > 0:
        return result
    else:
        pass
# createTbale()
db = config.db
cur = db.cursor(cursor=cursors.DictCursor)
# for i in range(0,99):
#     name = ‘wangquntest_‘.join(random.sample(string.ascii_letters+string.digits+string.punctuation,3))
#     age =random.randint(20,99)
#     insertData(db,cur,"ty_test_info","name","age",name = name,age = age)
findRecord_like(cur,"t_staff_info","account","1870465468")
findRecord_between(cur,"t_staff_info","create_time","2019-01-01","2019-12-01")
cur.close()
db.close()

 

以上是关于Python 增删改查的主要内容,如果未能解决你的问题,请参考以下文章

如何用SSM框架写一个增删改查的功能

如何用SSM框架写一个增删改查的功能

Python字典的小析(增删改查)

ListCode增删改查代码生成器介绍

Python数据类型-列表(list)增删改查

python ------- 文件处理之增删改查-------作业