在SQLite中设置F字符串格式会出现操作错误:没有这样的列:在VSCode中使用Jupiter Notebook

l7wslrjt  于 2022-11-15  发布在  SQLite
关注(0)|答案(1)|浏览(160)


数据库中有一个DrugName列,但我收到这个错误,指出该列不存在。

def create_connection(db_file):
    
    conn = None
    try:
        conn = sqlite3.connect(db_file)
    except Error as e:
        print(e)

    return conn

def select_by_slot(conn, slot_name, slot_value):
    """
    Query all rows in the tasks table
    :param conn: the Connection object
    :return:
    """
    cur = conn.cursor()
    cur.execute("SELECT * FROM finale WHERE {}='{}'".format(slot_name, slot_value))

    rows = cur.fetchall()

    if len(list(rows)) < 1:
        print("There are no resources matching your query.")
    else:
        print(rows)
        # for row in random.sample(rows, 1):
        #     print(f"Try the {(row[0])}")

select_by_slot(create_connection("ubats.db"),
    slot_name = 'DrugName',slot_value= 'Beclomethasone dipropionate 100mcg and formoterol fumarate dihydrate 6mcg pressurized inhalation solution')

我希望能够搜索特定的药物是否在该列中。如果是,则打印该行。我也尝试过搜索和使用f-字符串格式,但不起作用。
能给我点子吗?
错误消息:

---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
/Users/karan/udev/ubat_db/talk_db.ipynb Cell 2 in <cell line: 33>()
     29         print(rows)
     30         # for row in random.sample(rows, 1):
     31         #     print(f"Try the {(row[0])}")
---> 33 select_by_slot(create_connection("ubats.db"),
     34     slot_name = 'DrugName',slot_value= 'Beclomethasone dipropionate 100mcg and formoterol fumarate dihydrate 6mcg pressurized inhalation solution')

/Users/karan/udev/ubat_db/talk_db.ipynb Cell 2 in select_by_slot(conn, slot_name, slot_value)
     16 """
     17 Query all rows in the tasks table
     18 :param conn: the Connection object
     19 :return:
     20 """
     21 cur = conn.cursor()
---> 22 cur.execute("SELECT * FROM finale WHERE {}='{}'".format(slot_name, slot_value))
     24 rows = cur.fetchall()
     26 if len(list(rows)) < 1:

OperationalError: no such column: DrugName
0h4hbjxa

0h4hbjxa1#

这个问题显然是由于数据库创建不当造成的。

df = pd.read_csv('/Users/karan/Downloads/ubat.csv') 
df.to_sql('finale', conn, if_exists='append', index=False)

这种创建数据库的方式解决了这个问题。谢谢大家。

相关问题