您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Python3实现连接SQLite数据库的方法

2024/10/12 2:25:17发布27次查看
本文实例讲述了python3实现连接sqlite数据库的方法,对于python的学习有不错的参考借鉴价值。分享给大家供大家参考之用。具体方法如下:
实例代码如下:
import sqlite3db = rd:\pywork\test.db #pywork目录下test.db数据库文件drp_tb_sql = drop table if exists staffcrt_tb_sql = create table if not exists staff( id integer primary key autoincrement unique not null, name varchar(100), city varchar(100));#连接数据库con = sqlite3.connect(db)cur = con.cursor()#创建表staffcur.execute(drp_tb_sql)cur.execute(crt_tb_sql)#插入记录insert_sql = insert into staff (name,city) values (?,?) #?为占位符cur.execute(insert_sql,('tom','new york'))cur.execute(insert_sql,('frank','los angeles'))cur.execute(insert_sql,('kate','chicago'))cur.execute(insert_sql,('thomas','houston'))cur.execute(insert_sql,('sam','philadelphia'))con.commit()#查询记录select_sql = select * from staffcur.execute(select_sql)#返回一个list,list中的对象类型为tuple(元组)date_set = cur.fetchall()for row in date_set: print(row)cur.close()con.close()
希望本文实例对大家的python学习有所帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product