python类库26[sqlite]
一 sqlite 与 python 的类型对应
二 实例
import sqlite3 def sqlite_basic():
# Connect to db
conn = sqlite3.connect('test.db')
# create cursor
c = conn.cursor()
# Create table
c.execute('''
create table if not exists stocks
(date text, trans text, symbol text,
qty real, price real)
'''
)
# Insert a row of data
c.execute('''
insert into stocks
values ('2006-01-05','BUY','REHT',100,35.14)
'''
)
# query the table
rows = c.execute("select * from stocks")
# print the table
for row in rows:
print(row)
# delete the row
c.execute("delete from stocks where symbol=='REHT'")
# Save (commit) the changes
conn.commit()
# Close the connection
conn.close()
def sqlite_adv():
conn = sqlite3.connect('test2.db')
c = conn.cursor()
c.execute('''
create table if not exists employee
(id text, name text, age inteage)
''')
# insert many rows
for t in [('1', 'itech', 10),
('2', 'jason', 10),
('3', 'jack', 30),
]:
c.execute('insert into employee values (?,?,?)', t)
# create index
create_index = 'CREATE INDEX IF NOT EXISTS idx_id ON employee (id);'
c.execute(create_index)
# more secure
t = ('jason',)
c.execute('select * from employee where name=?', t)
# fetch query result
for row in c.fetchall():
print(row)
conn.commit()
conn.close()
def sqlite_adv2():
# memory db
con = sqlite3.connect(":memory:")
cur = con.cursor()
# execute sql
cur.executescript('''
create table book(
title,
author,
published
);
insert into book(title, author, published)
values (
'AAA book',
'Douglas Adams',
1987
);
''')
rows = cur.execute("select * from book")
for row in rows:
print("title:" + row[0])
print("author:" + row[1])
print("published:" + str(row[2]))
def sqlite_adv3():
import datetime # Converting SQLite values to custom Python types
# Default adapters and converters for datetime and timestamp
con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
cur = con.cursor()
cur.execute("create table test(d date, ts timestamp)") today = datetime.date.today()
now = datetime.datetime.now() cur.execute("insert into test(d, ts) values (?, ?)", (today, now))
cur.execute("select d, ts from test")
row = cur.fetchone()
print today, "=>", row[0], type(row[0])
print now, "=>", row[1], type(row[1]) cur.execute('select current_date as "d [date]", current_timestamp as "ts [timestamp]" from test')
row = cur.fetchone()
print "current_date", row[0], type(row[0])
print "current_timestamp", row[1], type(row[1])
#sqlite_basic()
#sqlite_adv()
#sqlite_adv2()
#sqlite_adv3()
完!
python类库26[sqlite]的更多相关文章
- python类库26[web2py之基本概念]
一 web2py的应用的执行环境Models,Controllers和views所在的执行环境中,以下对象已经被默认地导入: Global Objects: request,response,ses ...
- 二十六. Python基础(26)--类的内置特殊属性和方法
二十六. Python基础(26)--类的内置特殊属性和方法 ● 知识框架 ● 类的内置方法/魔法方法案例1: 单例设计模式 # 类的魔法方法 # 案例1: 单例设计模式 class Teacher: ...
- Python中使用SQLite
参考原文 廖雪峰Python教程 使用SQLite SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是用C写的,而且体积很小,所以经常被集成到各种应用程序中,甚至在IOS和 ...
- python类库32[多进程同步Lock+Semaphore+Event]
python类库32[多进程同步Lock+Semaphore+Event] 同步的方法基本与多线程相同. 1) Lock 当多个进程需要访问共享资源的时候,Lock可以用来避免访问的冲突. imp ...
- PyCharm无法找到已安装的Python类库的解决方法
一.问题描述 软件系统:Windows10.JetBrains PyCharm Edu 2018.1.1 x64 在命令行cmd中安装python类库包Numpy.Matplotlib.Pandas. ...
- python 数据库操作 SQLite、MySQL 摘录
转自: http://www.cnblogs.com/windlaughing/p/3157531.html 不管使用什么后台数据库,代码所遵循的过程都是一样的:连接 -> 创建游标 -> ...
- 在Ubuntu上升级SQLite,并让Python使用新版SQLite
(本文适用于Debian系的Linux,如Ubuntu.Raspbian等等.) 在Linux上,Python的sqlite3模块使用系统自带的SQLite引擎,然而系统自带的SQLite可能版本太老 ...
- SnowNLP:一个处理中文文本的 Python 类库[转]
SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的,于是写了一个方便处理中文的类库,并且和Te ...
- SQLite in Python: 如何在Python中使用SQLite数据库
SQLite3 可使用 sqlite3 模块与 Python 进行集成.sqlite3 模块是由 Gerhard Haring 编写的.它提供了一个与 PEP 249 描述的 DB-API 2.0 规 ...
随机推荐
- 阶段3 2.Spring_08.面向切面编程 AOP_3 spring基于XML的AOP-编写必要的代码
新建项目 先改打包方式 导包,就先导入这俩包的坐标 aspectjweaver为了解析切入点表达式 新建业务层接口 定义三个方法 看返回和参数的区别.为了把这三类方法表现出来,并不局限于方法干什么事 ...
- database使用
参照文档 https://www.cnblogs.com/laoqing/p/8542487.html
- swagger-ui升级swagger-bootstrap-ui界面好看到起飞
如果项目已经集成了swagger,只需要在pom.xml添加,如果你的项目没有集成swagger,自行百度或看最下方的链接 swagger-bootstrap-ui是Swagger的前端UI实现,目的 ...
- 模仿抽奖转盘,并且用cookie记录历史次数
自己制作了一个模仿抽奖转盘的小游戏,代码比较简单,规则是只有三次抽奖机会,并且浏览器会记录抽奖的次数, 代码如下 <!DOCTYPE html> <html> <head ...
- 面试--hr常问的问题
程序员换工作,会有技术面试(可能不止一轮的技术面),还会有hr的面试,技术面主要是偏向于技术问题,hr面试主要问的一些问题,下面做下汇总: 1.你换工作的原因,你为何辞职 必问的问题,送分题或者送命题 ...
- CentOS7安装配置MariaDB(mysql)数据主从同步
CentOS7安装MariaDB并配置主从同步 环境声明: 防火墙firewalld及SElinux均为关闭状态 主库节点:192.168.0.63 从库节点:192.168.0.64 配置主库节点: ...
- Redis配置主从时报错“Could not connect to Redis at 192.168.0.50:6379: Connection refused not connected>”
配置Redis主从时,修改完从节点配置文件,然后报错 [root@Rich七哥-0-50 redis]# /opt/redis/redis-cli -h 192.168.0.50 Could not ...
- Office_Word使用技巧大全(超全)
目录 不收藏不行的 word 使用技巧大全 三招去掉页眉那条横线 批量转换全角字符为半角字符 快速打开最后编辑的文档 格式刷的使用 删除网上 下载 资料的换行符(象这种 "↓" ) ...
- spring boot-6.profile 多环境支持
在正式项目中一般都会区分多个环境,一般至少分为开发环境,测试生产环境,生产环境,实际可能会有更加精细的区分,针对不同的环境,项目的配置可能需要切换,spring boot 提供了很方便的环境切换方式. ...
- Oracle 查看 impdp 正在执行的内容
1. 今天进行数据库备份恢复 一直卡住 找了一下 公司另外一个部门的方神提供了一个方法连查看 具体在做什么操作: 2. 现象. impdp 到一个地方直接卡住不动 具体位置 view 这个地方足足卡 ...