Python存储变量的两列到用SQLite创建的表中



我创建了一个变量,该变量存储患者ID和每个患者错过预约的数量。我用SQLite创建了一个表,我试图将我的变量存储到我创建的表中,但我得到了"ValueError"的错误:参数是不支持的类型。下面是我到目前为止的代码:

import pandas as pd
import sqlite3
conn = sqlite3.connect('STORE')
c = conn.cursor()
c.execute("DROP TABLE IF EXISTS PatientNoShow")
c.execute("""CREATE TABLE IF NOT EXISTS PatientNoShow ("PatientId" text, "No-show" text)""")
df = pd.read_csv(r"C:missedappointments.csv")
df2 = df[df['No-show']=="Yes"]
pt_counts = df2["PatientId"].value_counts()
c.executemany("INSERT OR IGNORE INTO PatientNoShow VALUES (?, ?)", pt_counts)

提前感谢您的帮助!还在学习,所以任何"解释"都像我5岁一样。欢迎回答!此外,一旦我创建了表并在其中存储了信息,我将如何打印或获得输出的可视化?

您在

中写到这两个变量的类型是text
c.execute("""CREATE TABLE IF NOT EXISTS PatientNoShow ("PatientId" text, "No-show" text)""")

pt_counts包含整数,因为它计数PatientId列中的值,此外.executemany()需要一个序列才能正常工作。

如果PatientId是字符串类型,这段代码应该工作:

import pandas as pd
import sqlite3
conn = sqlite3.connect('STORE')
c = conn.cursor()
c.execute("DROP TABLE IF EXISTS PatientNoShow")
c.execute("""CREATE TABLE IF NOT EXISTS PatientNoShow ("PatientId" text, "No-show" integer)""") # type changed
df = pd.read_csv(r"C:/Users/bob/Desktop/Trasporti_project/Matchings_locations/norm_data/standard_locations.csv")
pt_counts = df["standard_name"].value_counts()
c.executemany("INSERT OR IGNORE INTO PatientNoShow VALUES (?, ?)", pt_counts.iteritems()) # this is a sequence