一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python 操作SQLite數據庫詳情

Python 操作SQLite數據庫詳情

2022-02-28 11:59lyshark Python

這篇文章主要介紹了Python 操作SQLite數據庫,SQLite,是一款輕型的數據庫,是遵守ACID的關系型數據庫管理系統,它包含在一個相對小的C庫中,下面來看看詳細內容,需要的朋友可以參考一下

Python 操作SQLite數據庫詳情

前言:

SQLite屬于輕型數據庫,遵守ACID的關系型數據庫管理系統,它包含在一個相對小的C庫中。在很多嵌入式產品中使用了它,它占用資源非常的低,python 中默認繼承了操作此款數據庫的引擎 sqlite3 說是引擎不如說就是數據庫的封裝版,開發自用小程序的使用使用它真的大贊

一、簡單操作SQLite數據庫

簡單操作SQLite數據庫:創建 sqlite數據庫是一個輕量級的數據庫服務器,該模塊默認集成在python中,開發小應用很不錯.

?
1
2
3
4
5
6
7
8
9
10
11
12
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)        # 執行創建表操作

1、簡單的插入語句的使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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)

2、簡單的查詢語句的使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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])

二、更新數據與刪除

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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()

三、實現用戶名密碼驗證

當用戶輸入錯誤密碼后,自動鎖定該用戶1分鐘.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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函數檢索一個指定范圍時間戳中的數據.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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")
    print(temp)

五、SQLite提取數據并繪圖

通過使用matplotlib這個庫函數,并提取出指定時間的數據記錄,然后直接繪制曲線圖.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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()

到此這篇關于Python 操作SQLite數據庫詳情的文章就介紹到這了,更多相關Python 操作SQLite數據庫內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://www.cnblogs.com/LyShark/p/12172674.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本一区二区在线不卡 | 欧美在线一级视频 | 久草草在线视视频 | 免费高清在线视频色yeye | 韩国三级在线观看 完整版 韩国三级视频网站 | 亚洲精品无码久久不卡 | 欧美贵妇vs高跟办公室 | 国内剧情麻豆 | 国产成人精品日本亚洲网址 | 精品一区二区三区免费毛片 | 久久两性视频 | 十六一下岁女子毛片免费 | 五花大绑esebdsm国产 | 99久久国产综合精麻豆 | jk制服白丝超短裙流白浆 | 国产在线伊人 | 32d乳白色的奶罩未删除 | 久久受www免费人成_看片中文 | 亚洲色图第四色 | 国产视频一区二区 | 99视频精品全部 在线 | 国产免费美女视频 | 成熟女人50岁一级毛片不卡 | 久久99视热频国只有精品 | 热巴在公交车h文 | yjsp妖精视频在线观看免费 | 欧美成人v视频免费看 | 精品一区二区三区高清免费不卡 | bedfriend泰剧全集免费观看 | 亚洲高清视频免费 | 猫咪社区在线播放 | 色综合天天网 | 欧美伊人久久久久久久久影院 | 久久久久嫩草影院精品 | 国产成人+亚洲欧洲 | 美国艳星lisann成人作品 | 日韩成人小视频 | 全色黄大色黄大片爽一次 | 日本欧美一二三区色视频 | 日韩黄色录像 | aigao视频 |