zoukankan      html  css  js  c++  java
  • python 打开sqlite3内存数据库,操作完毕将数据保存到文件数据库

    http://my.oschina.net/u/89296/blog/42717

    #encoding=utf-8
    # 甄码农代码 2012 03 06
    # 打开sqlite3内存数据库,执行操作,将内存数据库保存到文件

    import sqlite3
    import StringIO

    #使用:memory:标识打开的是内存数据库
    con = sqlite3.connect(":memory:")
    cur = con.cursor()
    #使用executescript可以执行多个脚本
    cur.executescript("""
        create table quotes(
            rid INTEGER PRIMARY KEY AUTOINCREMENT,
            code char(10) NOT NULL,
            high real,
            open real,
            low real,
            close real,
            amount real,
            volume real)""")


    #execute执行脚本,参数要放到元组中
    cur.execute('insert into quotes(code,high,open,low,close,amount,volume) values(?,?,?,?,?,?,?)',
                ('600036',12.0,11.8,11.7,11.9,999999,8999))

    #打印数据表数据
    cur.execute("select * from quotes")
    print cur.fetchall()

    #生成内存数据库脚本
    str_buffer = StringIO.StringIO()
    #con.itrdump() dump all sqls
    for line in con.iterdump():
        str_buffer.write('%s\n' % line)

    #关闭内存数据库
    cur.close()


    #打开文件数据库
    con_file = sqlite3.connect('quotes.db3')
    cur_file = con_file.cursor()
    #执行内存数据库脚本
    cur_file.executescript(str_buffer.getvalue())
    #关闭文件数据库
    cur_file.close()

  • 相关阅读:
    linux 下安装mongodb
    python 多线程, 多进程, 协程
    5.rabbitmq 主题
    4.rabbitmq 路由
    e.target与e.currentTarget对比
    使用ffmpeg下载m3u8流媒体
    本机添加多个git仓库账号
    IE hack 条件语句
    IE8 兼容 getElementsByClassName
    IE 下 log 调试的一点不同
  • 原文地址:https://www.cnblogs.com/lexus/p/2402928.html
Copyright © 2011-2022 走看看