pyodbc

FreeTDS translating MS SQL money type to python float, not Decimal

时间秒杀一切 提交于 2019-12-13 04:12:27
问题 I am connecting to an MS SQL Server db from Python in Linux. I am connecting via pyodbc using the FreeTDS driver. When I return a money field from MSSQL it comes through as a float, rather than a Python Decimal. The problem is with FreeTDS. If I run the exact same Python code from Windows (where I do not need to use FreeTDS), pyodbc returns a Python Decimal. How can I get back a Python Decimal when I'm running the code in Linux? 回答1: You could always just convert it to Decimal when it comes

pandas read_sql Imports NULLs as 0s

倖福魔咒の 提交于 2019-12-13 03:23:42
问题 Edit: Apparently my connection (PYODBC) is reading in the NULLs as 0s. I'd like to correct this on a "higher" level as I'll be reading in lots of SQL files. I can't find anything in the documentation to prevent this. Any suggestions? I am importing a SQL file/query as a string and then using pd.read_sql to query my database. In SQL Server I can do calculations between NULL values which results in a NULL value, however, in Python, this errors out my script. Query ran in SQL Server: SELECT

Function sequence error (0) (SQLFetch) - SQL, Pyodbc

若如初见. 提交于 2019-12-13 02:35:27
问题 for _item in file_list: for col in cursor.execute("select column1,column2 from tbl"): if _item[0]==col.column1: #I also tried using a different cursor, still doesn't work var1 = cursor.execute("select column2 from tbl where column1=?", _item[0]) for i in var1: var2 = i[0] try: cursor3.execute("insert into tbl(column2,column1,column3,column4) values (?,?,?,?)", (var1, _item[0],_item[1],_item[2])) cursor3.commit() except IOError as error: print error What i am trying to do is compare the values

Passing a Python List through a SQL Statement on a Loop

瘦欲@ 提交于 2019-12-13 01:14:27
问题 I am trying to figure out how to pass a list as a parameters through a where statement in SQL, I can not program what I am looking for, but below is what I am looking for. This is what I would do for one parameter.... x = 1 sql = """Select t1,t2,t3,t4 from database where t1= ? """ cur.execute(sql,x) Example of what I need X = [1,2,3,4] Select t1,t2,t3,t4 from database where t1= 1 Select t1,t2,t3,t4 from database where t1= 2 Select t1,t2,t3,t4 from database where t1= 3 Select t1,t2,t3,t4 from

SQLAlchemy PyODBC MS SQL Server DSN-less connection

那年仲夏 提交于 2019-12-12 21:56:53
问题 Using python 2.7 and the MS odbc driver through pyodbc. My connection string looks like this: mssql+pyodbc://myuser:mypass@serverip/instancename?driver=ODBC+Driver+11+for+SQL+Server I am getting login failed. However if I use the same credentials and "serverip\instancename" in the microsoft sql server management studio, I can connect. The thing that is driving me crazy is a couple of days ago, this same connection string worked for me but to a different sql server instance on the same machine

How to save output of SQL Server stored procedure with multiple result sets into each dataframe in pandas

若如初见. 提交于 2019-12-12 19:13:06
问题 I have a SQL Server stored procedure that returns 3 separate tables. How can I store each of this table in different data-frame using pandas? Something like: df1 - first table df2 - second table df3 - third table Where should I start looking at? Thank you import pandas as pd import pyodbc from datetime import datetime param = datetime(year=2019,month=7,day=31) query = """EXECUTE [dbo].PythonTest_USIC_TreatyYear_ReportingPackage @AsOFDate = '{0}'""".format(param) conn = pyodbc.connect('DRIVER=

Pyodbc Error - Python to MS Access

我的梦境 提交于 2019-12-12 17:22:09
问题 I am running on Windows 7, Python 2.7 and Microsoft Access 2013. When I try running: import pyodbc conn_string = ''' DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; UID=admin; UserCommitSync=Yes; Threads=3; SafeTransactions=0; PageTimeout=5; MaxScanRows=8; MaxBufferSize=2048; FIL=MS Access; DriverId=25; DefaultDir=C:\Users\jseinfeld; DBQ=C:\Users\jseinfeld\Desktop\Databasetest1.accdb; ''' connection = pyodbc.connect(conn_string) I receive the following error message: Error: ('HY000', "

PostgreSQL ANSI,Python SQL, utf-8' codec can't decode byte 0xa0

元气小坏坏 提交于 2019-12-12 11:20:03
问题 I am trying to run a sql query in python. In python 2 this used to work but now that I am using python 3 this no longer is working. I get error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 1: invalid start byte Update, added in the 3 lines in the middle,also tried using 'windows-1252' here. same error: conn_str = 'DSN=PostgreSQL30' conn = pyodbc.connect('DSN=STACK_PROD') ###newly added conn.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8') conn.setdecoding(pyodbc.SQL

How do I configure pyodbc to correctly accept strings from SQL Server using freeTDS and unixODBC?

我与影子孤独终老i 提交于 2019-12-12 08:10:13
问题 I can not get a valid string from an MSSQL server into python. I believe there is an encoding mismatch somewhere. I believe it is between the ODBC layer and python because I am able to get readable results in tsql and isql. What character encoding does pyodbc expect? What do I need to change in the chain to get this to work? Specific Example Here is a simplified python script as an example: #!/usr/bin/env python import pyodbc dsn = 'yourdb' user = 'import' password = 'get0lddata' database =

PyOdbc fails to connect to a sql server instance

烂漫一生 提交于 2019-12-12 07:27:50
问题 I am trying to connect to a sql server instance using pyodbc version 3.0.6., SQLAlchemy 1.0.4 on Windows 7 using a Python 2.7 (32 bit). I am using a connection string as follows DRIVER={SQL Server};SERVER=mymachinename;DATABASE=mydb;UID=sa;PWD=admin1; but I keep getting this error Could not parse rfc1738 URL from string 'DRIVER={SQL Server};SERVER=mymachinename;DATABASE=mydb;UID=sa;PWD=admin1' I am using the connection string with sqlSoup.Something like this db = sqlsoup.SQLSoup