一 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]的更多相关文章

  1. python类库26[web2py之基本概念]

    一 web2py的应用的执行环境Models,Controllers和views所在的执行环境中,以下对象已经被默认地导入: Global Objects:  request,response,ses ...

  2. 二十六. Python基础(26)--类的内置特殊属性和方法

    二十六. Python基础(26)--类的内置特殊属性和方法 ● 知识框架 ● 类的内置方法/魔法方法案例1: 单例设计模式 # 类的魔法方法 # 案例1: 单例设计模式 class Teacher: ...

  3. Python中使用SQLite

    参考原文 廖雪峰Python教程 使用SQLite SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是用C写的,而且体积很小,所以经常被集成到各种应用程序中,甚至在IOS和 ...

  4. python类库32[多进程同步Lock+Semaphore+Event]

    python类库32[多进程同步Lock+Semaphore+Event]   同步的方法基本与多线程相同. 1) Lock 当多个进程需要访问共享资源的时候,Lock可以用来避免访问的冲突. imp ...

  5. PyCharm无法找到已安装的Python类库的解决方法

    一.问题描述 软件系统:Windows10.JetBrains PyCharm Edu 2018.1.1 x64 在命令行cmd中安装python类库包Numpy.Matplotlib.Pandas. ...

  6. python 数据库操作 SQLite、MySQL 摘录

    转自: http://www.cnblogs.com/windlaughing/p/3157531.html 不管使用什么后台数据库,代码所遵循的过程都是一样的:连接 -> 创建游标 -> ...

  7. 在Ubuntu上升级SQLite,并让Python使用新版SQLite

    (本文适用于Debian系的Linux,如Ubuntu.Raspbian等等.) 在Linux上,Python的sqlite3模块使用系统自带的SQLite引擎,然而系统自带的SQLite可能版本太老 ...

  8. SnowNLP:一个处理中文文本的 Python 类库[转]

    SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的,于是写了一个方便处理中文的类库,并且和Te ...

  9. SQLite in Python: 如何在Python中使用SQLite数据库

    SQLite3 可使用 sqlite3 模块与 Python 进行集成.sqlite3 模块是由 Gerhard Haring 编写的.它提供了一个与 PEP 249 描述的 DB-API 2.0 规 ...

随机推荐

  1. Python学习之==>迭代器

    一.概要 在了解Python的数据结构时,容器(container).可迭代对象(iterable).迭代器(iterator).生成器(generator).列表/集合/字典推导式(list,set ...

  2. Function程序设计及应用

    Function也称为函数,它是SAP中一个独物的程序模式,一般是一段单独的程序代码,可独立执行或直接被SAP其他程序所调用.Function支持远程访问模式,即提供接口供SAP程序使用(如VB,.N ...

  3. java:easyui(jQueryEasyUI,分页)

    1.介绍: jQuery EasyUI是一组基于jQuery的UI插件集合体,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不需要编写复杂的ja ...

  4. 【翻唱】学习日语歌 (青鸟)火影忍者 OP

    我的翻唱:https://node.kg.qq.com/play?s=Q1cY4PQ2-2VP6QOM&g_f=personal 中文音译: 哈巴哒伊哒啦 摸多啦呐伊哆伊迭 内杂西耷诺哇 啊哦 ...

  5. 接口自动化-python unittest+requests+HTMLrunner

    从2015年毕业入行软件测试,快满4年了,之前技术分享都在百度贴吧上面,现在正式开始在博客中记录工作技术,努力成长,加油 接口测试的步骤1.组装好该接口需要的参数数据2.使用get或post附带参数数 ...

  6. Python unittest 之 BeautifulReport可视化报告

    众所周知的报告是HTMLTestRunner,虽然经过众多的大神修改后,功能挺强大的,但这颜值,我就不多说了,大家自己感受下吧 HTMLTestRunner就不多说了,近来发现了一款款式新颖,还不漏油 ...

  7. centos没有ifcfg-eth0或者ifcfg-eth0文件内容为空

    虚拟机安装好CentOS 6系统后,发现ip在每次重启后都会还原,用ifconfig查看是有eth0网卡的(也有可能只有回环网卡lo),于是查看eth0网卡配置文件,发现在 /etc/sysconfi ...

  8. 微信小程序开发(三)----- 云开发案例

    1.发送请求 2.云函数中发送请求,例子request https://github.com/request/request-promise    创建云函数movielist,右键在终端打开,输入  ...

  9. 微信小程序开发(二)----- 云开发

    1.概念 微信小程序的云开发是腾讯云与微信团队深度合作推出的一个全新的小程序的解决方案,它提供了云函数.云数据库与云存储这三大基础能力支持,随着云开发的出现,小程序的开发者可以将服务端的部署和运营的环 ...

  10. IF-ELSE嵌套练习

    一,1,编写程序,由键盘输入三个整数分别存入变量num1,num2,num3中,对它们进行排序,使用if-else结构,并按从小到大的顺序输出: package practice; import ja ...