佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > python3 sqlite3限制條件查詢的操作

python3 sqlite3限制條件查詢的操作

熱門(mén)標(biāo)簽:呼叫中心市場(chǎng)需求 百度AI接口 電話運(yùn)營(yíng)中心 企業(yè)做大做強(qiáng) 客戶服務(wù) Win7旗艦版 硅谷的囚徒呼叫中心 語(yǔ)音系統(tǒng)

請(qǐng)注意10,11,24行的代碼,是本條博客的精華,邏輯并不難,就是有些小語(yǔ)法問(wèn)題比較糾結(jié),記錄一下。

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

補(bǔ)充:python + sqlite3 基本操作

連接數(shù)據(jù)庫(kù)

import sqlite3 
# 連接數(shù)據(jù)庫(kù)(如果不存在則創(chuàng)建)
conn = sqlite3.connect('test.db')
print("Opened database successfully")
 
# 創(chuàng)建游標(biāo)
cursor = conn.cursor() 
...
 
# 關(guān)閉游標(biāo)
cursor.close()
# 提交事物
conn.commit()
# 關(guān)閉連接
conn.close()

創(chuàng)建表

...
# 創(chuàng)建游標(biāo)
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)建游標(biāo)
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)建游標(biāo)
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()
...

其他操作

自增字段起始位置

# 設(shè)置起始值為1
update sqlite_sequence SET seq = 0 where name = '表名';
# 設(shè)置全部表起始值為默認(rèn)值
delete from sqlite_sequence where name='TableName'; --注意表名區(qū)分大小寫(xiě)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • python 操作sqlite數(shù)據(jù)庫(kù)的方法
  • python sqlite3 判斷cursor的結(jié)果是否為空的案例
  • Python3+SQLAlchemy+Sqlite3實(shí)現(xiàn)ORM教程
  • Python 操作SQLite數(shù)據(jù)庫(kù)的示例
  • python數(shù)據(jù)庫(kù)如何連接SQLite詳解

標(biāo)簽:海南 山西 濟(jì)南 安康 喀什 山西 長(zhǎng)沙 崇左

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python3 sqlite3限制條件查詢的操作》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    绥宁县| 铜鼓县| 榆社县| 扎赉特旗| 丹巴县| 金阳县| 宜宾县| 蒙山县| 五常市| 娄底市| 托克托县| 芜湖市| 淮南市| 元谋县| 惠安县| 睢宁县| 娱乐| 格尔木市| 九寨沟县| 托里县| 曲周县| 鄂托克旗| 商洛市| 偃师市| 崇州市| 涞水县| 绵竹市| 衡东县| 永州市| 新巴尔虎左旗| 招远市| 保德县| 溧阳市| 龙泉市| 成安县| 应城市| 阜康市| 应用必备| 成安县| 宁蒗| 潮州市|