zoukankan      html  css  js  c++  java
  • python 使用sqlite3

    Sqlite是一个轻量级的数据库,类似于Access.
    一、 安装

    Python 2.5开始提供了对sqlite的支持,带有sqlite3库.

    没有sqlite的版本需要去PySqlite主页上下载安装包.

    PySqlite下载地址http://code.google.com/p/pysqlite/downloads/list

    二、创建数据库/打开数据库
    Sqlite使用文件作为数据库,你可以指定数据库文件的位置。

    1. import sqlite3 #导入模块  
    2. cx = sqlite3.connect("d:\test.db")  

    #这个是建立在内存里, 内存中的任何操作都不需要commit
    #cx = sqlite3.connect(':memory:')

    使 用sqlite的connect可以连接一个数据库文件,当数据库文件不存在的时候,它会自动创建。如果已经存在这个文件,则打开这个文件。cx为数据库连接对象。

    三、操作数据库的基本对象
    3.1 数据库连接对象

    象前面的cx就是一个数据库的连接对象,它可以有以下操作:

             commit()--事务提交 
             rollback()--事务回滚 
             close()--关闭一个数据库连接 
             cursor()--创建一个游标 

    3.2 游标对象 所有sql语句的执行都要在游标对象下进行。

    cu = cx.cursor()#这样定义了一个游标。

    游标对象有以下的操作: 


            execute()--执行sql语句 
            executemany--执行多条sql语句 
            close()--关闭游标 
            fetchone()--从结果中取一条记录 
            fetchmany()--从结果中取多条记录 
            fetchall()--从结果中取出多条记录 
            scroll()--游标滚动 

    四、使用举例
    4.1 建库

    1. import sqlite3 #导入模块  
    2. cx = sqlite3.connect("d:\test.db")  

    4.2 建表

    1. cu=cx.cursor()   
    2. u.execute("""create table catalog ( id integer primary key, pid integer, name varchar(10) UNIQUE )""")  

    上面语句创建了一个叫catalog的表,它有一个主键id,一个pid,和一个name,name是不可以重复的。

    关于sqlite支持的数据类型,在它主页上面的文档中有描述,可以参考:Version 2 DataTypes.

    4.3 insert(插入)

    1. cu.execute("insert into catalog values(0, 0, 'name1')")   
    2. cu.execute("insert into catalog values(1, 0, 'hello')")   
    3. cx.commit()  

    如果你愿意,你可以一直使用cu游标对象。注意,对数据的修改必须要使用事务语句:commit()或rollback(),且对象是数据库连接对象,这里为cx。

    4.4 select(选择)

    1. cu.execute("select * from catalog")   
    2. print cu.fetchall()  

    [(0, 0, 'name1'), (1, 0, 'hello')]
    fetchall() 返回结果集中的全部数据,结果为一个tuple的列表。每个tuple元素是按建表的字段顺序排列。注意,游标是有状态的,它可以记录当前已经取到结果的 第几个记录了,因此,一般你只可以遍历结果集一次。在上面的情况下,如果执行fetchone()会返回为空。这一点在测试时需要注意。

    1. cu.execute("select * from catalog where id = 1")   
    2. print cu.fetchone()   

    (1, 0, 'hello')
    对数据库没有修改的语句,执行后不需要再执行事务语句。

    4.5 update(修改)

    1. cu.execute("update catalog set name='name2' where id = 0")   
    2. cx.commit()   
    3. cu.execute("select * from catalog")   
    4. print cu.fetchone()   

    (0, 0, 'name2')
    4.6
    delete(删除)

    1.  cu.execute("delete from catalog where id = 1")   
    2.  cx.commit()   
    3.  cu.execute("select * from catalog")   
    4.  cu.fetchall()   
    5. #cu.close()  
    6. #cx.close()   

    判断表是否存在

    'SELECT  count(*)   FROM sqlite_master WHERE type="table" AND name = "your_table_name"


    [(0, 0, 'name2')]
    原文参考:
    http://www.cnblogs.com/luckeryin/archive/2009/09/25/1574152.html
  • 相关阅读:
    cut的使用
    linux三剑客之一 sed
    uniq指令
    CF940A Points on the line 思维
    2018年全国多校算法寒假训练营练习比赛(第二场) B TaoTao要吃鸡 01背包变形题
    CF922A Cloning Toys
    牛客网 Wannafly挑战赛 C 列一列 简单题 (题目有点坑)
    牛客网 Wannafly挑战赛 A 找一找 思考题
    B. Tea Queue codeforces Round.37.div2 队列
    线段树+离散化 poj 2528 Mayor's posters
  • 原文地址:https://www.cnblogs.com/chenjianhong/p/4144440.html
Copyright © 2011-2022 走看看