国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Python 操作SQLite數(shù)據(jù)庫的示例

瀏覽:17日期:2022-07-08 08:55:43

SQLite,是一款輕型的數(shù)據(jù)庫,是遵守ACID的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),它包含在一個(gè)相對小的C庫中。在很多嵌入式產(chǎn)品中使用了它,它占用資源非常的低,python 中默認(rèn)繼承了操作此款數(shù)據(jù)庫的引擎 sqlite3 說是引擎不如說就是數(shù)據(jù)庫的封裝版,開發(fā)自用小程序的使用使用它真的大贊

簡單操作SQLite數(shù)據(jù)庫:創(chuàng)建 sqlite數(shù)據(jù)庫是一個(gè)輕量級的數(shù)據(jù)庫服務(wù)器,該模塊默認(rèn)集成在python中,開發(fā)小應(yīng)用很不錯(cuò).

import sqlite3# 數(shù)據(jù)表的創(chuàng)建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) # 執(zhí)行創(chuàng)建表操作

簡單操作SQLite數(shù)據(jù)庫:簡單的插入語句的使用

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()) # 取出所有的數(shù)據(jù)select = 'select * from persion where name=’lyshark’;'cursor.execute(select)print(cursor.fetchall()) # 取出所有的數(shù)據(jù)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])

簡單的更新數(shù)據(jù)與刪除

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()) # 取出所有的數(shù)據(jù)conn.commit() # 事務(wù)提交,每執(zhí)行一次數(shù)據(jù)庫更改的操作,就執(zhí)行提交cursor.close()conn.close()

SQLite小試牛刀 實(shí)現(xiàn)用戶名密碼驗(yàn)證,當(dāng)用戶輸入錯(cuò)誤密碼后,自動(dòng)鎖定該用戶1分鐘.

import sqlite3import re,timeconn = 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:') # 這個(gè)地方應(yīng)該嚴(yán)謹(jǐn)驗(yàn)證,盡量不要讓用戶拼接SQL語句 password = input('passwor:') # 此處為了方便不做任何驗(yàn)證(注意:永遠(yuǎn)不要相信用戶的輸入) 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('當(dāng)前用戶{}沒有被限制,允許登錄...'.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('用戶名正確,但是密碼錯(cuò)誤了...') else: print('賬戶 {} 還在限制登陸階段,請等待1分鐘...'.format(username)) else: print('用戶名輸入錯(cuò)誤')

SQLite檢索時(shí)間記錄 通過編寫的TimeIndex函數(shù)檢索一個(gè)指定范圍時(shí)間戳中的數(shù)據(jù).

import os,time,datetimeimport sqlite3'''conn = sqlite3.connect('data.db')cursor = conn.cursor()create = 'create table lyshark(' 'time int primary key,' 'cpu int not null' ')'cursor.execute(create)# 批量生成一堆數(shù)據(jù),用于后期的測試.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 傳入數(shù)據(jù)庫名稱# table = 指定表lyshark名稱# start = 2019-12-12 14:28:00# ends = 2019-12-12 14:29:20def 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提取數(shù)據(jù)并繪圖 通過使用matplotlib這個(gè)庫函數(shù),并提取出指定時(shí)間的數(shù)據(jù)記錄,然后直接繪制曲線圖.

import os,time,datetimeimport sqlite3import numpy as npfrom matplotlib import pyplot as pltdef 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數(shù)據(jù)庫的示例的詳細(xì)內(nèi)容,更多關(guān)于Python 操作SQLite數(shù)據(jù)庫的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 亚洲精品国产第一区第二区国 | 99国产精品欧美久久久久久影院 | 久久久久免费 | 日本一极毛片兔费看 | 国产图片亚洲精品一区 | 欧美精品 日韩 | 在线91精品亚洲网站精品成人 | 久久精品高清视频 | 国产人成午夜免费噼啪视频 | 欧美白人和黑人xxxx猛交视频 | 美女视频全部免费 | 欧美性精品hd在线观看 | 99精品久久秒播无毒不卡 | 欧美骚视频 | 国产成人香蕉在线视频网站 | 九九色综合 | 特级毛片aaaa级毛片免费 | 欧美色xxx| 国产亚洲人成a在线v网站 | 日韩视频在线观看一区 | 久久污 | 日韩三级精品 | 最新更新国内自拍视频 | 欧美一区二区在线免费观看 | 91精品一区二区三区在线 | 99视频在线观看免费视频 | 中文字幕一二三四区2021 | 在线观看黄网视频免费播放 | 深夜福利视频在线观看免费视频 | 欧美在线观看一区二区 | 国产一区二区免费在线观看 | 天堂视频在线免费观看 | 亚洲高清免费在线观看 | 日本三级韩国三级在线观看a级 | 九九热视频在线播放 | 99久久99久久精品免费看子伦 | 欧美日韩精品在线播放 | 在线播放免费一级毛片欧美 | 国内免费视频成人精品 | 亚洲一级特黄特黄的大片 | 九九99香蕉在线视频免费 |