Mihai Moldovan
Mihai Moldovan

Reputation: 137

How can i add column with same name in sqlite

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

Upvotes: 0

Views: 789

Answers (1)

yanxun
yanxun

Reputation: 626

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")

Upvotes: 2

Related Questions