python3 sqlite3限制條件查詢的操作
更新時間:2021年04月07日 11:19:54 作者:2021樂樂
這篇文章主要介紹了python3 sqlite3限制條件查詢的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
請注意10,11,24行的代碼,是本條博客的精華,邏輯并不難,就是有些小語法問題比較糾結,記錄一下。
import json import sqlite3 import re import argparse def Get(db_file): conn = sqlite3.connect(db_file) cur = conn.cursor() print("5555555") value1=(60)# this is must be () cur.execute("select * from exception where AGV_ID=(%s)" %(value1)) #cursor.execute("insert into exception values('%s', '%s','%s' ) " %(start_time ,ID ,infomation)) result= cur.fetchall() print("result:",result) for i in result: print(i) print("******************************888") def get_agv_id(db_file): try: conn = sqlite3.connect(db_file) cur = conn.cursor() cur.execute("select * from exception where AGV_ID=51") #print( cur.fetchall()) result= cur.fetchall() for i in result: print(i) except sqlite3.Error,e: print(e) if __name__ == '__main__': parser = argparse.ArgumentParser(description='check the information of db') #parser.add_argument('-h', '--help', help='Statistics for abnormal information') parser.add_argument('-n', '--name', help=' the db of name ') args = vars(parser.parse_args()) db_name = args['name'] print("db_name:",db_name) conn = sqlite3.connect('db_name') cursor = conn.cursor() Get('fitkits.db') get_agv_id('fitkits.db') conn.commit() conn.close() print('DONE!') print("666")
補充:python + sqlite3 基本操作
連接數(shù)據(jù)庫
import sqlite3 # 連接數(shù)據(jù)庫(如果不存在則創(chuàng)建) conn = sqlite3.connect('test.db') print("Opened database successfully") # 創(chuàng)建游標 cursor = conn.cursor() ... # 關閉游標 cursor.close() # 提交事物 conn.commit() # 關閉連接 conn.close()
創(chuàng)建表
... # 創(chuàng)建游標 cursor = conn.cursor() # 創(chuàng)建表 sql = 'CREATE TABLE Student(id integer PRIMARY KEY autoincrement, Name varchar(30), Age integer)' cursor.execute(sql) # 提交事物 conn.commit() ...
插入數(shù)據(jù)
... # 創(chuàng)建游標 cursor = conn.cursor() # 插入數(shù)據(jù) sql = "INSERT INTO Student(Name, Age) VALUES(\'love\', 22)" cursor.execute(sql) # 插入數(shù)據(jù) 2 data = ('love2', 2221) # or ['love2', 2221] sql = "INSERT INTO Student(Name, Age) VALUES(?, ?)" cursor.execute(sql, data) # 提交事物 conn.commit() ...
查詢數(shù)據(jù)
... # 創(chuàng)建游標 cursor = conn.cursor() # 查詢數(shù)據(jù) sql = "select * from Student" values = cursor.execute(sql) for i in values: print(i) # 查詢數(shù)據(jù) 2 sql = "select * from Student where id=?" values = cursor.execute(sql, (1,)) for i in values: print('id:', i[0]) print('name:', i[1]) print('age:', i[2]) # 提交事物 conn.commit() ...
其他操作
自增字段起始位置
# 設置起始值為1 update sqlite_sequence SET seq = 0 where name = '表名'; # 設置全部表起始值為默認值 delete from sqlite_sequence where name='TableName'; --注意表名區(qū)分大小寫
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
如何在Python函數(shù)執(zhí)行前后增加額外的行為
有的時候會需要在函數(shù)前后添點額外的功能(比如過濾、計時等)時,以前總是首先想到裝飾器。最近學習了Python的上下文管理器,所以本文就給大家介紹了如何在Python函數(shù)執(zhí)行前后增加額外的行為,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-10-10Python 安裝setuptools和pip工具操作方法(必看)
下面小編就為大家?guī)硪黄狿ython 安裝setuptools和pip工具操作方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05基于使用paramiko執(zhí)行遠程linux主機命令(詳解)
下面小編就為大家?guī)硪黄谑褂胮aramiko執(zhí)行遠程linux主機命令(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10python和Appium移動端多設備自動化測試框架實現(xiàn)
這篇文章主要介紹了python和Appium移動端多設備自動化測試框架實現(xiàn),基于pytest和Appium框架,支持Android和iOS功能自動化的測試框架的相關內(nèi)容,需要的小伙伴可以參考一下2022-04-04