码迷,mamicode.com
首页 > 编程语言 > 详细

Python 增删改查

时间:2020-02-22 15:38:37      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:mysq   info   pre   inf   date   count   cut   cursor   prim   

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 增删改查

标签:mysq   info   pre   inf   date   count   cut   cursor   prim   

原文地址:https://www.cnblogs.com/wangxiaoqun/p/12345506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!