手机
当前位置:查字典教程网 >脚本专栏 >python >Python3实现连接SQLite数据库的方法
Python3实现连接SQLite数据库的方法
摘要:本文实例讲述了Python3实现连接SQLite数据库的方法,对于Python的学习有不错的参考借鉴价值。分享给大家供大家参考之用。具体方法...

本文实例讲述了Python3实现连接SQLite数据库的方法,对于Python的学习有不错的参考借鉴价值。分享给大家供大家参考之用。具体方法如下:

实例代码如下:

import sqlite3 db = r"D:pyWorktest.db" #pyWork目录下test.db数据库文件 drp_tb_sql = "drop table if exists staff" crt_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() #创建表staff cur.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 staff" cur.execute(select_sql) #返回一个list,list中的对象类型为tuple(元组) date_set = cur.fetchall() for row in date_set: print(row) cur.close() con.close()

希望本文实例对大家的Python学习有所帮助。

【Python3实现连接SQLite数据库的方法】相关文章:

python 实现插入排序算法

Python通过解析网页实现看报程序的方法

Python去掉字符串中空格的方法

Python读写Redis数据库操作示例

python实现跨文件全局变量的方法

Python 连连看连接算法

如何运行Python程序的方法

python Django连接MySQL数据库做增删改查

使用python 获取进程pid号的方法

Python 分析Nginx访问日志并保存到MySQL数据库实例

精品推荐
分类导航