#!/usr/bin/env python
# -*- coding:utf-8 -*- import sqlite3,os,time
import traceback class Sqlite():
db_file = None # 数据库文件
connection = None # 数据库连接对象 def __init__(self):
self.db_file = "/www/wwwroot/ding-server/db/test.db" # 获取数据库对象
def GetConn(self):
try:
if self.connection == None:
self.connection = sqlite3.connect(self.db_file)
self.connection.text_factory = str
except Exception as ex:
traceback.print_exc()
return "error: " + str(ex) # 增加任务
def insert(self, sql):
self.write_lock()
self.GetConn()
self.connection.text_factory = str
try:
result = self.connection.execute(sql)
id = result.lastrowid
self.connection.commit()
self.rm_lock()
return id
except Exception as ex:
return "error: " + str(ex) # 删除任务
def delete(self, sql):
self.write_lock()
self.GetConn()
try:
result = self.connection.execute(sql)
self.connection.commit()
self.rm_lock()
return result.rowcount
except Exception as ex:
return "error: " + str(ex) # 修改任务状态(完成/未完成)
def update(self, sql):
self.GetConn()
try:
result = self.connection.execute(sql)
self.connection.commit()
return result.rowcount
except Exception as ex:
return "error: " + str(ex) # 查询任务
def select(self, sql):
self.GetConn()
result = self.connection.execute(sql)
data = result.fetchall()
tmp = list(map(list,data)) # 元组转成列表
data = tmp
return data # 方式1:创建数据表
def create(self):
self.GetConn()
sql = '''create table if not exists tb_user(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);'''
try:
self.connection.execute(sql)
self.connection.commit()
self.connection.close()
except Exception as ex:
traceback.print_exc()
return "error: " + str(ex) # 方式2:创建数据表
def create_back(self):
self.GetConn()
try:
fr = open('/www/wwwroot/ding-server/db/task.sql', 'rb')
result = self.connection.executescript(fr.read())
fr.close()
self.connection.commit()
return True
except Exception as ex:
traceback.print_exc()
return False def writeFile(self,filename,s_body,mode='w+'):
try:
fp = open(filename, mode);
fp.write(s_body)
fp.close()
return True
except:
try:
fp = open(filename, mode,encoding="utf-8");
fp.write(s_body)
fp.close()
return True
except:
return False # 是否有锁
def is_lock(self):
n = 0
while os.path.exists(self.__LOCK):
n+=1
if n > 50:
self.rm_lock()
break
time.sleep(0.01) # 写锁
def write_lock(self):
self.is_lock()
self.writeFile(self.__LOCK,"True") # 解锁
def rm_lock(self):
if os.path.exists(self.__LOCK):
os.remove(self.__LOCK) if __name__ == '__main__':
obj = Sqlite()
# obj.GetConn() # 获取数据库连接,没有的话,就创建数据库
obj.create()

参考:https://www.runoob.com/sqlite/sqlite-python.html

参考:https://www.jianshu.com/p/5ff30df3ba6b

Sqlite—Python接口的更多相关文章

  1. SQLite - Python

    SQLite - Python 安装 SQLite3 可使用 sqlite3 模块与 Python 进行集成.sqlite3 模块是由 Gerhard Haring 编写的.它提供了一个与 PEP 2 ...

  2. caffe的python接口学习(7):绘制loss和accuracy曲线

    使用python接口来运行caffe程序,主要的原因是python非常容易可视化.所以不推荐大家在命令行下面运行python程序.如果非要在命令行下面运行,还不如直接用 c++算了. 推荐使用jupy ...

  3. Windows+Caffe+VS2013+python接口配置过程

    前段时间在笔记本上配置了Caffe框架,中间过程曲曲折折,但由于懒没有将详细过程总结下来,这两天又在一台配置较高的台式机上配置了Caffe,配置时便非常后悔当初没有写到博客中去,现已配置好Caffe, ...

  4. 机器学习caffe环境搭建——redhat7.1和caffe的python接口编译

    相信看这篇文章的都知道caffe是干嘛的了,无非就是深度学习.神经网络.计算机视觉.人工智能这些,这个我就不多介绍了,下面说说我的安装过程即遇到的问题,当然还有解决方法. 说下我的环境:1>虚拟 ...

  5. Caffe + Ubuntu 14.04 64bit + 无CUDA(linux下安装caffe(无cuda)以及python接口)

    安装Caffe指导书 环境: Linux 64位 显卡为Intel + AMD,非英伟达显卡 无GPU 一. 安装准备工作 1. 以管理员身份登录 在左上角点击图标,搜索terminal(即终端),以 ...

  6. caffe的python接口学习(1):生成配置文件

    caffe是C++语言写的,可能很多人不太熟悉,因此想用更简单的脚本语言来实现.caffe提供matlab接口和python接口,这两种语言就非常简单,而且非常容易进行可视化,使得学习更加快速,理解更 ...

  7. Caffe学习系列(11):数据可视化环境(python接口)配置

    参考:http://www.cnblogs.com/denny402/p/5088399.html 这节配置python接口遇到了不少坑. 1.我是利用anaconda来配置python环境,在将ca ...

  8. caffe中python接口的使用

    下面是基于我自己的接口,我是用来分类一维数据的,可能不具通用性: (前提,你已经编译了caffe的python的接口) 添加 caffe塻块的搜索路径,当我们import caffe时,可以找到. 对 ...

  9. Caffe学习系列(13):数据可视化环境(python接口)配置

    caffe程序是由c++语言写的,本身是不带数据可视化功能的.只能借助其它的库或接口,如opencv, python或matlab.大部分人使用python接口来进行可视化,因为python出了个比较 ...

随机推荐

  1. python排序算法之一:冒泡排序(及其优化)

    相信冒泡排序已经被大家所熟知,今天看了一篇文章,大致是说在面试时end在了冒泡排序上,主要原因是不能给出冒泡排序的优化. 所以,今天就写一下python的冒泡排序算法,以及给出一个相应的优化.OK,前 ...

  2. 深入理解inode和硬链接和软连接和挂载点

    inode 一.inode是什么? 理解inode,要从文件储存说起. 扇区 文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5 ...

  3. re实战记录

    re实战记录 针对网页中的空格符 一般使用的.,但是它不能匹配\n,所以使用[\s\S]或者[\d\D]匹配所有字符 import re l1=r''' <div class="thu ...

  4. tabBarItem是模型,只有控件才有textColor属性

    如果通过模型设置控件的文字颜色,只能通过文本属性(富文本:颜色,字体,图文混排,空心)

  5. Hibernate注解之@Enumerated:针对枚举enum(转)

    https://my.oschina.net/xinxingegeya/blog/359968 @Column(name = "store_type", nullable = fa ...

  6. Ubuntu18.04下搭建LNMP教程-超详细图文(Nginx+MySQL+PHP含各种解决报错问题)

     笔者最近在VM15.0上安装了Ubuntu18.,先来安装一个LNMP(Nginx+MySQL+PHP)网站服务器架构,为后续的实验做准备~    LNMP是指一组通常一起使用来运行动态网站或者服务 ...

  7. 《手把手教你》系列练习篇之8-python+ selenium自动化测试 -压台篇(详细教程)

    1. 简介 本文是练习篇的最后一篇文章,虽然练习篇的文章到此就要和大家说拜拜了,但是我们的学习之路才刚刚开始.不要停下你的脚步,大步朝前走吧!比你优秀的人还在走着,我们有什么理由停下自己的脚步了,生命 ...

  8. DOM中操作结点的属性_操作元素结点的样式

    有俩种方式操作结点的属性. 首先我们需要先获取所要操作的结点元素: var uname=document.getElementById("uname"); var gan=unam ...

  9. FIve in a row

    Alice and Bob play 5-in-a-row game. They have a playing field of size 10 × 10. In turns they put eit ...

  10. Coderfocers-616c

    You are given a rectangular field of n × m cells. Each cell is either empty or impassable (contains ...