Error connecting to sql server using python

I recently installed Microsoft SQL Server 2014 on my PC, because I want to create a database for the web application that I have been creating (I studied Python for a year and had very simple experience with SQLite).

After installing SQL Server 2014 and creating a database called Users, I just try to run some very simple commands in my database, but again and again I run into the first hurdle!

I installed pymssql and pyodbc and tried to run the commands directly with them, but could not. (for example, pymssql gives me a TypeError argument: the type "NoneType" is not iterable when I set the variable conn = pymssql.connect (server, user, password, "tempdb")

My last attempt was to use SQLalchemy to achieve my long awaited connection to the SQL database. However, after setting this parameter, it does not work with the following error: "sqlalchemy.exc.OperationalError: (pymssql.OperationalError) (20009," DB-Lib 20009 error message, severity 9: \ nUnable for connection: Adaptive Server unavailable or does not exist \ nNet-Lib error during unknown error (10035) \ n ') "

The question I need is how do I start a conversation with my database using SQLalchemy?

The code I use is as follows: from sqlalchemy * import

    engine = create_engine('mssql+pymssql://Han & Lew:@SlugarPlum:1433/Users')

    m = MetaData()

    t = Table('t', m,
            Column('id', Integer, primary_key=True),
            Column('x', Integer))

    m.create_all(engine)

, SlugarPlum. . THELROYSERVER. DSN = 1433. . ( , , , , , , , .)

, - - Python-SQL, , , !

+4
1

pyodbc. pymssql , pymssql. Windows Linux, Linux. , .

def sqlalchemy_connect(connect_string): 
    """ Connect to the database via ODBC, start SQL Alchemy engine. """

    def connect():
        return pyodbc.connect(connect_string, autocommit=True)

    db = create_engine('mssql://', creator=connect)
    db.echo = False

    return db

def main():
    global DBCONN

    # Linux with FreeTDS
    connect_string = "DRIVER={FreeTDS};SERVER=<server name>;PORT=<port num>;DATABASE=<db>;UID=<user>;PWD=<password>;TDS_Version=<version num>;"

    # Windows with SQL Server
    connect_string = "DRIVER={SQL Server};SERVER=<server name>;PORT=<port num>;DATABASE=<db>;UID=<user>;PWD=<password>;"

    DBCONN = sqlalchemy_connect(connect_string)


if __name__ == "__main__":
    main()
+1

All Articles