import sqlite3
con = sqlite3.connect('d:/test.db3') #test.db3存在则直接读取,不存在则创建
cur=con.cursor() #游标,可以认为类似于recordset
cur.execute('CREATE TABLE IF NOT EXISTS Studenta (Stuid TEXT, Age INTERGER, Name TEXT)')
cur.execute('INSERT INTO Studenta VALUES("00001", 20, "Lucy")')
cur.execute('INSERT INTO Studenta VALUES("00002", 21, "Lily")')
con.commit()
cur.execute('SELECT * FROM Studenta')
con.text_factory = str #sqlite默认为unicode输出,此处指定为str,即python默认的utf-8
print(cur.fetchone())
print(cur.fetchall())
# cur.close()
# con.close()
mylist = [("00003", 20, "David"), ("00004", 21, "Richard"),
("00005", 22, "qxzy")]
for s in mylist:
cur.execute('INSERT INTO Studenta VALUES(?, ?, ?)', (s[0], s[1], s[2]))
con.commit()
cur.execute('SELECT * FROM Studenta')
con.text_factory = str
print(cur.fetchall())
网友评论