matsutus matsutus - 6 months ago 13
SQL Question

Python Sqlite inserting strings of numbers one at a time

So I have made a python script, which collects the creation-, and expirationdates of domainnames from a list of whois -results.

Now I would like to insert these dates into the table, and the hoped result would be

24.1.2005
24.1.2017
22.3.2002


But what the script inserts into the database is

('2')
('4')
('.')
('1')


I've tried converting the datatypes in the database to anything I can imagine, and that didn't do the trick.

def date():

o = open('outputfile.rtf')

for line in o:

line = line.replace('\n', '') //Just removing some breaks




if 'created' in line:

line = line.replace('created: ', '')

creationdate = str(line)

print (creationdate)
c.executemany("INSERT INTO pvm (creationdate) VALUES (?)", creationdate)


conn.commit()

Answer

Edit: Can you try to reformat creationdate to be:

creationdate = ['24.1.2005', '22.3.2002', '20.3.2006', '11.12.2013', '22.2.2005', '19.7.2005']

Note: In addition to the above - matsutus updated his code to the following line:

c.execute("INSERT INTO pvm (creationdate) VALUES (?)", (line,)) 

And the combination of both lines solved the problem.


https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-executemany.html

*EDIT BY THE ASKER *

Turning the data into an array, and then making sure the insert execute is

c.execute("INSERT INTO pvm (luomispvm) VALUES (?)", (line,))

instead of

c.execute("INSERT INTO pvm (luomispvm) VALUES (?)", line)

Fixed it!