Python Forum
python Extract sql data by combining below code.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python Extract sql data by combining below code.
#1
Hi Team,

How to combine below two code both code works, I want sql data using SCP Method.
SCP method is faster to extract data. SCP method does not have extract header option , hence I am using PYODBC Module header.



---------Extract header of sql table -----------into csv

import pyodbc
import csv
import os

connection = pyodbc.connect(
    'DRIVER={ODBC Driver 17 for SQL Server};SERVER=DESKTOP-GQK64O6;DATABASE=Customer;Trusted_Connection=yes;')
with connection.cursor() as crsr:
    qry = "Select * from employee"
    crsr.execute(qry)

    folderPath = "C:\\Users\\malle\\OneDrive\\Desktop\\C\\test_data\\output"
    header = next(zip(*crsr.description))
    print(header)

with open(fullpath, "a", newline="") as outfile:
            writer = csv.writer(outfile, delimiter="|", quoting=csv.QUOTE_NONNUMERIC)
		writer.writerows([header, row])


-----------Append sql data to csv without header---------------
[Python]
from pathlib import Path
import subprocess
 
source_table = "xxx_xxxx_xxx_POC.dbo.Table1"
destination = str(Path("E:\test\table1.csv"))
subprocess.run(["bcp", source_table, "out", destination,  "-ABCD12345678\\ABCD5678,10010","-c", '-t"|"', "-T" ])
Reply
#2
Hi Mob,

sorry , my mistake , it is not SCP , it should be bcp [Bulk Copy] from sql server.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,052 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  Why can't it extract the data from .txt well? Melcu54 3 678 Aug-20-2023, 10:07 PM
Last Post: deanhystad
  python extract mg24 1 952 Nov-02-2022, 06:30 PM
Last Post: Larz60+
  SQL Alchemy help to extract sql data into csv files mg24 1 1,778 Sep-30-2022, 04:43 PM
Last Post: Larz60+
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,268 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Build a matrix by pressing buttons of an interface in Tkinter which extract data from juandiegopulla 1 1,949 Sep-13-2021, 07:28 PM
Last Post: deanhystad
  Python Pandas: How do I extract all the >1000 data from a certain column? JaneTan 0 1,563 Jul-17-2021, 09:09 AM
Last Post: JaneTan
  Need help on extract dynamic table data Dr_Strange 0 2,491 Apr-30-2021, 07:03 AM
Last Post: Dr_Strange
  Python modules to extract data from a graph? bigmit37 5 22,411 Apr-09-2021, 02:15 PM
Last Post: TysonL
  Pandas Extract data from two dataframe nio74maz 1 2,184 Dec-26-2020, 09:52 PM
Last Post: nio74maz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020