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 规 ...
随机推荐
- Oracle 数据字典视图(V$,GV$,X$)
常用的几个数据字典: user_objects : 记录了用户的所有对象,包含表.索引.过程.视图等信息,以及创建时间,状态是否有效等信息,是非DBA用户的大本营.想知道自己有哪些对象,往这里查. u ...
- VirtualBox-5.2.8-121009-Win,虚拟机指令ifconfig不显示ip解决方法
- 批量生成xml文件数据C#实现
方法一 // < Records count = "5" > //< Record > // < Contact_ID > 5 - 55W - ...
- 【嵌入式开发】树莓派h264实时视频监控
FishXX之前用VLC串流输出视频,在电脑上需要VLC播放器.试了一下,感觉还是有点延时. 今天发现一个更加流畅,也不需要VLC播放器的树莓派远程视频监控方法,直接在浏览器中打开即可. 地址:htt ...
- sqlplus语句示例
我们通常所说的DML.DDL.DCL语句都是sql*plus语句,它们执行完后,都可以保存在一个被称为sql buffer的内存区域中,并且只能保存一条最近执行的sql语句,我们可以对保存在sql b ...
- Microsoft BarCode Control 16.0属性
Labview(2018)可通过Active调用Microsoft BarCode Control 16.0来生成条形码, 参考资料如下: 生成效果: 二维码: 条形码: 执行程序发现修改线条宽度不影 ...
- noip2015day2-运输计划
题目描述 公元$ 2044 $年,人类进入了宇宙纪元. \(L\) 国有 \(n\) 个星球,还有 \(n-1\) 条双向航道,每条航道建立在两个星球之间,这 \(n-1\) 条航道连通了 \(L\) ...
- 利用ansible进行主机信息收集
--- - hosts: myjob gather_facts: True vars: IP: "{{ ansible_default_ipv4['address'] }}" HO ...
- 树型DP入门
题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚 ...
- 解决 Intellij IDEA Cannot Resolve Symbol ‘BASE Decoder’ 问题
最近接盘了用springboot框架搭建的后台,第一次接触java的我就遇上了bug: 因为jdk更新而导致Cannot Resolve Symbol ‘BASE Decoder’ 问题 看了很多网上 ...