I'm getting the following error after running this:
connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=xxxxx.xxxx.net;PORT=1443;DATABASE=xxx;UID=xxxx;PWD=xxxx')
cursor = connection.cursor()
SQLCommand = ("SELECT [Date]...FROM [dbo].[...]")
cursor.execute(SQLCommand)
results = cursor.fetchone()
connection.close
Error: libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: collate_byname::collate_byname failed to construct for C/en_US.UTF-8/C/C/C/C
Any ideas on how to remediate?
Related
code I used to establish connection between python code and ms-access
import pyodbc
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\LnT_project\Project.accdb;')
cursor = conn.cursor()
cursor.execute("Insert into dbtable (File_name, File_size) values(abc, 2)")
conn.commit()
I am getting this error:
pyodbc.Error: ('07002', '[07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2. (-3010) (SQLExecDirectW)')
Try with quotes to mark as a text value:
cursor.execute("Insert into dbtable (File_name, File_size) values('abc', 2)")
At the beginning of my process, I connect to a SQL Server database
conn = pyodbc.connect("DRIVER=" + driver + ";SERVER=" + server + ";PORT=" + port + ";DATABASE=" + dbn + ";UID=" + user +";PWD=" + pwd)
and then use that connection and execute queries with this code:
cur = conn.cursor()
try:
cur.execute(sql)
conn.commit()
if cur.description is not None:
data = pd.io.sql.read_sql_query(sql, conn)
return data
except Exception as err:
conn.rollback()
raise err
cur.close()
It works fine until the code does not execute a query for several hours, then I get the following error:
cur.execute(sql)
pyodbc.OperationalError: ('08S01', '[08S01] [FreeTDS][SQL Server]Write to the server failed (20006) (SQLExecDirectW)')
It seems that there is a timeout. Is there any way to recycle the existing connection (conn)?
I am looking to connect python to an Access database with the following code:
connStr = (
r"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};"
r"DBQ=O:\Architecture\DAART\Data Analytics Team\DAART.accdb;"
)
cnxn = pyodbc.connect(connStr)
cursor = cnxn.cursor()
df = pd.read_sql("select * from APMS SV-8 Report", cnxn)
For the last line of code, I am receiving the following error message:
DatabaseError: Execution failed on sql 'select * from APMS SV-8 Report': ('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error in FROM clause. (-3506) (SQLExecDirectW)')
Access SQL requires you bracket table names if they contain spaces, keywords or special characters:
select * from [APMS SV-8 Report]
while executing stored procedure using pymssql getting error:
_mssql.MSSQLDatabaseException: (8144, b'Procedure or function GET_USER_DETAILS has too many arguments specified. DB-Lib error
message 8144, severity 16:\nGeneral SQL Server error: Check messages
from the SQL Server\n')
code snippet:
connection = self.engine.raw_connection()
cursor = connection.cursor()
args= ('user')
cursor.callproc("GET_USER_DETAILS", args)
cursor.nextset()
result = list(cursor.fetchone())
cursor.close()
connection.commit()
print(result)
stored procedure GET_USER_DETAILS, accept only one parameter, thats user name.
The below code is working.
connection = self.engine.raw_connection()
cursor = connection.cursor()
args= ('user')
cursor.callproc("GET_USER_DETAILS", (args,))
cursor.nextset()
result = list(cursor.fetchone())
cursor.close()
connection.commit()
print(result)
I have written below code to connect SQL Server Database from Visual Studio using Python:
import pyodbc
con = pyodbc.connect('Driver={SQL Server};Server=localhost;Database=ReportServerTempDB;Trusted_Connection=yes')
cur = con.cursor()
cur.execute("select [User], [datetime] FROM [ReportServerTempDB].[dbo].
[DBUpgradeHistory]")
for row in cur:
print (row.user + "," + row.datetime)
#print row[0] + "," + row[1]
cur.close()
con.close()
However, I'm getting an error like this:
Traceback (most recent call last):File "IronPythonApplication1.py",
line 2,in con = pyodbc.connect('Driver={SQL
Server};Server=localhost;Database=ReportServerTempDB;Trusted_Connection=yes')
pyodbc.Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver]
[DBNETLIB]SQL Server does not exist or access denied. (17)
(SQLDriverConnect)')
Note: I have Windows Authentication to SQL Server, and I'm using VS 2015, and Python environment is IRON Python 64 bit 2.7
EDIT:
I changed driver as this: Driver={ODBC Driver 11 for SQL Server}
If I give like this in my code
for row in cur:
print (row.user)
getting a new kind of error.
Traceback (most recent call last):
File "IronPythonApplication1.py", line 6, in
for row in cur:
pyodbc.ProgrammingError: Attempt to use a closed cursor.
How to solve this?
I just changed it like this and its working:
import pyodbc
con = pyodbc.connect('Driver={ODBC Driver 11 for SQL Server};Server=localhost;Database=ReportServer;Trusted_Connection=yes')
cur = con.cursor()
cur.execute("select userid,username from Users")
for row in cur.fetchall():
print (row)