此文章非原创,只是自己百度和修改,看别人的都不能运行,需要自己修改,为了像我这样的同学少走一些弯路,给大家一些参考,此代码是本人一步一步实现,是可以直接用的。
有些sqlite3的插入,更新,删除,自己照葫芦画瓢应该会写,不会的可以交流,这是本人第二次写这玩意,有不好的地方见谅,也不知道有没有人看,希望看见的可以学到一点知识吧。importsqlite3
cx = sqlite3.connect('D:/student3.db')
cx.execute( '''CREATE TABLE StudentTable(ID INTEGER PRIMARY KEY AUTOINCREMENT,StuId INTEGER NOT NULL,NAME TEXT NOT NULL,CLASS INT NOT NULL);''')
print("Table created successfully!")
cx.execute('''CREATE TABLE CourseTable (CourseId INT NOT NULL,Name TEXT NOT NULL,Teacher TEXT NOT NULL,Classroom TEXT NOT NULL,StartTime CHAR(11) NOT NULL,EndTime CHAR(11) NOT NULL);''')
cu = cx.cursor()
CourseTable = [('1', 'Qt', 'ming', 602, 'Monday9:00', 'Monday11:00'),
('2', 'Linux', 'han', 605, 'Friday13:20', 'Friday14:20'),
('3', 'sqlite3', 'hah', 608, 'Thursday15:00', 'Thursday17:00'),
]
cu.executemany("insert into CourseTable values(?, ?, ?, ?, ?, ?)", CourseTable)
cx.commit()
print("Table created successfully!")
cx.execute('''CREATE TABLE XuankeTable(ID INTEGER PRIMARY KEY AUTOINCREMENT,StuId INT NOT NULL,CourseId INT NOT NULL);''')
print("Table created successfully")
definsert_stu():#插入学生信息
cu = cx.cursor()
stu_id = input("请输入学生学号:")
cu.execute("select StuId from StudentTable where StuId =%s"%(stu_id))
row = cu.fetchone()
ifrow:
print("Sorry,该学号已存在,请重新输入")