473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Import a textfile to SQL

I知 a dba for a SQL and I知 trying to learn how to import data from a simple text file. The text file looks like "1, name" "2, name2" and so on. How is it possible to solve this import with python? It痴 no problem to connect to the database and do some insert and select statement. It is when I put the read from text file and then insert this data to the database I got problem.

One script I have tried looks like this:
Expand|Select|Wrap|Line Numbers
  1. import pymssql
  2. import string,re
  3.  
  4. myconn = pymssql.connect(host='lisa',user='sa',password='',database='junk')
  5. mycursor = myconn.cursor()
  6.  
  7.  
  8. inpfile=open('c:\\temp\\test.txt','r')
  9. lines=inpfile.readlines()
  10. while lines <>'':
  11.     lines = inpfile.readline()
  12. inpfile.close()
  13.  
  14. stmt=("""insert into table (id, name) values (%s, %s)""" % ("'"+str(lines)+"'")
  15.  
  16. print mycursor
  17. mycursor.execute(stmt)
  18.  
  19. myconn.commit()
  20. myconn.close()
  21.  
Regard Joel
Sep 4 '06 #1
1 11004
ghostdog74
511 Expert 256MB
I知 a dba for a SQL and I知 trying to learn how to import data from a simple text file. The text file looks like "1, name" "2, name2" and so on. How is it possible to solve this import with python? It痴 no problem to connect to the database and do some insert and select statement. It is when I put the read from text file and then insert this data to the database I got problem.

One script I have tried looks like this:

import pymssql
import string,re

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


inpfile=open('c:\\temp\\test.txt','r')
lines=inpfile.readlines()
while lines <>'':
lines = inpfile.readline()
inpfile.close()

stmt=("""insert into table (id, name) values (%s, %s)""" % ("'"+str(lines)+"'")

print mycursor
mycursor.execute(stmt)

myconn.commit()
myconn.close()


Regard Joel

you could try this:
Expand|Select|Wrap|Line Numbers
  1. import pymssql
  2. import string,re
  3. myconn = pymssql.connect(host='lisa',user='sa',password='',database='junk')
  4. mycursor = myconn.cursor()
  5. inpfile=open('c:\\temp\\test.txt','r')
  6. while 1:
  7.          line = inpfile.readline()
  8.          if line == '':
  9.                   break
  10.          num , name = line.split()
  11.          stmt = ("""insert into table (id, name) values (%s, %s)""" % (num, "'"+str(name)+"'")
  12.          mycursor.execute(stmt)
  13.  
  14.  
  15. myconn.commit()
  16. myconn.close()
  17.  
Untested, so you have to try it yourself
Sep 6 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Larry Rekow | last post by:
I have a report that's created each day as a flat textfile. Because I came from the Access world, I created a macro that imports it with a schema that gives meaningful names to the various...
3
by: Elaksomfan | last post by:
Hi, I am having trouble importing data from an excel spreadsheet into MS SQL Server 2000 using DTS Wizard. The DTS import process is successfull, no errors, but only 50 rows of approx. 1500 rows...
2
by: Sigurd Bruteig | last post by:
Hi all! With the transferText method I have made a routine that import a textfile to a table.The textfile is a pricelist from the supplier. The problem is that the supplier uses . (dot) as...
4
by: mibispam | last post by:
Hello, I'm trying to import a textfile into an Access table. I want to write a macro to do all the work automatically. My textfile looks like that: artNr;description;sellingValue...
1
by: ghadley_00 | last post by:
Hi, I have a MS access database table for which I regularly need to import fixed width text data. At present I have to to cut and paste the text data from its source to a text file, save the...
1
by: joel.sjoo | last post by:
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....
2
by: arunaksha7 | last post by:
I am new to ASP.NET. I am facing a problem to import contents of a textfile to my ASP.NET Web Application datagrid.
9
by: a | last post by:
Dear friends I want import data from CSV file to mdb file How can I do that in vb.net?
5
by: kashif73 | last post by:
I have a text file with hundreds of records. each line contain 1250 values seperated by a semicolon. I have created 2 tables in SQL server 2000, one with 1000 columns & the second with 250 columns....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project用lanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.