0
Good night.
I’m trying to put a dynamic column with the function
class db():
def __init__(self,coluna):
self.coluna = coluna
def inserir_coluna_tabela1(self):
import sqlite3 as sql
conn = sql.connect('db/dbase.db')
cursor = conn.cursor()
cursor.execute("""
ALTER TABLE 'tabela01'
ADD COLUMN ? 'TEXT NOT NULL'
""", self.coluna)
conn.commit()
#
print('Novo campo adicionado com sucesso.')
#
conn.close()
camponovo = db('2018')
camponovo.inserir_coluna_tabela1()
and is returning error: sqlite3.Operationalerror: near "?": syntax error
Thank you!
You cannot pass something structural as parameter of Prepared statement. In this case, you should interpolate the value of
self.colunain the string ofALTER TABLE. The marks of?only for DML, not for DDL– Jefferson Quesado
Thanks! I made this change and it worked: cursor.execute("ALTER TABLE " + self.table +" ADD COLUMN '" + self.column +"' TEXT")
– Renato Lacerda
The jsbueno answer answers your question. I think it is appropriate to mark it as accepted
– Jefferson Quesado