• Home
  • About
    • 朱智博在Github上的Blog photo

      朱智博在Github上的Blog

      朱智博,朱智博的博客,zhuio,zhuio.github.io,

    • Learn More
    • Email
    • Github
    • Weibo
  • Posts
    • All Posts
    • All Tags
  • Projects

sqlite3

15 Jan 2017

Reading time ~1 minute

sqlite3使用

sqlite3基本使用方法

import sqlite3

conn = sqlite3.connect('tutorial.db')
c = conn.cursor()

def creat_table():
	c.execute("CREATE TABLE IF NOT EXISTS stuffToPlot(unix REAL,timeStamp TEXT,keyword TEXT,value REAL)")

def data_entry():
	c.execute("INSERT INTO stuffToPlot VALUES(141313141,'2017-1-14 00:37:24','python',5)")
	conn.commit()
	c.close()
	conn.close()

creat_table()
data_entry()

动态添加数据库

import sqlite3
import time
import datetime
import random

conn = sqlite3.connect('tutor1.db')
c = conn.cursor()

def creat_table():
	c.execute("CREATE TABLE IF NOT EXISTS stuffToPlot(unix REAL,timeStamp TEXT,keyword TEXT,value REAL)")

def data_entry():
	for i in range(10):
		unix = time.time()
		date = str(datetime.datetime.fromtimestamp(unix).strftime('%Y-%m-%d %H-%M-%S'))
		keyword = 'python'
		value = random.randrange(0,10)

		c.execute("INSERT INTO stuffToPlot (unix,timeStamp,keyword,value) VALUES(?,?,?,?)",(unix,date,keyword,value))
		conn.commit()

creat_table()
data_entry()
conn.close()


sqlite3 Share Tweet +1