2

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

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thats right thanks! and even we can add column of any type as i have used type REAL.
Sure - this was just an example to get you started

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.