Python 操作SQLite数据库的示例
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。在很多嵌入式产品中使用了它,它占用资源非常的低,python 中默认继承了操作此款数据库的引擎 sqlite3 说是引擎不如说就是数据库的封装版,开发自用小程序的使用使用它真的大赞
简单操作SQLite数据库:创建 sqlite数据库是一个轻量级的数据库服务器,该模块默认集成在python中,开发小应用很不错.
import sqlite3 # 数据表的创建 conn = sqlite3.connect("data.db") cursor = conn.cursor() create = "create table persion(" \ "id int auto_increment primary key," \ "name char(20) not null," \ "age int not null," \ "msg text default null" \ ")" cursor.execute(create) # 执行创建表操作
简单操作SQLite数据库:简单的插入语句的使用
insert = "insert into persion(id,name,age,msg) values(1,'lyshark',1,'hello lyshark');" cursor.execute(insert) insert = "insert into persion(id,name,age,msg) values(2,'guest',2,'hello guest');" cursor.execute(insert) insert = "insert into persion(id,name,age,msg) values(3,'admin',3,'hello admin');" cursor.execute(insert) insert = "insert into persion(id,name,age,msg) values(4,'wang',4,'hello wang');" cursor.execute(insert) insert = "insert into persion(id,name,age,msg) values(5,'sqlite',5,'hello sql');" cursor.execute(insert) data = [(6, '王舞',8, 'python'), (7, '曲奇',8,'python'), (9, 'C语言',9,'python')] insert = "insert into persion(id,name,age,msg) values(?,?,?,?);" cursor.executemany(insert,data)
简单的查询语句的使用
select = "select * from persion;" cursor.execute(select) #print(cursor.fetchall()) # 取出所有的数据 select = "select * from persion where name='lyshark';" cursor.execute(select) print(cursor.fetchall()) # 取出所有的数据 select = "select * from persion where id >=1 and id <=2;" list = cursor.execute(select) for i in list.fetchall(): print("字段1:", i[0]) print("字段2:", i[1])
简单的更新数据与删除
update = "update persion set name='苍老师' where id=1;" cursor.execute(update) update = "update persion set name='苍老师' where id>=1 and id<=3;" cursor.execute(update) delete = "delete from persion where id=3;" cursor.execute(delete) select = "select * from persion;" cursor.execute(select) print(cursor.fetchall()) # 取出所有的数据 conn.commit() # 事务提交,每执行一次数据库更改的操作,就执行提交 cursor.close() conn.close()
SQLite小试牛刀 实现用户名密码验证,当用户输入错误密码后,自动锁定该用户1分钟.
import sqlite3 import re,time conn = sqlite3.connect("data.db") cursor = conn.cursor() """create = "create table login(" \ "username text not null," \ "password text not null," \ "time int default 0" \ ")" cursor.execute(create) cursor.execute("insert into login(username,password) values('admin','123123');") cursor.execute("insert into login(username,password) values('guest','123123');") cursor.execute("insert into login(username,password) values('lyshark','1231');") conn.commit()""" while True: username = input("username:") # 这个地方应该严谨验证,尽量不要让用户拼接SQL语句 password = input("passwor:") # 此处为了方便不做任何验证(注意:永远不要相信用户的输入) sql = "select * from login where username='{}'".format(username) ret = cursor.execute(sql).fetchall() if len(ret) != 0: now_time = int(time.time()) if ret[0][3] <= now_time: print("当前用户{}没有被限制,允许登录...".format(username)) if ret[0][0] == username: if ret[0][1] == password: print("用户 {} 登录成功...".format(username)) else: print("用户 {} 密码输入有误..".format(username)) times = int(time.time()) + 60 cursor.execute("update login set time={} where username='{}'".format(times,username)) conn.commit() else: print("用户名正确,但是密码错误了...") else: print("账户 {} 还在限制登陆阶段,请等待1分钟...".format(username)) else: print("用户名输入错误")
SQLite检索时间记录 通过编写的TimeIndex函数检索一个指定范围时间戳中的数据.
import os,time,datetime import sqlite3 """ conn = sqlite3.connect("data.db") cursor = conn.cursor() create = "create table lyshark(" \ "time int primary key," \ "cpu int not null" \ ")" cursor.execute(create) # 批量生成一堆数据,用于后期的测试. for i in range(1,500): times = int(time.time()) insert = "insert into lyshark(time,cpu) values({},{})".format(times,i) cursor.execute(insert) conn.commit() time.sleep(1)""" # db = data.db 传入数据库名称 # table = 指定表lyshark名称 # start = 2019-12-12 14:28:00 # ends = 2019-12-12 14:29:20 def TimeIndex(db,table,start,ends): start_time = int(time.mktime(time.strptime(start,"%Y-%m-%d %H:%M:%S"))) end_time = int(time.mktime(time.strptime(ends,"%Y-%m-%d %H:%M:%S"))) conn = sqlite3.connect(db) cursor = conn.cursor() select = "select * from {} where time >= {} and time <= {}".format(table,start_time,end_time) return cursor.execute(select).fetchall() if __name__ == "__main__": temp = TimeIndex("data.db","lyshark","2019-12-12 14:28:00","2019-12-12 14:29:00")
SQLite提取数据并绘图 通过使用matplotlib这个库函数,并提取出指定时间的数据记录,然后直接绘制曲线图.
import os,time,datetime import sqlite3 import numpy as np from matplotlib import pyplot as plt def TimeIndex(db,table,start,ends): start_time = int(time.mktime(time.strptime(start,"%Y-%m-%d %H:%M:%S"))) end_time = int(time.mktime(time.strptime(ends,"%Y-%m-%d %H:%M:%S"))) conn = sqlite3.connect(db) cursor = conn.cursor() select = "select * from {} where time >= {} and time <= {}".format(table,start_time,end_time) return cursor.execute(select).fetchall() def Display(): temp = TimeIndex("data.db","lyshark","2019-12-12 14:28:00","2019-12-12 14:29:00") list = [] for i in range(0,len(temp)): list.append(temp[i][1]) plt.title("CPU Count") plt.plot(list, list) plt.show() if __name__ == "__main__": Display()
文章作者:lyshark
文章出处:https://www.cnblogs.com/lyshark
以上就是Python 操作SQLite数据库的示例的详细内容,更多关于Python 操作SQLite数据库的资料请关注猪先飞其它相关文章!
相关文章
- 这篇文章主要介绍了python-opencv-画外接矩形框的实例代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-04
- 以前我们开发大型项目时都会用到svn来同步,因为开发产品的人过多,所以我们会利用软件来管理,今天发有一居然可以利用php来管理svn哦,好了看看吧。 代码如下 ...2016-11-25
- 操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了...2016-11-25
Python astype(np.float)函数使用方法解析
这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 2022虎年新年即将来临,小编为大家带来了一个利用Python编写的虎年烟花特效,堪称全网最绚烂,文中的示例代码简洁易懂,感兴趣的同学可以动手试一试...2022-02-14
- 在本篇文章里小编给大家分享的是一篇关于python中numpy.empty()函数实例讲解内容,对此有兴趣的朋友们可以学习下。...2021-02-06
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
python-for x in range的用法(注意要点、细节)
这篇文章主要介绍了python-for x in range的用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-05-10- 这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
- 这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
- 这篇文章主要介绍了python如何实现b站直播自动发送弹幕,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下...2021-02-20
- 这篇文章主要介绍了Intellij IDEA连接Navicat数据库的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借价值,需要的朋友可以参考下...2021-03-25
- 在开发过程中,我们经常会将日期时间的毫秒数存放到数据库,但是它对应的时间看起来就十分不方便,我们可以使用一些函数将毫秒转换成date格式。 一、 在MySQL中,有内置的函数from_unixtime()来做相应的转换,使用如下: 复制...2014-05-31
python Matplotlib基础--如何添加文本和标注
这篇文章主要介绍了python Matplotlib基础--如何添加文本和标注,帮助大家更好的利用Matplotlib绘制图表,感兴趣的朋友可以了解下...2021-01-26- 这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
- C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
- 通过内网连另外一台机器的mysql服务, 确发现速度N慢! 等了大约几十秒才等到提示输入密码。 但是ping mysql所在服务器却很快! 想到很久之前有过类似的经验, telnet等一些服务在连接请求的时候,会做一些反向域名解析(如果...2015-10-21
- 今天小编就为大家分享一篇python 计算方位角实例(根据两点的坐标计算),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27
- 某些时候,例如为了搭建一个测试环境,或者克隆一个网站,需要复制一个已存在的mysql数据库。使用以下方法,可以非常简单地实现。假设已经存在的数据库名字叫db1,想要复制一份,命名为newdb。步骤如下:1. 首先创建新的数据库newd...2015-10-21