ความคิดเห็น •

  • @quangvo4868
    @quangvo4868 3 ปีที่แล้ว +2

    Excellent video. I followed along and was able to connect and executed SQL. Thank you for sharing!

  • @juanignacio6479
    @juanignacio6479 2 ปีที่แล้ว

    This video was the most useful resource I found about pyodbc. Thank you!

  • @abu1479
    @abu1479 3 ปีที่แล้ว +2

    Thanks a lot for this video. It was very helpful in setting up the connection as I was struggling with it for a long time. You explained everything in an easy to understand manner. Thanks again!!

    • @SigmaCoding
      @SigmaCoding 3 ปีที่แล้ว

      Glad it helped!

    • @MrRitwikSarkar
      @MrRitwikSarkar 2 ปีที่แล้ว

      @@SigmaCoding How to connect with Amazon Aurora Mysql server using PyODBC and not SQLalchemy or PyMysql

  • @yashpatel9550
    @yashpatel9550 4 ปีที่แล้ว

    thanks brother.....you are amazing.....been trying to connect azure sql with django from while....finally its done....thanks

  • @goingfurther8092
    @goingfurther8092 3 ปีที่แล้ว

    Liked and subscribed. You are amazing at teaching.

  • @dparaguai
    @dparaguai 3 ปีที่แล้ว

    Thanks Buddy, great video !

  • @JackIsNotInTheBox
    @JackIsNotInTheBox 4 ปีที่แล้ว +5

    WOW AMAZING!! I learned so much, especially all the nice tips like showing all your drviers before connections.

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      Yeah, this library is excellent and gives you access to so many tools. The only challenge I've seen so far with it is the type of driver you need to work with. When you have both 64-bit and 32-bit applications, things can get wacky.

  • @foucault9978
    @foucault9978 3 ปีที่แล้ว +3

    Thank you so much. Excellent explanation and worked example :)

  • @kennethstephani692
    @kennethstephani692 3 ปีที่แล้ว +1

    Great video!!

  • @stackinglittlesats
    @stackinglittlesats 4 ปีที่แล้ว

    Very useful Sr. Thank you.

  • @ketanbutte3497
    @ketanbutte3497 ปีที่แล้ว

    finally. I could run this. Thanks alot

  • @attiqrahman1891
    @attiqrahman1891 5 ปีที่แล้ว

    Helped a lot thanks

  • @ignacioa3698
    @ignacioa3698 4 ปีที่แล้ว +1

    Awesome stuff !!!!!!!!

  • @DingsGuy
    @DingsGuy 4 ปีที่แล้ว +1

    Thanks for this

  • @jeffmiller9742
    @jeffmiller9742 3 ปีที่แล้ว

    You are amazing!!!!!!

  • @stockmarkettoday93
    @stockmarkettoday93 3 ปีที่แล้ว +1

    good one. Thanks !

    • @SigmaCoding
      @SigmaCoding 3 ปีที่แล้ว

      Glad you liked it!

  • @dairof7
    @dairof7 3 ปีที่แล้ว

    great, thanks a lot

  • @akhilvyas9908
    @akhilvyas9908 5 ปีที่แล้ว +3

    I’m trying this method to import data from a excel file with 15 columns in a sql table with same 15 columns. I’m getting an error “string index out of range” in the line values = (row[0], row[1].....row[14]). How to fix this?

  • @ayushkathpalia8982
    @ayushkathpalia8982 3 ปีที่แล้ว

    Hey , thanks a lot for this video.It helped me alot. Though i not able to retrieve an image saved in sql server as image format .I would really appreciate some help. Thanks . Good work Dude!

  • @vandro9370
    @vandro9370 ปีที่แล้ว +1

    Thanks.

  • @mrsprogrammer4549
    @mrsprogrammer4549 4 ปีที่แล้ว +1

    hey!! great video! just a quick question though, what if I have a large table in json and crazy amount of columns, any easier way that coding every column?

  • @eagletrots3592
    @eagletrots3592 4 ปีที่แล้ว

    Grt video and if you could create a video on exporting a data frame to sql server through python

  • @saraswatirai8826
    @saraswatirai8826 4 ปีที่แล้ว +1

    Hi, I am struggling with executing a MS sql job using PYOdbc. Can you please help on how to call sql job using pyodbc?

  • @prateeksharma-ig5qg
    @prateeksharma-ig5qg ปีที่แล้ว

    Hi, Thanks for the video,
    I created a access database to store value fetched from xpaths, i have to delete the stored values in my table to use the table freshly for the next run in a loop after storing the data in a excel source file. I used 'TRUNCATE TABLE table_name'', but its showing invalid syntax near Table.
    What can i do here? please help..

  • @pawlowski6132
    @pawlowski6132 4 ปีที่แล้ว

    Do we know how to authenticate using Azure AD vs Windows credentials (as per the video) when connecting to Azure SQL?

  • @Chris_87BC
    @Chris_87BC 3 ปีที่แล้ว

    if my data is not in a matrix (list with list elements) but in a dataframe, do I need to convert my pandas dataframe to a matrix like in your example in order to insert the data into a SQL Server table?

  • @JackIsNotInTheBox
    @JackIsNotInTheBox 4 ปีที่แล้ว +7

    Per the docs,
    "Connections are automatically closed when they are deleted (typically when they go out of scope) so you should not normally need to call [conn.close()], but you can explicitly close the connection if you wish.
    "
    and similarly for cursors:
    "Cursors are closed automatically when they are deleted (typically when they go out of scope), so calling [csr.close()] is not usually necessary."

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      If I understand correctly, what you're saying is that there is no need to close the connection? See, that's strange because on my work laptop, when I do anything related to our SQL database using PYODBC, I always have to close out the connection.
      If I don't, I have all these "sessions" that are still left open on the server. Now I may be doing something wrong on my end that's causing the session not to end, or maybe I'm misinterpreting what the server is telling me. However, it's strange that they say you don't need to close anything explicitly.

    • @devenshah3653
      @devenshah3653 2 ปีที่แล้ว +1

      @@SigmaCoding I always close connection. Like garbage collection may be automatic but better have a safe practice. BTW, great explanation.

  • @MatBat__
    @MatBat__ ปีที่แล้ว

    Great vid!
    Just a question, do you bother with rollback at all? Or does de odbc deal with it automaticly?

  • @ungeheurenungeziefer2662
    @ungeheurenungeziefer2662 3 ปีที่แล้ว

    Also if you have a pandas df not list,
    you need to use df.values.tolist()
    to convert to list, and than df = df[1::] to remove headers from list

  • @ApplySkills
    @ApplySkills 4 ปีที่แล้ว

    cnxn = pyodbc.connect()
    Can you explain why connect() is a method? From the camel casing, it looks like a function. Also within the module its also defined like a function. Not sure why its being instantiated like an object of a class. Thanks

  • @dmuhsin
    @dmuhsin 3 ปีที่แล้ว

    I am trying to run a merge query with Pandas and pyodbc and Im not sure how to go about doing that any help?

  • @fafza1
    @fafza1 4 ปีที่แล้ว

    Thanks for the video. It was super useful. Can you please help with adding a whole dataframe to SQL Server table? Table already setup in SQL server. I see some dataframe.To_SQL method but can't get it to work.

  • @irmdev595
    @irmdev595 2 ปีที่แล้ว

    i have a python dash app deployed on Azure apps. When it accesses SQL Server it leaves the connection open.
    Yes i explicitly closed, shut of connection pools as well

  • @user-ym5tk7sm7j
    @user-ym5tk7sm7j 8 หลายเดือนก่อน

    Thanks for the video. I followed the same exact steps and got an "invalid escape sequence \s" on the line where you set the server variable. how are you not getting that same error?

  • @bhupendersingh6655
    @bhupendersingh6655 ปีที่แล้ว

    Hi, the data fetched from mssql are coming with (") and ' and , included how to avoid it

  • @DerDudeHH
    @DerDudeHH 3 ปีที่แล้ว

    thanks for the video
    But when I have a dataframe with a lot of columns, it's not pleasant to use this method to insert. It would take hours...

  • @JoshuaDHarvey
    @JoshuaDHarvey 4 ปีที่แล้ว

    Great video, thank you. Was hoping to hear more about how/why the 'for row in cursor' works given the cursor object returns something along the lines of so not totally clear why or how the for loop is able to access the rows just b/c the cur.execute(select *) statement is included. printing cursor variable even after the cur.execute is performed doesn't return a single line with the array.

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      Well keep in mind that it's not technically an array in python. It's a Cursor object, and that cursor object just so happens to have a mechanism that allows you to iterate through it. My guess is there is probably a "__getitem__" method which allows you to loop through the returned values.
      I know this might sound confusing, but the giveaway is the fact you're seeing a "Cursor" object. That to me says it's not a list, so it could have its own "__repr__" method and stuff like that.

  • @shakeelahmed8624
    @shakeelahmed8624 ปีที่แล้ว

    How to check if before insertion if record already exists to avoid duplication ?

  • @aartigupta6033
    @aartigupta6033 4 ปีที่แล้ว

    Hi I am getting error installation of pyodbc how can I solve this error it requires c++ compiler

  • @AhmedHassan-jo1il
    @AhmedHassan-jo1il 4 ปีที่แล้ว

    Hello, quick question here
    I'm making a gui where the user enters the server name and then I'll list all databases etc
    how to I config the connection like
    mydb = pyodbc.connect(server="";)
    and then in a function I'll get the input from the user and configure the connection to that server like
    mydb.config(server=userinput.get())
    seems that pyodbc has no attribute config so what can I use instead

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      I'm a little confused about what you're trying to do. Are you trying to ask for the Server name and then use that information to query all the databases in that server to then populate that in the GUI? If that is the case, here is the connection string and query.
      # Connection String.
      cnxn = pyodbc.connect(driver = 'Driver', server='server', database='master', trusted_connection='yes')
      cursor = cnxn.cursor()
      cursor.execute("SELECT name FROM sys.databases")

  • @blindhydra
    @blindhydra 2 ปีที่แล้ว

    Hey thanks for the video! I am running into this issue:
    return pyodbc.connect(
    pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
    I dont quite understand what I should fix based of of this error.

  • @nirajshah7170
    @nirajshah7170 4 ปีที่แล้ว +1

    Very nice and detail explanation! Appreciating efforts! What if it is SQL authentication credentials require instead of windows authentication?

    • @nirajshah7170
      @nirajshah7170 4 ปีที่แล้ว

      Additional question, query output is not displaying column names. I want to export the query output in pandas dataframe. Do I need to insert the columns names in pd manually, or there is a way to populate column names with query result ?

  • @pyrofred11691
    @pyrofred11691 3 ปีที่แล้ว

    I keep getting invalid object name, I ran the query in the microsoft sql manager no problem, but in python I get the error.

  • @biswasstar
    @biswasstar 5 ปีที่แล้ว +2

    I have 8 columns and while i pass all the values , it gives me error: "The SQL contains 0 parameter markers, but 8 parameters were supplied', 'HY000".. Please help..

    • @SigmaCoding
      @SigmaCoding 5 ปีที่แล้ว

      Do you have the code?

  • @kerlanandrew8888
    @kerlanandrew8888 4 ปีที่แล้ว +1

    How can I connect to SQL and read a file that contains SQL Insert statement and write the same to tables already existing in SQL.

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      In essence, all you would do is read the file and then execute the query inside of it. However, be careful because it will need to be formatted a certain way sometimes. For example, if you want to just read they file and store the "query string" in a variable it would look something like this:
      sql_file = "my_sql_query.sql"
      sql_query = open(sql_file, mode='r', encoding='utf-8-sig').read()
      print(sql_query)
      Also note the encoding, you may or may not need this depending on how the file is, but for safety I usually put it in there.

  • @user-bl2km6pl4v
    @user-bl2km6pl4v 7 หลายเดือนก่อน

    I installed the pyodbc on a MAC M1 but I can not see any drivers in for loop:
    import pyodbc
    for driver in pyodbc.drivers():
    print(driver))
    any idea?

  • @jnovick69
    @jnovick69 3 ปีที่แล้ว +1

    When I run the print(drivers) section it does not return anything, not even an error. Does this mean I do not have any drivers installed?

    • @SigmaCoding
      @SigmaCoding 3 ปีที่แล้ว

      Potentially, what version of PYODBC did you install the 32 bit version or 64 bit version?

  • @keving3660
    @keving3660 3 ปีที่แล้ว

    if i want to insert data from api, how do i do that? thank you

  • @ignacioa3698
    @ignacioa3698 4 ปีที่แล้ว +1

    So you notated the server name here for your server in your example.
    However, what’s the alternative I’f say you’re using SQLite which is a serverless, and pretty much SQLite dumps into simple file copies? How you pass it through and connect to SQLite?

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      So keep in mind I've never actually connected to an SQLite database using PYODBC but supposedly you can from reading the documentation. Here is how the code will look:
      cnxn = pyodbc.connect("Driver=SQLite3 ODBC Driver;Database=sqlite.db")
      Here is the link to where I got the code from github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQLite
      He does mention you may not even need the driver at all. However, he doesn't give an example as to how the code will change if you don't reference the driver.

  • @businessdevelopmentstrategies
    @businessdevelopmentstrategies 2 ปีที่แล้ว

    After connecting how to make it as a dataframe.
    Please tell me

  • @yeryungkoo2505
    @yeryungkoo2505 4 ปีที่แล้ว

    Hi, Thanks for the video. I am now trying to set up the pyodbc and make connection with SQL server on MACos. I already installed odbc driver but when I ran 'pyodbc.drivers()' on jupyter nootbook, it returned empty list []. Do you know how to solve this problem?

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว +1

      For macOS the PYODBC library is very different compared to windows. I think you can still connect to a SQL Server instance but I don't think you can specify any driver because macOS doesn't install any.
      You might want to look at this: github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX

  • @suryagaur7440
    @suryagaur7440 5 ปีที่แล้ว +1

    what if we have 100 columns do we have to write all columns name in insert into Statement.

    • @SigmaCoding
      @SigmaCoding 5 ปีที่แล้ว +1

      Technically you don't have to specify the column names in a insert query, you just then have to make sure that the info is in the correct order when you insert it.

  • @iosifabramov254
    @iosifabramov254 3 ปีที่แล้ว

    Hello friend. I am getting error after finish coding - pyodbc.ProgrammingError: ("A TVP's rows must be Sequence objects.", 'HY000'). And using pycharm app. Could you help me please?

  • @mahesht4870
    @mahesht4870 ปีที่แล้ว

    Great Video!. Can you please clarify, how windows authentication will work when we connect to SQL server hosted in Azure VM.

  • @tavijagan
    @tavijagan 4 ปีที่แล้ว +1

    Excellent tutorial!! I don't see the excel drivers within pyodbc. How do I install Excel or any other missing pyodbc drivers?

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      It can be found here: www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=13255

  • @aartigupta6033
    @aartigupta6033 4 ปีที่แล้ว

    Which version of visual studio c++ compiler required and what is prerequisite of this code run

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      I honestly couldn't tell you at this point I made this video a while ago so I don't remember. What error are you getting though?

  • @lickylizard6639
    @lickylizard6639 4 ปีที่แล้ว +1

    Hey man awesome video. im just having trouble actually using pyodbc. i did eveything like you did. but it doesnt see the attributes of the pyodbc. Any suggestions?

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      What do you get instead or is it just returning an error?

  • @dheerajmenon6978
    @dheerajmenon6978 3 ปีที่แล้ว +1

    How to pass Null as parameter. To fetch columns which have null values?

    • @SigmaCoding
      @SigmaCoding 3 ปีที่แล้ว

      A NULL value is just None inside of Python. So if you want the value to be NULL then just pass through None.

  • @matheusfeitoza5526
    @matheusfeitoza5526 4 ปีที่แล้ว

    Hey, how can I get the PK id after insertion data? sql = "INSERT INTO FORM SELECT " + str(form.FK_Customer) + " , '" + form.Description_Form + "' , 1019,getdate() SELECT @@IDENTITY AS ID"
    row = cursor.execute(sql)
    for r in row:
    print(r)
    cursor.commit()
    I am trying to use the code above, but it is not working

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      What's the error you're getting? Also, is it the insert query that's not working or the select query? I can't tell if they're two separate queries that you run independently or together.
      Also, be careful using a GetDate() function in the query. What exactly is that doing, and you verified that the table can accept the date data type, right? Dates can cause all sorts of messes if not used correctly. Like the use of the "@@Identity" system function.

  • @Mahmoud-ys1kt
    @Mahmoud-ys1kt ปีที่แล้ว

    first i would like to thank you
    but i am having some simple error after defining connection string
    ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

  • @heatheralbrecht481
    @heatheralbrecht481 4 ปีที่แล้ว

    Thank you for the video! Do you have a video showing how to load a whole dataframe into a table rather than row by row?

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      Not yet, and PYODBC is weird with this topic because even if you use the "executemany()" method, like they specify in the documentation. What you'll find is it still inserts it row by row. The work around I do is to create a string where I do a bulk insert of tuples, to "bypass" it.

    • @mirzaputra9766
      @mirzaputra9766 3 ปีที่แล้ว

      use python pandas

  • @anjofamini5697
    @anjofamini5697 4 ปีที่แล้ว +1

    hello, I followed and tried your tutorial but im getting and error in the part of assigning data to values. the error is string index out of range.
    let me know your thoughts. thanks

    • @anjofamini5697
      @anjofamini5697 4 ปีที่แล้ว

      btw, may data source is a dataframe

    • @omardagdoug6409
      @omardagdoug6409 4 ปีที่แล้ว

      @@anjofamini5697 Hi Anji. did y solve the problem please ? thanks

    • @anjofamini5697
      @anjofamini5697 4 ปีที่แล้ว

      ​@@omardagdoug6409 ​ , yep, but I used other kind approach. using sqlalchemy

    • @omardagdoug6409
      @omardagdoug6409 4 ปีที่แล้ว

      @@anjofamini5697 Yes I've seen sqlachemy and to_sql approach. My problem is that I have to use SQL server as DB, and I couldn't fix the out of range problem.
      Thanks Bro for replying ;))

  • @gatzsandrofuertesosorio1140
    @gatzsandrofuertesosorio1140 3 ปีที่แล้ว +1

    Thanks man, I had a mistake when I tried to put the ip in the server parameter. I have a doubt, which is better SQL Server Native Client 11.0 or ODBC Driver 17 for SQL Server?

    • @SigmaCoding
      @SigmaCoding 3 ปีที่แล้ว

      It depends, for me I seem to have less issues with the SQL Server Native Client and that seems to work more broadly across different systems.

    • @gatzsandrofuertesosorio1140
      @gatzsandrofuertesosorio1140 3 ปีที่แล้ว

      @@SigmaCoding Thanks man. And in the case of ip? is it possible to put the ip in the variable server?

    • @anuragrrajan8541
      @anuragrrajan8541 3 ปีที่แล้ว

      @@SigmaCoding Is SQL Server Native Client 11.0 free or licensed?

  • @chandanroy87
    @chandanroy87 3 ปีที่แล้ว

    Awesome Video !
    Question ?
    plttrackingconn = pyodbc.connect("Driver={SQL Server};"
    "Server=10.98.97.101;"
    "Database=Tracking;"
    "Trusted_Connection=yes;"
    "ApplicationIntent=ReadOnly;")
    'Tracking' is in an availability group and is currently accessible for connections when the application intent is set to read only.
    Not working for me

  • @TradesMax
    @TradesMax ปีที่แล้ว

    this method INSERT very slowly, can you create a video on how to do fast_executemany

  • @TheKazanovic
    @TheKazanovic 4 ปีที่แล้ว +1

    Is it possible use SQL database Without Installing Microsoft SQL Server Management Studio using python? For example i must install MSQL SS, create server, make database, create tables... Can i do that without install MSQL SS and using only python?

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      I think what you're talking about is an in-memory database, something similar to what SQL Alchemy offers. I don't think PYODBC offers such functionality,.

    • @TheKazanovic
      @TheKazanovic 4 ปีที่แล้ว

      @@SigmaCoding Thanks, I'll try with SQL Alchemy

  • @MrRitwikSarkar
    @MrRitwikSarkar 2 ปีที่แล้ว

    How to connect with Amazon Aurora Mysql server using PyODBC and not SQLalchemy or PyMysql

  • @nikhilpunde6410
    @nikhilpunde6410 4 ปีที่แล้ว +2

    Hi @SigmaCoding , I'm trying to connect to Microsoft SQL production database using pyodbc , but I'm getting an error "[DBNETLIB]SSL Security error (18) (SQLDriverConnect) ][DBNETLIB]ConnectionOpen (SECDoClientHandshake())" ,I tried searching it everywhere but I couldn't find the useful. While connecting to Microsoft SQL Testing database , I'm easily connecting to it and getting the required output in my Dataframe, please help......................

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว +1

      Hi Nikhil, are you using window's authentication to login or are you providing a username and password? Also is this a local server?

    • @nikhilpunde6410
      @nikhilpunde6410 4 ปีที่แล้ว

      @@SigmaCoding windows authentication and yes on local

    • @nikhilpunde6410
      @nikhilpunde6410 4 ปีที่แล้ว +1

      I tried many things bro , but still not able to do so, I will email you the solutions I tried . Please help me to solve this , I had almost written 1000 lines of code for my automation use case using pandas ,it starts with connecting to testing DB and then fetching 5 input tables and using them for further operation . So as of now for production , exporting DB data to excel and then reading this using pandas and continuing my project.

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว +1

      @@nikhilpunde6410 Alright, just email me at coding.sigma@gmail.com

    • @nikhilpunde6410
      @nikhilpunde6410 4 ปีที่แล้ว

      @@SigmaCoding sent you sir

  • @SeeCoolThings
    @SeeCoolThings 5 ปีที่แล้ว +2

    Hi. Just wondering, why do you choose pyodbc over pypyodbc?

    • @SigmaCoding
      @SigmaCoding 5 ปีที่แล้ว +1

      No in particular reason, it was just the first library I was exposed to at my job that interacted with ODBC. I've heard of pypyobdc and have heard good things about it, it's basically pyodbc just in pure python.

    • @SeeCoolThings
      @SeeCoolThings 5 ปีที่แล้ว

      @@SigmaCoding Sorry a random question out of nowhere, do you live in Taiwan by any chance?

    • @SigmaCoding
      @SigmaCoding 5 ปีที่แล้ว

      @@SeeCoolThings I do not, I am currently living in San Diego.

  • @gokhanucar213
    @gokhanucar213 3 ปีที่แล้ว

    cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server}; \
    SERVER=' + server + '; \
    DATABASE=' + database +';\
    Trusted_Connection=yes;')
    Im getting this error for my connection string above:
    File "", line 20
    SERVER=' + server + '; \

    ^
    SyntaxError: EOL while scanning string literal

  • @nagarajumuthyala5798
    @nagarajumuthyala5798 ปีที่แล้ว

    Thanks a lot bro.. i wasted one full day just because of insert query ....

  • @burhanumar1242
    @burhanumar1242 4 ปีที่แล้ว

    Can you please tell how to import python pyodbc library in Jupyter Notebook!

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      It's just import pyodbc. That assumes you have it installed though.

  • @JonathanJournal
    @JonathanJournal 4 ปีที่แล้ว +1

    how to deal with csv, for example your sales_list is an csv file, and insert all row?

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      Are you talking about doing a bulk insert? Meaning you insert multiple rows at once?

    • @JonathanJournal
      @JonathanJournal 4 ปีที่แล้ว +1

      @@SigmaCoding found answer, you helped. now another problem is I run this everyday. But only want to save 2 days record. Meaning when i run at tomorrow, i need to delete record from yesterday. You could shed some light on this? thanks

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      @@JonathanJournal Lot's of ways to approach this, but the most simple one is that when you insert your data, you add a timestamp for when added. What this will do is provide a way to see what data was entered when. All you would do is every time you startup this script is have a "DELETE" query that will delete records that match the previous day's date. That means you'll need to add a "WHERE" condition that will filter the records to be removed. Just keep in mind, once it's deleted, it's deleted; there is no going back. In other words, make sure you really want to delete historical data.

    • @JonathanJournal
      @JonathanJournal 4 ปีที่แล้ว

      @@SigmaCoding how do you deal with dataframe, and write to microsoft sql server?

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว +1

      @@JonathanJournal If you want to write a Pandas data frame to SQL Server, there are a few ways to achieve this. However, will you be writing to an existing table, or will this data frame create a new SQL table?
      1. Use SQL Alchemy.
      In this library, you can create you use PYODBC in tandem with sqlalchemy to establish a connection. Then you use the Pandas data frame "to_sql" method to write the data frame to the table. Here is an example that will create a new table and show the difference between appending and replacing:
      # import the libraries
      import sqlalchemy
      import pyodbc
      # create a connection, might be different if you're using a trusted connection.
      engine = sqlalchemy.create_engine("mssql+pyodbc://:@")
      # Insert data to a new table
      df.to_sql("my_new_table", engine)
      # Append data to an existing table.
      df.to_sql("my_existing_table", engine, if_exists = 'append')
      # Replace table and then insert data.
      df.to_sql("my_exisiting_table", engine, if_exists = 'replace')
      2. Loop through the data frame and insert directly:
      In this case, you create a connection as you do in any of the examples, but then we will specify an insert statement that we will execute for each row in the data frame. Imagine I had a data frame with 3 columns first_name, last_name, and age.
      # here's my data frame
      my_df = pd.read_excel("my_excel.xlsx")
      # loop through each row, in my data frame.
      for index, row in df.iterrows():
      cursor.execute("INSERT INTO dbo.my_table ([first_name], [last_name], [age]) VALUES (?, ?, ?)", (row['first_name'], row[last_name'], row['age'])
      # commit the insert
      conn.commit()
      # close the cursor
      cursor.close()
      # close the connection
      conn.close()
      3. Do a bulk insert
      To do a bulk insert, you would have to convert the data frame to a numpy recordset. However, you must be very careful with this as numpy has datatypes that SQL server can't handle, so you'll need to make sure that recordset has the proper data types. I've only done this once, and I can't find that example I did, but it is possible. You just have to do some planning.
      If you need help during the process feel free to reach out through email at coding.sigma@gmail.com.

  • @edgarvargas7162
    @edgarvargas7162 4 ปีที่แล้ว +1

    Hi, maybe you have an example using UID (username) and PWD (password)? I'm using ODBC Driver 13 for SQL Server and can't pass DOMAIN\user because I have the error:
    Error: ('28000', "[28000] [unixODBC][Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'DOMAin\\username'. (18456) (SQLDriverConnect)")
    the main issue is: backslash is not possible to pass to connection string?
    this' my connection:
    cnxn= pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+user+';PWD='+ password)
    I'd really appreciated your comments
    Regards,

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      I would recommend you do the following:
      # to fix the double backslash wrap it in r'', meaning the raw string.
      my_server = r"ServerName"
      # declare the rest of your variables
      my_database = "DatabaseName"
      my_userid = "UserID"
      my_password = "Password"
      # connect to the server, using the key word alternative approach.
      connSqlServer = pyodbc.connect(driver='{ODBC Driver 13 for SQL Server}',
      server=my_server,
      database=my_database,
      uid=my_userid,
      pwd=my_password)
      Let me know if that fixes the issue for you.

  • @ebinjose1015
    @ebinjose1015 5 ปีที่แล้ว +1

    How to select a table with it's column name in python ?

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      I'm a little confused by the question. Are you asking how to select a specific column from the table?

    • @ebinjose1015
      @ebinjose1015 4 ปีที่แล้ว +1

      @@SigmaCoding yes, because I am able to see the dataset without column names.

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      @@ebinjose1015 All you would do is just change the query you execute.
      For example, this would change from this.
      *cursor.execute('SELECT * FROM td_price_data')*
      To this.
      *cursor.execute('SELECT Date FROM td_price_data')*
      You're just changing the SQL query that's all.

    • @bryantTheFatBadger
      @bryantTheFatBadger 4 ปีที่แล้ว +1

      @@SigmaCoding I think he was asking how to return the column headers in the result set, so the first row would be all the column headers/names, and then each subsequent row would be the result set.

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      @@bryantTheFatBadger Gotcha in that case to get all the column names you would want to write the following for the cursor:
      columns = [column[0] for column in cursor.description]
      This would create a list of column names for you. After that, the code would be the same.

  • @jovischuah4031
    @jovischuah4031 4 ปีที่แล้ว +1

    hi , is anybody here know how do code prevent duplicated data?

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      Question, when you setup the table in the database did you specify an identity column? One with a primary key that won't allow for duplicates? Or are you asking how would you do that using PYODBC?

    • @jovischuah4031
      @jovischuah4031 4 ปีที่แล้ว +1

      @@SigmaCoding my primary key is not unique, it is duplicated in one table.example, many data are under one series number. Therefore, i dont know how to code prevent duplicated data when new entry coming in with using PYODBC?

    • @jovischuah4031
      @jovischuah4031 4 ปีที่แล้ว +1

      one more question is how to speed up insert query(10 thousand rows++)?could you give me some hint?

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      @@jovischuah4031 Okay, so you'll need to devise a way for generating a unique primary key each time the data is added. Are there any columns that you can use that can serve as the primary key? Like a DateTime or something like that?
      Also, to insert multiple rows at once in a "quick" fashion, you need to set an attribute first. You need to add the following line:
      cursor.fast_executemany = True
      and then call the following:
      cursor.executemany(insert_query, values)
      where the insert query is the query string you define in your code, and the values are the values you wish to insert.

    • @SigmaCoding
      @SigmaCoding 4 ปีที่แล้ว

      @@jovischuah4031 You'll need to create a unique key somehow then. You can generate one or combine multiple columns so you can create one. The end goal is just to create one.

  • @yasirmalik198
    @yasirmalik198 ปีที่แล้ว

    my pyodbc drivers is empty
    Please help

  • @afzalalizatchi3380
    @afzalalizatchi3380 4 ปีที่แล้ว

    i think it's Oracle Data Base Connectivity