0
import pyodbc
import numpy
import pandas as pd
import pypyodbc

def sql_conn():
    conn = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};"
                          "Server=;"
                          "Database=db_name;"
                          "uid=xxxx;pwd=xxxx;")
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM dbo.ImageDB')
    for row in cursor:
        print(row)

It's running on a Windows Server 2012 R2.

Whenever I run the python script I keep getting the message

Process finished with exit code 0

I know the connection wasn't made. How do I get the server name? Is it a combination of the server IP and the SQL Server name? Do I need to provide a PORT number? I tried a bunch of combinations for the server name but all give me the same output.

Also which is better pyodbc or pypyodbc?

I am sorry if this sounds like a stupid question but I am really new to this and any help would be appreciated.

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bittu
  • 79
  • 1
  • 8
  • Take into account that you are not actually invoking ``sql_conn()`` function. Append at the end of the script ``sql_conn()`` – Tomás Denis Reyes Sánchez Mar 26 '20 at 21:41
  • I forgot to copy that. It still gives me the same output :( – Bittu Mar 26 '20 at 21:46
  • From where are you trying to connect? – Tomás Denis Reyes Sánchez Mar 26 '20 at 21:53
  • From my Windows laptop. I am using Pycharm to run the python script. – Bittu Mar 26 '20 at 22:06
  • I haven't worked directly with lightsail but would recommend you connecting from a client first to see if it works. Then Look at this link https://stackoverflow.com/questions/16024956/connecting-to-sql-server-with-pypyodbc and this one https://lightsail.aws.amazon.com/ls/docs/en_us/articles/amazon-lightsail-connecting-to-your-mysql-database for getting the details of your database. Even though it's not sql server it may help you determine your server URI. Take into account inbound/outbound rules of the security group where the instance is running. – Tomás Denis Reyes Sánchez Mar 26 '20 at 22:53
  • Just to be clear, when you say "connecting from a client", do you mean that I should ensure the database is up and active via the RDP interface provided by AWS? – Bittu Mar 27 '20 at 00:57

0 Answers0