"Python connects SQLite database Files"

Source: Internet
Author: User
Tags sqlite sqlite database


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"


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.