Import Records From CSV File (or any data file) to SQL Server (or any database system) With Python

Sdílet
Vložit
  • čas přidán 19. 07. 2024
  • In this Python tutorial, we are going to learn how to import records from a CSV file to Microsoft SQL Server.
    The script can also be adapted to import dataset from an Excel file, a text file, or any data file as we will be reading the dataset into a pandas dataframe first. And the database system is not limited to just SQL Server, it can be MySQL, Oracle, or any other database systems. I am using SQL Server because this is the database system I know the best.
    📎 Source Code: learndataanalysis.org/import-...
    📺 How To Build A MS SQL Server SQL Query Tool Using Excel: • Building A MS SQL Serv...
    Buy Me a Coffee? Your support is much appreciated!
    ----------------------------------------------------------------------------------------------------------------
    PayPal Me: www.paypal.me/jiejenn/5
    Venmo: @Jie-Jenn
    Patreon: / jiejenn
    Check out Kit →
    www.kite.com/get-kite/?...
    Dataset Used: data.austintexas.gov/Transpor...
    Timestamp:
    00:00 - Tutorial info
    00:45 - Data source used in this video
    02:15 - Table Creation SQL Statement
    02:35 - Writing automation script in Python
    03:20 - How to find out what drivers are available on your PC
    03:45 - Get SQL Server Server Name
    04:45 - Import data to DataFrame
    07:45 - Data clean up
    14:52 - Convert DataFrame object to List object
    16:00 - Create connection string
    19:45 - Create connection object
    22:00 - Create cursor object
    25:35 - Run the script for testing.
    Support my channel so I can continue making free contents
    ----------------------------------------------------------------------------------------------------------------
    By shopping on Amazon → amzn.to/2JkGeMD
    More tutorial videos on my website → LearnDataAnalysis.org
    Follow Me on Facebook → / learn-data-analysis-10...
    Business Inquiring: CZcams@LearnDataAnalysis.org
    Tags:
    #SQLServer #Python

Komentáře • 58

  • @sauravkumar9454
    @sauravkumar9454 Před 3 lety +1

    Great tutorial mate. just had difficulty in understanding few words but the subtitle made sure I have none. Thanks a lot.

  • @DavidRodrigues-bj6wv
    @DavidRodrigues-bj6wv Před 3 lety

    Excellent tutorial. Thank you for sharing!

  • @fernandoflores3728
    @fernandoflores3728 Před 3 lety

    Thanks a lot, great tutorial!

  • @Krazy_Shorts
    @Krazy_Shorts Před rokem

    Great vid! Thanks...This will help me big time!

  • @yusliandi642
    @yusliandi642 Před 3 lety

    Thank for this

  • @sauravshrestha1890
    @sauravshrestha1890 Před 2 lety +2

    For Step 3.3, I usually use following syntax, instead of using '?' for each columns. It will be helpful for larger data with huge number of columns.
    VALUES ({','.join(['?']*len(.columns))})
    This will count the number of columns and gives you back the required '?' as shown in the example.

    • @jiejenn
      @jiejenn  Před 2 lety +2

      Great tip, thanks for sharing.

    • @sauravshrestha1890
      @sauravshrestha1890 Před 2 lety +2

      @@jiejenn my pleasure. I recently had to deal with multiple tables containing atleast 60 columns. Typing 60 to 70 ‘?’ Was confusing, untidy and higher chances of mistakes. Glad I found this trick somewhere in youtube as well 😃 happy to share here.
      Thank you for great video

  • @LoveisHell85
    @LoveisHell85 Před 3 lety

    Thanks for this

  • @goanywhere7693
    @goanywhere7693 Před 3 lety

    Hello Jie, I need to parallel ingest a CSV file with half million records for a assignment. Should i chunk the file and then follow this video? or do you have some other suggestion. Thanks.

  • @cvillejin
    @cvillejin Před 2 lety

    Hey Jie - do you have any solutions for "TypeError: tuple indices must be integers or slices"? - This guide worked for most of my .csv files, but for one particular file, I'm getting the tuples error.
    edit: files

  • @ruthfussee5503
    @ruthfussee5503 Před 2 lety

    Do you have a tutorial where we do something like this, but update if there is a primary key exist or add in the entire row if it doesnt?

  • @nishantm1924
    @nishantm1924 Před 3 lety

    how can you import csv file to an already existing table using sql query?
    like suppose you have Customer table and now you want to add data from xyz.csv file and both Customer and csv file have same attribute ,then how to do such task?

  • @K-Von
    @K-Von Před 3 lety

    Thanks for the video! Could you help me with a question, how could I restrict the number of insertions given a number, instead of inserting all the rows?

    • @erenhan
      @erenhan Před 2 lety +1

      you can iloc or loc and store it another dataframe and send via to_sql

  • @Arunkumar-ky9cd
    @Arunkumar-ky9cd Před 3 lety

    Hi Jie,
    I got the Error as GETDATA' is not a recognized built-in function name, Any Suggestion?

  • @wingscofant
    @wingscofant Před 3 lety

    Hi man you're awesome, can you help me sending code for inserting/uploading csv and images to database(SQL Server/Wamp Server)

  • @tekindogan9164
    @tekindogan9164 Před 3 lety

    👍👍👍

  • @boogeyman9824
    @boogeyman9824 Před 3 lety

    Getting pypyodbc .error data source name not found and no default driver specified pls help me in this 🙏

  • @Boswortel
    @Boswortel Před 2 lety

    Very nice tutorial but i am wondering if this is the only/best way to import a large amount of data. In bash i always use mysqlimport but does python also have something like this?

    • @jiejenn
      @jiejenn  Před 2 lety

      It's really depending on the system. I came from MS SQL Server, so usually for bulk upload, I would start with SSIS. For anything else required data exchange with 3rd-party system, that's when I would go with scripting route. Executemany() method despite is efficiently when it comes to memory allocation, it is still not a true bulk insert.

  • @maikolvargas7149
    @maikolvargas7149 Před rokem +1

    if you struggle with the blank values in your files use this
    df_data = df[columns]
    newdf = df.fillna(' ')
    records = newdf.values.tolist()

  • @ozzyit7512
    @ozzyit7512 Před 2 lety

    Very helpful.. But what if i have to handle with multiple sheets in an exce(csv) file can you tell me what addition i need to do in this code

    • @jiejenn
      @jiejenn  Před 2 lety +1

      The workflow is a bit different. I will cover that topic in a separate video.

  • @wilsonusman
    @wilsonusman Před 3 lety

    Hi Jie, thanks for making this video. I was wondering if you could implement something like tqdm library into this same script. That would be very helpful specially if you're trying to load millions of records.

    • @jiejenn
      @jiejenn  Před 3 lety

      Hi Wilson. Never used tqdm library before, so don't know if I will be able to help.

  • @MichelUNITED
    @MichelUNITED Před 3 lety +1

    @Jie jenn. i need your help and it is urgent. I need to know how i can create A SCRIPT to populate a database. The tools i have to do this are python interpreter(visual studio code), Mysql workbench and xampp. Please help meeeee.do i need to install any ODBC DRIVER to start with?

  • @muhammadosama5666
    @muhammadosama5666 Před 2 lety

    Great video thanks..But i have a question how can i import multiple sheets from excel document?

    • @jiejenn
      @jiejenn  Před 2 lety

      I will be covering that topic in a separate video.

    • @muhammadosama5666
      @muhammadosama5666 Před 2 lety

      @@jiejenn Ok i'll be waiting for that video
      Thankyou

  • @ririraman7
    @ririraman7 Před 2 lety

    can we use this python script to append data in same table of SQL if new files arrive?

    • @jiejenn
      @jiejenn  Před 2 lety

      Insert into appends records to the same table you specified.

    • @ririraman7
      @ririraman7 Před 2 lety

      @@jiejenn Yes correct, I did today and it worked! Thank you very much brother! you are saving lives!

  • @nghiatran1509
    @nghiatran1509 Před 2 lety

    Can you import database in XBRL to Microsoft Access ?

    • @jiejenn
      @jiejenn  Před 2 lety

      Don't have experience with XBRL, so I am not sure to be honest.

  • @MichelUNITED
    @MichelUNITED Před 3 lety

    please i don't have a SQL Server but i have Xampp connected to Mysql workbench. i know i can run the script on MYsql workbench. i don't understand the ODBC FILE you imported on visual studios. Also the connection is not clear to the data base is not clear.

  • @adilsonpinto2752
    @adilsonpinto2752 Před rokem

    FileNotFoundError: [Errno 2] No such file or directory: 'Real-Time_Traffic_Incident_Reports.csv' which is?

    • @jiejenn
      @jiejenn  Před rokem

      Perhaps this will help www.google.com/search?client=firefox-b-1-d&q=python+FileNotFoundError

  • @ozzyit7512
    @ozzyit7512 Před 2 lety

    Bro when i'm trying to import multiple sheets from an excel file its not working !
    Can you please make a tutorial on how to import multiple sheets, any help would very helpfull

  • @akhilseeramsetty9125
    @akhilseeramsetty9125 Před 2 lety

    PERFECT SYNTAX FOR USERNAME AND PASSWORD PLEASE
    IN CONN_STRING

  • @aianIII
    @aianIII Před rokem

    I followed your code and tried to generate the data in SQL Server by running the SELECT script. However, when I ran the SELECT script again, the data was gone.
    It appears that the data is not being stored in the database. Could you please help me?

    • @jiejenn
      @jiejenn  Před rokem

      The script doesn't generate any data, it merely import a data file into your SQL Server database.

    • @aianIII
      @aianIII Před rokem

      @@jiejenn I'm sorry if I didn't explain it clearly. I used a SELECT script to query the data in MS SQL, and it was successful. However, when I executed the same script for the second time, the queried data disappeared.

    • @jiejenn
      @jiejenn  Před rokem

      @@aianIII Then I don't know if I can help without looking at your script. Perhaps there might a typo or two in your code somewhere.

  • @cvillejin
    @cvillejin Před 2 lety

    Hi Jie - When I try to run this code, I get an error "Database Error:
    [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied" how can I fix this?

    • @jiejenn
      @jiejenn  Před 2 lety +1

      It is difficult to tell based on a very general error message. I would suggest you post your question on Stack Overflow.

    • @cvillejin
      @cvillejin Před 2 lety +1

      @@jiejenn no problem. I was able to combine what I learned in your video with some other guides and it is now fully working! Thanks again for all your great videos!

    • @yannantso4652
      @yannantso4652 Před 2 lety

      @@cvillejin I got the same problem, coud you tell how does you have solved your problem ? thx

    • @cvillejin
      @cvillejin Před 2 lety +1

      @@yannantso4652 try changing the driver to the version of your sql server

  • @adilsonpinto2752
    @adilsonpinto2752 Před rokem

    Step 3 It is giving the following error: NameError: name 'cursor' is not defined

    • @jiejenn
      @jiejenn  Před rokem

      Makes sure cursor is created first.