python 之 sqlite3
# -*- coding: utf-8 -*-
''' 创建数据库日志,三列为时间 身份证号和备注名'''
import os
import sys
import sqlite3
import datetime class sqlite3_log(): def __init__(self):
pass #连接表,表名为参
def connect_db(self,db_name):
conn = sqlite3.connect(db_name)
return conn #关闭连接,表名为参
def close_db(self,conn):
conn.close() #建立一个表 表名应该使用参数确定
def create_table(self,conn,table_name):
cu = conn.cursor()
cu.execute('''create table if not exists %s
(time_now text primary key not null,
id_card float not null,
name text not null);''' %table_name)
conn.commit() #插入数据
def insert_values(self, table_name, datetime, idcard, name):
cu = conn.cursor()
cu.execute("insert into %s values ( ?, ?, ?)" %table_name, (datetime, idcard, name))
conn.commit() #查询表是否存在
def query_table(conn,db_name,table_name):
cu = conn.cursor()
cu.execute("select %s from %s where type = 'table' " %db_name, table_name )
return cu.fetchall() #逻辑流程
def run_log(self, db_name, table_name, tim, idcard, name):
global conn
#检测数据库表是否存在
if os.path.exists(db_name):
print "the table is exists"
conn = sqlite3.connect(db_name) else:
print "######"
conn = self.connect_db(db_name) #检测表是否存在并创建表
print "create table if not exists"
self.create_table(conn, table_name)
print "create table-conn"
#a = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print "insert finished"
self.insert_values(table_name, tim, idcard, name)
#关闭连接
self.close_db(conn) def test():
#测试代码
db_name = './Peeer.db'
a = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
g = "wei"
#print time1
sq = sqlite3_log()
conn = sq.connect_db(db_name)
#sq.create_table(conn, "talbe_sqlite") sq.insert_values("talbe_sqlite" , a , "", g)
sq.insert_values("talbe_sqlite" , a, "", "guo")
sq.close_db(conn) if __name__ == '__main__':
#测试逻辑流程
sq = sqlite3_log()
a = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
sq.run_log('./Peeeeer.db', "talbe_sqlite", a, "", "guo")
此代码已经测试,可以直接调用此类把数据写入。
python 之 sqlite3的更多相关文章
- 《Python操作SQLite3数据库》快速上手教程
为什么使用SQLite数据库? 对于非常简单的应用而言,使用文件作为持久化存储通常就足够了,但是大多数复杂的数据驱动的应用需要全功能的关系型数据库.SQLite的目标则是介于两者之间的中小系统.它有以 ...
- Python 中 sqlite3的使用
Python 中 sqlite3的使用 一.sqlite安装 下载地址 http://www.sqlite.org 1.数据库生成 sqlite3.exe testdb 2.创建表格,插入数据 3.在 ...
- Python访问sqlite3数据库取得dictionary的正路!
[引子] 很多人都知道,Python里是内置了很好用的sqlite3的.但这个库有个缺陷,在执行fetchall()/fetchone()等方法后,得到的是一个tuple.以前吧,做自己的小项目,tu ...
- Python之sqlite3
Python sqlite3数据库是一款非常小巧的内置模块,它使用一个文件存储整个数据库,操作十分方便,相比其他大型数据库来说,确实有些差距.但是在性能表现上并不逊色,麻雀虽小,五脏俱全,sqlite ...
- python 对 sqlite3的简单使用
SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成.Python就内 ...
- PYTHON 对SQLITE3的简单使用
SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成.Python就内 ...
- Python安装sqlite3
今天使用PYthon时,发现错误 ImportError: No module named sqlite 这是因为缺少 SQLITE3的缘故. 下面分享一下解决此问题的方法步骤: 1. 查看是Pyth ...
- python用sqlite3模块操作sqlite数据库-乾颐堂
SQLite是一个包含在C库中的轻量级数据库.它并不需要独立的维护进程,并且允许使用非标准变体(nonstandard variant)的SQL查询语句来访问数据库. 一些应用可是使用SQLite保存 ...
- python之sqlite3使用详解
Python SQLITE数据库是一款非常小巧的嵌入式开源数据库软件,也就是说没有独立的维护进程,所有的维护都来自于程序本身.它使用一个文件存储整个数据库,操 作十分方便.它的最大优点是使用方便,功能 ...
- Python处理Sqlite3数据库
sqlite3比较小众 本章主要通过Python Code表述如何增.查.改.删 sqlite3 DB 一.直接上代码 #!/usr/bin/env python # -*- coding: utf- ...
随机推荐
- hdu 5017 模拟退火算法
hdu 5017 http://blog.csdn.net/mypsq/article/details/39340601 #include <cstdio> #include <cs ...
- poj 2823单调队列模板题
#include<stdio.h>//每次要吧生命值长的加入,吧生命用光的舍弃 #define N 1100000 int getmin[N],getmax[N],num[N],n,k, ...
- 《Spring in action》之装配Bean
创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入的本质. Spring装配Bean的三种主要机制: 1.在XML中进行显示配置 2.在java中进行显示配置 3.隐式的bean发现机制和自动 ...
- 武大OJ 613. Count in Sama’s triangle
Description Today, the math teacher taught Alice Hui Yang’s triangle. However, the teacher came up w ...
- BZOJ(6) 1084: [SCOI2005]最大子矩阵
1084: [SCOI2005]最大子矩阵 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3566 Solved: 1785[Submit][Sta ...
- pga_aggregate_target, sga_target, memory_target
对于这三个参数有一些了解,但是又有一些疑惑. pga_aggregate_target 最初的了解: 这个参数控制着PGA的大小,如果work_area_policy 设置成auto,则oracle采 ...
- WebDev.WebServer40.EXE
http://www.cnblogs.com/tong-tong/archive/2013/05/02/3049428.html 大学玩asp.net时就发现VS在Debug时会起一个web服务,这东 ...
- Android 属性动画(Property Animation) 全然解析 (上)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38067475 1.概述 Android提供了几种动画类型:View Animat ...
- PHP中错误与异常的日志记录用法分析
原文:http://www.jb51.net/article/89548.htm ----------------------------------------------------------- ...
- 十进制浮点数转换成IEEE754标准的32浮点数的二进制格式
参考: http://jimmygod.blog.163.com/blog/static/43511339200792605627411/ http://blog.csdn.net/archersab ...