from pymysql importcursors
from tools.config import
config
import
random,string
defcreatedatabase():
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")
defcreatetbale():
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():
(tuple(values))
sql = '
insert into {} {}
'.format(tablename,key).replace("
'","")+'
values {}
'.format(tuple(values))
(sql)
try:
cursor.execute(sql)
db.commit()
print("
成功新增資料")
print("
插入資料的id:
",cursor.lastrowid)
except
exception as e:
(e)
#發生錯誤時候回滾
db.rollback()
defdeleterecord(db,cursor,table,key,value):
sql = '
delete from {} where {} = {}
'.format(table,key,value)
cursor.exectue(sql)
db.commit()
print("
成功刪除%d條資料
" %cursor.rowcount)
defupdaterecord(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)
#查詢這張表的所有資料
deffindrecord(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
#有條件的查詢語句
deffindrecord_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
#有條件的模糊查詢語句
deffindrecord_like(cursor,table,key,value):
sql = '
select * from {} where {} like "%%{}%%"
'.format(table,key,value)
(sql)
num =cursor.execute(sql)
result =cursor.fetchall()
print("
查詢到%d條資料
" %num)
if num >0:
return
result
else
:
pass
#在一段時間範圍內查詢資料
deffindrecord_between(cursor,table,key,value,value2):
sql = '
select * from {} where {} between "{}" and "{}"
'.format(table,key,value,value2)
(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 增刪改查
lists aa aa 增lists.insert 0,aa 在索引為0的地方插入 aa lists.insert 5,aa 在索引為5的地方插入 aa 如果索引沒到5則插到尾部 刪lists.remove aa 刪除 第一遇到的 aa 從左向右 del lists 5 刪除指定索引的值 如果索引超...
Python的增刪改查
python coding utf 8 對列表的增刪改查 namelist 張三 里斯 王二 張三 查詢 print namelist 遍歷列表 for i in namelist print i 增加 names2 yasuo timo extend 將列表新增至另乙個列表的結尾 namelist...
mysql增刪改查效果 mysql增刪改查
檢視所有資料庫 mysql show databases 建立乙個庫ghd並指定字符集為utp8 mysql create database ghd charset utf8 檢視mysql支援的字符集 mysql show char set 建立乙個表,並設定id為主鍵 create table ...