最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python 操作SQLite數(shù)據(jù)庫詳情

 更新時(shí)間:2021年11月11日 17:25:28   作者:lyshark  
這篇文章主要介紹了Python 操作SQLite數(shù)據(jù)庫,SQLite,是一款輕型的數(shù)據(jù)庫,是遵守ACID的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),它包含在一個(gè)相對(duì)小的C庫中,下面來看看詳細(xì)內(nèi)容,需要的朋友可以參考一下

前言:

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

一、簡(jiǎn)單操作SQLite數(shù)據(jù)庫

簡(jiǎn)單操作SQLite數(shù)據(jù)庫:創(chuàng)建 sqlite數(shù)據(jù)庫是一個(gè)輕量級(jí)的數(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)建表操作


1、簡(jiǎn)單的插入語句的使用

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、簡(jiǎn)單的查詢語句的使用

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()


三、實(shí)現(xiàn)用戶名密碼驗(yàn)證

當(dāng)用戶輸入錯(cuò)誤密碼后,自動(dòng)鎖定該用戶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:")  # 這個(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("賬戶 {} 還在限制登陸階段,請(qǐng)等待1分鐘...".format(username))
    else:
        print("用戶名輸入錯(cuò)誤")


四、SQLite檢索時(shí)間記錄

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

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)
# 批量生成一堆數(shù)據(jù),用于后期的測(cè)試.
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: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提取數(shù)據(jù)并繪圖

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

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()

到此這篇關(guān)于Python 操作SQLite數(shù)據(jù)庫詳情的文章就介紹到這了,更多相關(guān)Python 操作SQLite數(shù)據(jù)庫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python使用matplotlib顯示圖像實(shí)例

    Python使用matplotlib顯示圖像實(shí)例

    在Python項(xiàng)目中處理圖像數(shù)據(jù)之前,需要確保安裝了matplotlib庫,它是一個(gè)用于繪制圖表和圖像顯示的工具,若尚未安裝,可以使用pip命令進(jìn)行安裝,安裝完成后,可以通過matplotlib的pyplot模塊讀取并顯示MNIST手寫數(shù)據(jù)集中的圖像,若需要顯示灰度圖
    2024-10-10
  • 使用python編寫簡(jiǎn)單計(jì)算器

    使用python編寫簡(jiǎn)單計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了使用python編寫一個(gè)簡(jiǎn)單的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Python編程實(shí)現(xiàn)的簡(jiǎn)單Web服務(wù)器示例

    Python編程實(shí)現(xiàn)的簡(jiǎn)單Web服務(wù)器示例

    這篇文章主要介紹了Python編程實(shí)現(xiàn)的簡(jiǎn)單Web服務(wù)器功能,涉及Python URL請(qǐng)求與響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • 使用Python監(jiān)控文件內(nèi)容變化代碼實(shí)例

    使用Python監(jiān)控文件內(nèi)容變化代碼實(shí)例

    在python中文件監(jiān)控主要有兩個(gè)庫,一個(gè)是pyinotify,一個(gè)是watchdog。pyinotify依賴于Linux平臺(tái)的inotify,今天我們就來探討下pyinotify.
    2018-06-06
  • celery4+django2定時(shí)任務(wù)的實(shí)現(xiàn)代碼

    celery4+django2定時(shí)任務(wù)的實(shí)現(xiàn)代碼

    這篇文章主要介紹了celery4+django2定時(shí)任務(wù)的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • python 如何通過KNN來填充缺失值

    python 如何通過KNN來填充缺失值

    這篇文章主要介紹了python 通過KNN來填充缺失值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Django自定義User模型、認(rèn)證、權(quán)限控制的操作

    Django自定義User模型、認(rèn)證、權(quán)限控制的操作

    這篇文章主要介紹了Django自定義User模型、認(rèn)證、權(quán)限控制的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 淺談MySQL中的觸發(fā)器

    淺談MySQL中的觸發(fā)器

    這篇文章主要介紹了MySQL中的觸發(fā)器,包括使用觸發(fā)器添加、更新、刪除用戶等操作,需要的朋友可以參考下
    2015-05-05
  • python的launcher用法知識(shí)點(diǎn)總結(jié)

    python的launcher用法知識(shí)點(diǎn)總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于python的launcher用法知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2020-08-08
  • Python文件右鍵找不到IDLE打開項(xiàng)解決辦法

    Python文件右鍵找不到IDLE打開項(xiàng)解決辦法

    這篇文章主要介紹了Python文件右鍵找不到IDLE打開項(xiàng)解決辦法,本文使用注冊(cè)表解決了這個(gè)問題,需要的朋友可以參考下
    2015-06-06

最新評(píng)論

黄冈市| 黄石市| 银川市| 如皋市| 即墨市| 云龙县| 栾川县| 高青县| 伊春市| 灵川县| 张家界市| 兰州市| 红桥区| 乌鲁木齐市| 武功县| 涞水县| 花莲县| 广州市| 杨浦区| 江华| 晴隆县| 禹城市| 遵化市| 武陟县| 文水县| 英德市| 龙口市| 从江县| 南陵县| 大方县| 霍山县| 舞钢市| 禄丰县| 吉木乃县| 邮箱| 桂平市| 旬阳县| 沾化县| 定西市| 黄骅市| 扎兰屯市|