Import a textfile to MS SQL with python

joel.sjoo at gmail.com joel.sjoo at gmail.com
Wed Sep 6 03:51:12 EDT 2006


I'm a dba for SQL server and I Will import a textfile to SQL. For
example I use a file with 3 columns. ID, Name and Surname and the
columns are tab separated. I don't know much about programming.
Anyway, I use this code below. It works, but it will not split the
columns. I have tried to change the argumnts in str(alllines[]) Some of
the columns can include many characters and some not. For exampel names
can be Bo or Lars-Ture.

I be glad if some can help me with this.

Regar Joel

import pymssql
import string,re

myconn =
pymssql.connect(host='lisa',user='sa',password='AGpu83!#',database='junk')
mycursor = myconn.cursor()

inpfile=open('c:\\temp\\test.txt','r')
for alllines in inpfile.read().split('\n'):
	stmt="insert into python (id, namn, efternamn) values ('%s', '%s',
'%s')" %(str(alllines[0]),str(alllines[2:10]),str(alllines[3:10]))

mycursor.execute(stmt)
	print stmt
inpfile.close()
myconn.commit()
myconn.close()




More information about the Python-list mailing list