数据库
Sqlite数据库
链接数据库并创建游标对象
co = sqlite3.connect('fish.db')
cu = co.cursor()
执行语句
cu.execute("CREATE TABLE tfish (fid char(10) ,fname char(50) ,fhabit char(50)) ") #建表语句
cu.execute("INSERT INTO tfish(fid,fname,fhabit) VALUES ('f1','鲤鱼','杂食性')") #执行插入语句
| 方法 | 描述 |
|---|
| 游标对象.execute(sql) | 执行一条sql语句 |
| 游标对象.execute(sql,parameters) | 执行一条带参语句 |
| 游标对象.executemany(sql,parameters) | 执行多条带参语句 |
a = [('f2','草鱼','食草性'),('f3','鲫鱼','温水性'),('f4','鳙鱼','温水性'),('f5','黑鱼','肉食性')]
cu.executemany("INSERT INTO tfish VALUES (?,?,?)",a)
co.commit()#提交数据库
cu.execute("SELECt * FROM tfish")#查询
for x in range(5):
print(cu.fetchone())#获取游标查询结果