0

I need to add columns to a database and every second column does not have a name, i wanted to give it a generic "x" name but i get the sqlite3.OperationalError: duplicate column name: x error,

        for meci in ech1:

            c.execute("ALTER TABLE Aranjate ADD COLUMN "+ ech1[ii] +" INT")
            c.execute("ALTER TABLE Aranjate ADD COLUMN x INT")
            c.execute("ALTER TABLE Aranjate ADD COLUMN "+ ech2[ii] +" INT")
            conn.commit()
            ii = ii +1

and i tried to replace x with x = str(ii) so it will not have the same name and insert it as variable:

    c.execute("ALTER TABLE Aranjate ADD COLUMN " + x + " INT")

but i suppose that sqlite does not accept integers as table names as i get the error sqlite3.OperationalError: near "0": syntax error where 0 is the first x

It will not be a problem for me if those columns are named the same as all i will do with this table is export it as a csv file

1 Answer 1

2

SQLite does not allow column names to be the same because then that would defeat the purpose of a database, you would have contradicting data of the same property for each row.

SQLite also does not allow column names to start with digits, so you can't have 0one as column name, but zero1 is fine. However, you can add brackets around it to make it valid.

Try:

c.execute("ALTER TABLE Aranjate ADD COLUMN [" + x + "] INT")
Sign up to request clarification or add additional context in comments.

1 Comment

Column names that begin with digits are allowed, it is just that you have to quote them like any other identifier (such as those containing spaces) that needs special handling. You're probably better off using standard SQL quoting (i.e. double quotes) than SQL-Server style bracket quoting.

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.