简体   繁体   English

如何使用列表中的列名创建SQLite3表?

[英]How to create a SQLite3 table with column names from a list?

I'm storing patients' daily blood pressure data in an SQLite3 table. 我将患者的每日血压数据存储在SQLite3表中。 Each patient corresponds to a row, each date corresponds to a column. 每个患者对应一行,每个日期对应一列。 How do I initialize this table with the column names being the dates from a Python Pandas series? 如何使用列名是Python Pandas系列的日期来初始化此表? Perhaps something like: 也许像这样:

DATE_LIST = pandas.date_range(start_date, end_date)
cursor.execute('''CREATE TABLE bloodpressure DATE_LIST REAL''')

The above will create a table with a single column named DATE_LIST, which is not what I want. 上面的代码将创建一个只有一个名为DATE_LIST的列的表,这不是我想要的。 I want to use the dates in DATE_LIST as column names. 我想使用DATE_LIST中的日期作为列名。

You are breaking the 1NF of databases. 您正在破坏数据库的1NF。 Consider restructuring the database to follow the normal form. 考虑按照正常形式重组数据库。

In simpler terms, each row should be a patients blood pressure at some date. 简单来说,每一行都应该是某个日期的患者血压。

patient_id | bloodpressure | date
-----------+---------------+-----------
101        | 120/80        | 2016-08-04
101        | 130/84        | 2016-08-03  
102        | 150/90        | 2016-08-03

The SQL statement to create this could look something like 创建此语句的SQL语句可能类似于

CREATE TABLE bloodpressure (
    patient_id integer,
    bloodpressure text,
    date text,
    primary key(patient_id)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM