pyodbc

How to get the IDENTITY value when using INSERT … OUTPUT with pyodbc

淺唱寂寞╮ 提交于 2019-12-07 07:56:43
问题 I am trying to get the ID of a newly inserted row by using OUTPUT . However, I encountered the HY010 error. The following query/code is what I use: string = """ SET NOCOUNT ON; DECLARE @NEWID TABLE(ID INT); INSERT INTO dbo.t1 (Username, Age) OUTPUT inserted.id INTO @NEWID(ID) VALUES(?, ?) SELECT ID FROM @NEWID """ cursor.execute(string, "John Doe", 35) cursor.commit() id = cursor.fetchone()[0] the last line id = cursor.fetchone()[0] led to a HY010 error (see below). Any advice would be

How can i pass configuration variable values into the pyodbc connect command?

删除回忆录丶 提交于 2019-12-07 06:42:58
问题 I have a .ini(configuration file) where i have mentioned the server name, Database Name, UserName and Password with which i can connect my app to the MSSQL self.db = pyodbc.connect('driver={SQL Server};server=homeserver;database=testdb;uid=home;pwd=1234') corresponding data mentioned above connect statement is now in config.ini ` self.configwrite = ConfigParser.RawConfigParser() configread = SafeConfigParser() configread.read('config.ini') driver = configread.get('DataBase Settings','Driver')

UnicodeDecodeError on Python 3.5 when doing SQL query

五迷三道 提交于 2019-12-07 04:54:36
I was using Python 3.5 + pyodbc (on windows 10) to do apply some SQL queries on a server, and everything was doing fine like this: In [1]: import pyodbc ...: In [2]: server = 'tcp:192.168.100.76' ...: database = 'database' ...: username = 'username' ...: password = 'password' ...: In [3]: cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+use ...: rname+';PWD='+ password) ...: cursor = cnxn.cursor() In [29]: sqlQuery = "SELECT TOP 50 * " + \ ...: "FROM dbo.DS10_CIBBASE " + \ ...: "LEFT JOIN dbo.DS10_CIBCITY AS O_CITY ON dbo.DS10_CIBBASE

Passing table name as a parameter in pyodbc

半城伤御伤魂 提交于 2019-12-07 02:11:50
问题 I am trying to pass a table name in pyodbc as a parameter to access data from ms sql 2005. I've tried to substitute it with ? but it never works. I would be glad to receive any advice on how to accomplish this. 回答1: Since you are using pyodbc, I assume you are not calling a SPROC. I recommend building your SQL string in python and then passing it to SQL to execute. import pyodbc dbconn = pyodbc.connect(ConnectionString) c = dbconn.cursor() j = 'table1' #where table1 is entered, retreived, etc

Is it possible to have SQL Server convert collation to UTF-8 / UTF-16

 ̄綄美尐妖づ 提交于 2019-12-06 19:23:54
问题 In a project I am working on my data is stored in SQL Server, with the collation Danish_Norwegian_CI_AS . The data is output'ed through FreeTDS and ODBC, to python that handles the data as UTF-8. Some of the characters, like å, ø and æ, are not being coded correctly, causing the project progress to grind to a halt. I spent a couple of hours reading about the confusing world of encodings, collation and code-pages, and feel like I have gotten a better understanding of the entire picture. Some

Convert a pyodbc.Row to a string

久未见 提交于 2019-12-06 16:32:00
I need to convert a pyodbc.Row to a string. The internet provides several suggestions, none of which seem to work for me. row = cursor.fetchone() #unicodedata.normalize('NFKD', row).encode('ascii','ignore') #TypeError: must be unicode, not pyodbc.Row #row.fieldname.encode('utf8') #AttributeError: 'pyodbc.Row' object has no attribute 'fieldname' tblName = str(row) tblName.replace("text:u","").replace("'","") tblName = tblName.encode('utf-8') print tblName The above either give an error (shown in comment) or seems to have no effect as shown in the output here: (u'myTableName', ) # print tblName

pyodbc execute SQL code

别等时光非礼了梦想. 提交于 2019-12-06 15:37:20
I am trying to use pyodbc cursor execute the right way to prevent injection attacks, as suggested here: what does ? mean in python pyodbc module My code is as follows: query = """\ SELECT ?,count(*) FROM ? WHERE ?=? """, ('date', 'myTable', 'date', '2017-05-08') cursor.execute(query) And I get an error: TypeError: The first argument to execute must be a string or unicode query. For the right answer I'd want to: Keep the question mark format to avoid SQL injection attacks Keep the triple quotes format so I can write long SQL queries and not loose code readability. Is there a way to achieve this

Pyodbc Issue with Teradata

半世苍凉 提交于 2019-12-06 14:38:21
Getting below error while connecting with Teradata from pyodbc in Linux ('632', '[632] 523 630 (0) (SQLDriverConnect)') Details below: Connection String DRIVER={Teradata};DBCNAME=;UID=;PWD=;QUIETMODE=YES; Pyodbc Version:pyodbc-2.1.8 Python Version: 2.7.3 Teradata below: RELEASE 14.10.03.07 VERSION 14.10.03.06 Linux Host Details: 2.6.39-400.17.2.el5uek Please help. Need to export ODBCINI path, its done export ODBCINI=/opt/teradata/client/14.10/odbc_64/odbc.ini 来源: https://stackoverflow.com/questions/26621179/pyodbc-issue-with-teradata

How do I access AS/400 using SQLAlchemy?

此生再无相见时 提交于 2019-12-06 13:15:23
问题 Short version: Please tell me how to connect to AS/400s via SQLAlchemy. Long version My ultimate goal is to join data from SQL Server and AS/400 to be displayed in a Flask Python application. My approach has been to get the data from each database into Pandas dataframes, which can then be joined and output as JSON. If anyone has a better approach, feel free to leave a comment. The problem with the way I'm trying to do this is that Pandas.read_sql_query() relies on SQLAlchemy, and getting

How to configure the Database setting Django-MSSQL using django-pyodbc (ubuntu 16.04)?

孤人 提交于 2019-12-06 12:45:40
I'm new to Django and currently trying to use another database to save my model (i.e. MS SQL ). My database is deployed in a docker container: 903876e64b67 microsoft/mssql-server-linux "/bin/sh -c /opt/mssq" 5 hours ago Up 5 hours 0.0.0.0:8888->1433/tcp nauseous_williams I also create a new user for my login to the SQL Server. Username='kucing', password='xxxxx' With my user, I can use sqlcmd to access my DB as below: sqlcmd -S localhost,8888 -U kucing -P 'xxxxx' Therefore, I change my Django setting for my DB as shown here: DATABASES = { 'default': { 'ENGINE': 'sql_server.pyodbc', 'NAME':