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 规 ...
随机推荐
- template要加s,重启服务,不然报错
- ABAP 实现内表自定义的F4功能
“实现多列内容的F4功能 REPORT Z_TAB_TEST. TYPES: shlp_descr TYPE shlp_descr . DATA: BEGIN OF itab OCCURS 0 ...
- jeecms v9图标不显示问题
- tensorflow-2.0 技巧 | ImageNet 归一化
_MEAN_RGB = [123.15, 115.90, 103.06] def _preprocess_subtract_imagenet_mean(inputs): ""&qu ...
- 应用安全 - 免杀 - 工具 - the-backdoor-factory - 使用|命令 - 汇总
安装 Kali下方式一: git clone https://github.com/secretsquirrel/the-backdoor-factory方式二: apt-get install ba ...
- 【神经网络与深度学习】Caffe部署中的几个train-test-solver-prototxt-deploy等说明
1:神经网络中,我们通过最小化神经网络来训练网络,所以在训练时最后一层是损失函数层(LOSS), 在测试时我们通过准确率来评价该网络的优劣,因此最后一层是准确率层(ACCURACY). 但是当我们真正 ...
- ubuntu中用命令创建数据库
# switch to postgres account sudo su postgres # create a new postgres login: jetty; and set the pass ...
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 H. Skiing
题意:在这个寒假中,鲍勃有计划在山区度假胜地滑雪.这个滑雪胜地有M个不同的滑雪道和N个不同的标志位于那些转弯处点.从第S标记到第T标志的第i路径具有长度L.每个路径必须遵循降低高度的原则,起点必须严格 ...
- php转码 iconv和mb_convert_encoding
最近在给公司做一个小工具,将excel表格按照一定的格式转为txt文本格式,要求转后的txt文本是GBK编码,但是总会有几个excel表格无法正常转码,最后查阅相关资料,得到解决方案 先说明下,在ph ...
- 无法安装 Microsoft Visual Studio 2010 Service Pack 1,因为此计算机的状态不支持此安装。有关如何解决此问题的说明,请参阅 Microsoft 下载中心网站上的自述文件
解决办法: 32 位系统删除:HKEY_LOCAL_MACHINE\Software\Microsoft\VSTO Designtime Setup\ 64 位系统删除:HKEY_LOCAL_MACH ...