首页 > 数据库技术 > 详细

SQLite

时间:2018-10-23 11:30:17      阅读:295      评论:0      收藏:0      [点我收藏+]

一、简介

sqlite是个关系型的嵌入式数据库,简单易用,而且在大多数的操作系统上都默认安装了

 

二、基本命令

1、创建数据库

sqlite3 /data/tmp/wtv.db

2、查看数据库

sqlite> .databases
seq  name             file
---  ---------------  ----------------------------------------------------------
0    main             /data/tmp/wtv.db

3、创建表

CREATE TABLE w02840(id integer primary key autoincrement, uuid char(50) not null unique, no int not null);

4、查看当前数据库下面的表

sqlite> .tables
w02840

 

三、导出和导入

# 导出
sqlite3 /data/tmp/wtv.db .dump > wtv.sql
# 导入
sqlite3 /data/tmp/wtv.db < wtv.sql

 

四、接口(Python)

# python已经内置了SQLite3驱动

#!/usr/bin/env python

import json
import sqlite3
import urllib2

TEMPLATES = [0951, 0152, 0553, 02538, 0284, 02861, 0285, 02862]

URL = "http://127.0.0.1:8000/epg/getDetails.shtml?templateId=%s"


def wuuid(c, template, url):
    try:
        response = urllib2.urlopen(url, timeout=1)
    except urllib2.URLError:
        print 0  # 接口Error则输出0
        return 0  # 退出当前循环
    else:
        data = json.load(response)
        if data == []:
            print "online %s empty" % template
        else:
            for item in data:
                sql = "insert into w%s (uuid, no) values (‘%s‘, %d)" % (template, item[uuid], item[no])
                c.execute(sql)


if __name__ == __main__:

    conn = sqlite3.connect(/data/tmp/wtv.db)
    c = conn.cursor()
    for template in TEMPLATES:
        url = URL % template
        wuuid(c, template, url)
    conn.commit()
    print "Records created successfully";
    conn.close()

 

SQLite

原文:https://www.cnblogs.com/metasequoia/p/9835443.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!