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

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

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

服務器之家 - 腳本之家 - Python - python3 sqlite3限制條件查詢的操作

python3 sqlite3限制條件查詢的操作

2021-10-06 00:072021樂樂 Python

這篇文章主要介紹了python3 sqlite3限制條件查詢的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

請注意10,11,24行的代碼,是本條博客的精華,邏輯并不難,就是有些小語法問題比較糾結,記錄一下。

?
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
40
41
42
43
44
45
46
47
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 基本操作

連接數據庫

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import sqlite3
# 連接數據庫(如果不存在則創建)
conn = sqlite3.connect('test.db')
print("Opened database successfully")
 
# 創建游標
cursor = conn.cursor()
...
 
# 關閉游標
cursor.close()
# 提交事物
conn.commit()
# 關閉連接
conn.close()

創建表

?
1
2
3
4
5
6
7
8
9
10
11
...
# 創建游標
cursor = conn.cursor()
 
# 創建表
sql = 'CREATE TABLE Student(id integer PRIMARY KEY autoincrement, Name varchar(30), Age integer)'
cursor.execute(sql)
 
# 提交事物
conn.commit()
...

插入數據

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
# 創建游標
cursor = conn.cursor()
 
# 插入數據
sql = "INSERT INTO Student(Name, Age) VALUES(\'love\', 22)"
cursor.execute(sql)
 
# 插入數據 2
data = ('love2', 2221) # or ['love2', 2221]
sql = "INSERT INTO Student(Name, Age) VALUES(?, ?)"
cursor.execute(sql, data)
 
# 提交事物
conn.commit()
...

查詢數據

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
# 創建游標
cursor = conn.cursor()
 
# 查詢數據
sql = "select * from Student"
values = cursor.execute(sql)
for i in values:
 print(i)
 
# 查詢數據 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
2
3
4
# 設置起始值為1
update sqlite_sequence SET seq = 0 where name = '表名';
# 設置全部表起始值為默認值
delete from sqlite_sequence where name='TableName'; --注意表名區分大小寫

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。如有錯誤或未考慮完全的地方,望不吝賜教。

原文鏈接:https://blog.csdn.net/weixin_42528089/article/details/94395107

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91视频免费观看网站 | 男人的天堂日本 | 国产精品亚洲一区二区 | 久久精品中文闷骚内射 | 亚洲欧美国产另类视频 | 加勒比一本大道香蕉在线视频 | 日本孕妇与黑人xxxxxx | 暖暖视频高清图片免费完整版 | 国产资源视频在线观看 | 四虎国产欧美成人影院 | 欧美特黄视频在线观看 | 楚乔传第二部全60集免费观看 | 亚州vs欧州vs日 | 欧美特黄三级在线观看 | www.国产一区二区三区 | 91狠狠| 色鬼网| 久久午夜夜伦痒痒想咳嗽P 久久无码AV亚洲精品色午夜麻豆 | blacked在线播放 | 欧美日韩国产一区二区三区伦 | 国内自拍视频在线观看 | 成人性色生活片免费网 | 肉大捧一进一出视频免费播放 | 亚洲国产五月综合网 | www.87福利| 我和岳的性事小说 | 91在线高清视频 | 国产普通话对白露脸流出 | 经典WC女厕所里TV | 青草碰人人澡人人澡 | 99久久精品国语对白 | 午夜深情在线观看免费 | 99视频久久 | 精品国产福利片在线观看 | 大乳孕妇一级毛片 | 成人性生交大片免费看软件 | 欧美专区在线视频 | www.四虎com | 午夜精品久久久久久久2023 | 蜜桃在线 | 天使萌痴汉在线中文字幕 |