Reputation: 419
When adding columns to PostgreSQL using Python's psycopg2 module, why does the first column name always appear as the word "data"? I'm trying to better understand this for myself and can provide code as needed.
data | Fieldtest1 | Fieldtest2 | Fieldtest3 | Fieldtest4 | Fieldtest5 | Fieldtest6
------+------------+------------+------------+------------+------------+------------
(0 rows)
Code:
def add_cols(tablename, colnames, data):
conn = psycopg2.connect("dbname=test user=test_user password=test_password host=localhost port=5432")
cursor = conn.cursor()
###Create a new table
cursor.execute("CREATE TABLE IF NOT EXISTS \""+tablename+"\" (data text);")
###Layer the new table with column names
for colname in colnames:
cursor.execute("ALTER TABLE IF EXISTS "+tablename+" ADD IF NOT EXISTS \""+colname+"\" VARCHAR;")
Upvotes: 0
Views: 22
Reputation: 123
because you are specifying it here (in the parentheses):
cursor.execute("CREATE TABLE IF NOT EXISTS \""+tablename+"\" (data text);")
Upvotes: 1