Import a textfile to MS SQL with python

joel.sjoo at gmail.com joel.sjoo at gmail.com
Wed Sep 6 06:28:34 EDT 2006


Ok thanks Tim. I'm possible to read the file now as you described but
when I pass it to the DB I got an error says:

[['1', 'Joel', 'Sjoo'], ['2', 'Sture', 'Andersson'], ['3', 'Arne',
'Svensson']]
Traceback (most recent call last):
File "txttosql6.py", line 23, in ?
    row
File "C:\Python24\Lib\site-packages\pymssql.py", line 120, in execute
    self.executemany(operation, (params,))
File "C:\Python24\Lib\site-packages\pymssql.py", line 146, in
executemany
    raise DatabaseError, "internal error: %s (%s)" %
(self.__source.errmsg(), se
lf.__source.stdmsg())
pymssql.DatabaseError: internal error: None (None)

I dont know if it is the pymssql module that not work with this code. I
a code that you described.

import csv
import pymssql
reader = csv.reader (open ("c:\\temp\\test.txt"), delimiter="\t")
data = []
for line in reader:
  data.append (line)

myconn =
pymssql.connect(host='lisa',user='sa',password='',database='junk')
mycursor = myconn.cursor()
for row in data:
  mycursor.execute(
    "INSERT INTO python (id, namn, efternamn) VALUES (?, ?, ?)",
    row
  )

db.commit () # if needed etc.
db.close ()




More information about the Python-list mailing list