2
I have an empty list that will be fed by inputs
.
In case, how do I insert the data stored in this list into a database (SQL Server)? I tried this way but it didn’t work.
import pyodbc, datetime
server = '90.90.90.90'
database = 'TESTE'
username = 'teste'
senha = 'teste123'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
now = datetime.date.today()
lista = []
v1 = lista.insert(0,input('entre com um numero:' ))
v2 = lista.insert(1,input('entre com o segundo numero:' ))
v3 = lista.insert(2,input('entre com o terceiro numero:'))
v4 = lista.insert(3,'{}'.format(now))
comandoSQL = ('INSERT INTO TESTE_INPUT (VARIAVEL_1, VARIAVEL_2, VARIAVEL_3,
VARIAVEL_4) VALUES (?,?,?,?)', (v1,v2,v3,v4))
print(comandoSQL)
cursor.execute(comandoSQL)
cnxn.commit()
Thank you! I will try and give feedback on.
– imaestri
It worked! It was worth too much...
– imaestri
Taking advantage of the topic, you know how to convert a datetime field to Insert?
– imaestri
@imaestri That’s another question. But I suggest that before you search the site, because there are already many questions about date conversion. If you don’t find anything for your specific problem, then you create another question. It is not that I do not want to answer, it is that we have to keep the site organized: a question by specific problem (anyway, if you do not find anything and ask another question, I will answer if possible) :-)
– hkotsubo
All right! Thank you..
– imaestri