473,789 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Import a textfile to SQL

1 New Member
I’m a dba for a SQL and I’m 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’s 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 11233
ghostdog74
511 Recognized Expert Contributor
I’m a dba for a SQL and I’m 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’s 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',us er='sa',passwor d='',database=' junk')
mycursor = myconn.cursor()


inpfile=open('c :\\temp\\test.t xt','r')
lines=inpfile.r eadlines()
while lines <>'':
lines = inpfile.readlin e()
inpfile.close()

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

print mycursor
mycursor.execut e(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
2545
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 columns, and then uses a query to massage some of the data for me (deletes the first blank row and does a couple of calculations) Then I use DTS to import the Access query as a table. the textfile has a column called "File_num", and among several...
3
3671
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 of data are imported. I tried to remove 20 rows in the excel spreadsheet in the interval row 0-50. When i later ran the import, only 30 rows were imported. I deleted almost every row in the interval 0-50, with the result of the import having 0...
2
455
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 descimal point, but the standard desimal point is , (comma). How do i change the desimal point, without any user action. Is it possible to do this in a query? Sigurd --
4
5297
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 111111;top;0.34 The different colums are divided by ";" and the sellingValue is a
1
3782
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 file, import the text file as fixed width text , and then run an update query to copy the appropriate info into fields of a different table. Is it possible to write a macro to do all these steps? Also, is it
1
2619
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. 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...
2
1836
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
5785
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
3166
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. my question is how can i import this textfile in to these 2 tables.and how i ensure that first 1000 values go to first table & the rest 250 in the next from each line of the text file. can be this be done via VB.NET or ASP? I would be grateful for...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10199
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9020
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—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.