Trouble connecting to SQL server with python -
i have installed microsoft sql server 2014 on pc want create database web application building (i have been learning python year , have basic experience sqlite).
after installing sql server 2014 , creating database called users, trying run basic commands database falling @ first hurdle on , over!
i have installed pymssql , pyodbc , tried running commands directly these have failed. (e.g. pymssql gives me typeerror: argument of type 'nonetype' not iterable when set variable conn = pymssql.connect(server, user, password, "tempdb")
my latest attempt use sqlalchemy achieve long awaited connection sql database. however, after installing this, failing on following error: "sqlalchemy.exc.operationalerror: (pymssql.operationalerror) (20009, 'db-lib error message 20009, severity 9:\nunable connect: adaptive server unavailable or not exist\nnet-lib error during unknown error (10035)\n')"
the question need answering is, how start talking database using sqlalchemy?
the code using follows: 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)
yes, pc called slugarplum. user han & lew. , server called thelroyserver. dsn = 1433. no password. (i don't know if wise giving information online data have not sensitive guess it's worth shot.)
also, if can direct me ultra-beginners resource python-sql server awesome getting beaten how complex seems be!
here's connect function , example connecting via pyodbc. connecting via pymssql should easy formatting connecting string pymssql. i've provided windows , linux options, tested on linux. hope helps.
def sqlalchemy_connect(connect_string): """ connect 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 freetds connect_string = "driver={freetds};server=<server name>;port=<port num>;database=<db>;uid=<user>;pwd=<password>;tds_version=<version num>;" # windows 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()
Comments
Post a Comment