1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
| import pymysql import datetime
def dbconnect(): conn = pymysql.connect( host="127.0.0.1", user="root", password="root123456", database="test", port=3306, charset="utf8" ) print("连接数据库成功") return conn
def jiegou(): sql="""create table if not exists MovieType( tid int primary key, tname varchar(20), tcontent varchar(200), tdate datetime )""" conn = dbconnect() jianbiao=conn.cursor() jianbiao.execute(sql) print("MovieType表创建完成") jianbiao.close() conn.close() print("关闭数据库成功")
def edit(): print("示例: 1,喜剧,这是一个搞笑的电影") data=input("请输入要插入的数据(逗号隔开):").split(",") tid=int(data[0]) tname=data[1] tcontent=data[2] sql=f"insert into MovieType values({tid},'{tname}','{tcontent}',now())" conn = dbconnect() inserdata=conn.cursor() res=inserdata.execute(sql) if res>0: print("添加成功") else: print("添加失败") conn.commit() inserdata.close() conn.close() print("关闭数据库成功")
def cha(): sql="select * from MovieType" conn=dbconnect() show=conn.cursor() show.execute(sql) res=show.fetchall() for i in res: print(i[0],i[1],i[2],datetime.datetime.strftime(i[3],"%Y年%m月%d日")) show.close() conn.close() print("关闭数据库成功")
if __name__=="__main__": cha()
import datetime class MovieType: tid=0 tname="" tcontent="" tdate=datetime.datetime.now()
def __init__(self,tid1,tname1,tcontent1): self.tid=tid1 self.tname=tname1 self.tcontent=tcontent1
import pymysql class DBHelper: def openDB(self): global conn conn=pymysql.connect(host="127.0.0.1", user="root", password="root123456", database="test", port=3306,charset="utf8") global baoma baoma=conn.cursor()
def closeDB(self): baoma.close() conn.close()
def edit(self,sql): self.openDB() result=baoma.execute(sql) conn.commit() self.closeDB() return result def chaOne(self,sql): self.openDB() baoma.execute(sql) result=baoma.fetchone() self.closeDB() return result def chaAll(self,sql): self.openDB() baoma.execute(sql) result=baoma.fetchall() self.closeDB() return result class MovieTypeDAL(DBHelper): def insert(self,mt): sql=f"insert into MovieType values({mt.tid},'{mt.tname}','{mt.tcontent}',now())" return self.edit(sql) def showformtid(self,tid): sql=f"select * from MovieType where tid='{tid}'" return self.chaOne(sql) def update(self,mt): edtname=0 edtcontent=0 if(mt.tname!=""): sql1=f"update MovieType set tname='{mt.tname}' where tid={mt.tid}" edtname=self.edit(sql1)
if(mt.tcontent!=""): sql2=f"update MovieType set tcontent='{mt.tcontent}' where tid={mt.tid}" edtcontent=self.edit(sql2)
return edtname,edtcontent def delet(self,tid): sql=f"delete from MovieType where tid={tid}" return self.edit(sql)
def showall(self): sql="select * from MovieType" return self.chaAll(sql)
from Model import MovieType from DAL import MovieTypeDAL import datetime
mtdal=MovieTypeDAL() menu="""\n-------------⾖瓣⽹管理系统----------------- ===========功能菜单=========== 1 录⼊电影类型信息 2 修改电影类型信息 3 删除电影类型信息 4 显⽰所有电影类型信息 0 退出系统 ============================="""
def option1(): tid=input("请输⼊编号:") res=mtdal.showformtid(tid) if not res: tname=input("请输⼊名称:") tcontent=input("请输⼊内容:") mt=MovieType(tid,tname,tcontent) res= mtdal.insert(mt) if res>0: print("录入成功") else: print("录入失败") else: print("编号已存在!") input("\n按下回车键返回主菜单..")
def option2(): tid=input("请输入修改编号:") print("\n编号 名称 内容 创建⽇期") res=mtdal.showformtid(tid) if not res: print("此电影类型不存在!") input("\n按下回车键返回主菜单..") return print(res[0],res[1],res[2],datetime.datetime.strftime(res[3],"%Y年%m月%d日\n"))
tname=input("请输入修改名称(若不修改则直接回车):") tcontent=input("请输⼊修改内容(若不修改则直接回车):") mt=MovieType(tid,tname,tcontent) res=mtdal.update(mt) print(f"{res[0]}个名称被修改,{res[1]}个内容被修改") input("\n按下回车键返回主菜单..")
def option3(): tid=input("请输入删除编号:") res1=mtdal.showformtid(tid) if not res1: print("此电影类型不存在!") input("\n按下回车键返回主菜单..") return print("\n编号 名称 内容 创建⽇期") print(res1[0],res1[1],res1[2],datetime.datetime.strftime(res1[3],"%Y年%m月%d日\n")) esc=input("确认要删除吗? y/n:") if esc.lower() == 'y' or esc.lower() == 'yes': res2=mtdal.delet(tid) if res2>0 : print("删除成功") else: print("删除失败") input("\n按下回车键返回主菜单..")
def option4(): print("\n编号 名称 内容 创建⽇期") res=mtdal.showall() for i in res: print(i[0],i[1],i[2],datetime.datetime.strftime(i[3],"%Y年%m月%d日")) input("\n按下回车键返回主菜单..")
def option0(): esc=input("请问是否退出? y/n:") if esc.lower() == 'y' or esc.lower() == 'yes': print("已成功退出") exit() else: input("\n按下回车键返回主菜单..")
if __name__=="__main__": while True: print(menu) option=input("请输⼊操作选项:") if option=="1": option1() elif option=="2": option2() elif option=="3": option3() elif option=="4": option4() elif option=="0": option0() else: print("无效操作,请重新输入!") input("按下回车键继续..")
|