Involved:
- Python Connection db file
- How to get data by column name
- How to pass in parameters
The sample code is as follows:
# 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:
# Set the name to use to get the data after the query
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,) comma after the tuple type
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
Please pay attention to the public number
"Python connects SQLite database Files"