涉及:
- Python连接db文件
- 如何按照列名获取数据
- 如何传入参数
示例代码如下:
# encoding=utf-8
import sqlite3
def get_signal_names(db_file_path):
names = list()
with sqlite3.connect(db_file_path, isolation_level="IMMEDIATE", timeout=60, check_same_thread=False) as con:
# 设置可以使用名称来获取查询后的数据
con.row_factory = sqlite3.Row
cur = con.cursor()
sql = "select distinct(Name) from Channels order by Name asc"
cur.execute(sql)
results = cur.fetchall()
for row in results:
names.append(row["Name"])
return names
def get_channel_record(db_file_path, signal_name):
channel = dict()
with sqlite3.connect(db_file_path, isolation_level="IMMEDIATE", timeout=60, check_same_thread=False) as con:
con.row_factory = sqlite3.Row
cur = con.cursor()
sql = "select MessageId, MessageChannel, SignalType from test_tb where Name = ?"
# 注意(signal_name,)元组类型后的逗号
cur.execute(sql, (signal_name,))
re = cur.fetchone()
channel["message_id"] = re["MessageId"]
channel["signal_type"] = re["SignalType"]
channel["message_channel"] = re["MessageChannel"]
return channel
更多请关注微信公众号
原文地址:https://www.cnblogs.com/nelson2013/p/9251938.html
时间: 2024-11-06 17:23:23