简体   繁体   中英

Python- Add dynamic number of columns to sqlite database

I am new to python and working on a project in which i need to create dynamic number of columns in table in a database. For example,

User input = 5    
Output=
Database:
    table:
        col1    col2    col3    col4    col5

Before this, I was creating dynamic number of tables using this loop:

for w in range(number + 1):
        IP.execute('CREATE TABLE IF NOT EXISTS table' + str(w) + '(column REAL)')

But now I do not need to create tables but want to add dynamic columns. I tried using similar loop but it does not work. Can anyone help me with this as I don't know much about sqlite

You can add a CHAR(20) column to an existing table x with the SQL statement

alter table x add column colName CHAR(20);

By writing a loop to construct the column names you can add as many as you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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