I'm using pyodbc to connect to Vertica.
I've installed Vertica driver, unixodbc and pyodbc.
Now I have problem with executinq unicode queries.
con = pyodbc.connect('Driver={driver};Servername={server};Port={port};Database={db};UserName={user};password={password}'.format(...))
cur = con.cursor()
cur.execute('SELECT * FROM table') # It works
cur.execute(u'SELECT * FROM table') # It fails
The error is:
ProgrammingError: ('42601', '[42601] ERROR 4856: Syntax error at or near " (4856) (SQLExecDirectW)')
Looks like wrong encoding defined in vertica.ini file(or wrong locale setting in odbc.ini)
vertica.ini
[Driver]
DriverManagerEncoding=UTF-16
...
odbc.ini
...
Locale = en_US#collation=binary
...
I bet on vertica.ini. I have no MacOS to check, but by docs you need to define UTF-32. Can you post your vertica.ini abd odbc.ini + database locale?
HP Vertica
daniel=> show locale;
name | setting
--------+--------------------------------------
locale | en_US#collation=binary (LEN_KBINARY)
(1 row)
For more info read here.
What version of pyodbc and python you are using? What OS?
try using first line in your python code as # -- coding: utf-8 --?
and update pyodbc
sudo pip install --upgrade pyodbc
Related
I am trying to connect to an oracle database with Python code. I am using the OracleDB package but want it so that the user is able to connect to the DB with their own password machine and password rather than coding it into the code itself.
So far I have this,
import oracledb
import os
username=os.environ.get("Username")
pw=os.environ.get("pasword")
conn = oracledb.connect(user=username, password=pw, host="url", port=0000, service_name="service"
Source the environment variables (Make them available to the python process)
$cat env.sh
export USERNAME=app_schema
export PASSWORD=secret
$cat connect.py
import oracledb
import os
username=os.environ.get("USERNAME")
pw=os.environ.get("PASSWORD")
conn = oracledb.connect(user=username, password=pw, host="localhost", port=1521, service_name="XEPDB1")
c = conn.cursor()
c.execute('select dummy from dual')
for row in c:
print (row[0])
conn.close()
$ # source the variables (note the dot)
$. env.sh
$python connect.py
X
Best of luck!
I am try to connect to my gaussdb or even postgressql using in python jaydebeapi from linux
and i keep get error
Class name not found
I copy my jar file to /usr/lib/jvm/java-11-openjdk-amd46/lib/com.driver.jar
is there something else ?
‘’’
import jaydebeapi
import sys
jaydebeapi.connect("com.gauss.Driver",
url, [username, password], "./file-jdbc.jar")
Erorr Class com.gauss.driver not found
‘’’
You can try to explicitly start a JVM and pass it the fullpath of your driver jar file:
import jaydebeapi
import jpype
jpype.startJVM(jpype.getDefaultJVMPath(),
f"-Djava.class.path=/usr/lib/jvm/java-11-openjdk-amd46/lib/com.driver.jar")
jaydebeapi.connect("com.gauss.Driver", url, [username, password], "/usr/lib/jvm/java-11-openjdk-amd46/lib/")
If you're using postgresSQL databases I would also suggest you to take a look to the psycopg library:
https://pypi.org/project/psycopg/
https://www.geeksforgeeks.org/introduction-to-psycopg2-module-in-python/
I know how to connect to a MS OLAP cube using Python on Windows - well, at least one method :-).
Usually I use the win32py package and invoke a .COM object to connect:
import win32com.client
connection = win32com.client.Dispatch(r'ADODB.Connection')
connString = 'Connection String to MSOLAP CUBE'
connection.Open(connString)
However, now I want to do the same with Linux (RHEL 7.2), and I have no idea how to do that. Can you please help me with that?
What I tried so far:
I tried using pyodbc-package or the sqlalchemy to establish a connection, but was not really successful in doing so (see Edit-1 section). Moreover, I am not even sure if I am looking at the right packages, or if it is even possible at all to connect to a MS OLAP cube on Linux :S
What I found so far:
On Windows I usually use a .COM object or the .NET framework to connect to the cube. However, if I understand it right, these are Windows specific languages/frameworks, and can't be used on Linux. Right now I am looking at Mono for Linux, to see if I can use it to establish the connection.
More information:
Python version: 3.4
Linux distribution: Red Hat Enterprise Linux 7.2
sudo privileges on the machine: yes
.
Edit-1 (see: shellter comment)
I tried three different methods till now, but as mentioned above, I am not even sure if I work in the right direction. The only thing I am confident in, is that I can't establish a connection to the Cube via a .COM object (like in the Windows case), as Linux does not support the .COM environment.
Code-1:
import pyodbc
server = 'server address'
database = 'database name'
connStr = 'Provider=MSOLAP;Data Source=' + server + ';Initial Catalog=' + database + '; Application Name=Python;'
pyodbc.connect(connStr)
Error-1:
File "C:/Users/Desktop/untitled/main.py", line 35, in <module>
File connStr(connStr)
File TypeError: 'str' object is not callable
Code-2:
import pyodbc
driver = '{MSOLAP}'
server = 'server address’
database = 'database name'
user = ‘user name’
passw = 'password'
connStr = "Provider=MSOLAP;Data Source=" + server + ";Initial Catalog=" + database + "; Application Name=Python;"
Error-2:
File "C:/Users/besechr/Desktop/untitled/main.py", line 19, in <module>
pyodbc.connect('DRIVER=' + driver + ';SERVER=' + server + ';DATABASE=' + database + ';UID=' + user + ';PWD=' + passw)
pyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
btw: if I try to connect to the underlying SQL server with the above-stated connection string (changing the driver parameter to {SQL Server}) it works fine.
Code-3:
import pyodbc
import sqlalchemy
engine = sqlalchemy.create_engine('mssql+pyodbc://' + server )
engine.connect()
Error-3:
sqlalchemy.exc.DBAPIError: (Error) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') None None
I had the same requirement.
We were running on Ubuntu 20.04.
Attempt 1
We Needed Python.Net latest Stable
This needed Mono As a Dependancy
Mono only supported .NetFW4.xx integration
We used standard installation of Python.Net (which was also .NetFW4.xx support)
Hence we HAD to use The specific lib Microsoft.AnalysisServices.AdomdClient.retail.amd64 Which is Windows compatible only
If we used microsoft.analysisservices.adomdclient.netcore.retail.amd64 it fails with no proper errors
Also All the packages had to be placed inside Monos package store. Else it failed to find them.
Finally it failed saying
EntryPointNotFoundException: GetConsoleWindow assembly:<unknown assembly> type:<unknown type> member:(null)
at (wrapper managed-to-native) Microsoft.AnalysisServices.AdomdClient.Interop.NativeMethods.GetConsoleWindow()
at Microsoft.AnalysisServices.AdomdClient.Utilities.WindowsRuntimeHelper.IsProcessWithUserInterface () [0x00018] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.Configuration.ConfigurationHelper.CheckIfProcessWithUserInterface () [0x00009] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.Configuration.RuntimeConfiguration..ctor (System.Collections.Generic.IDictionary`2[TKey,TValue] configuration) [0x0000a] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.Configuration.ConfigurationHelper.EnsureConfigurationIsLoaded () [0x00034] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.Configuration.ConfigurationHelper.get_Connectivity () [0x00000] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.GetTcpClient (Microsoft.AnalysisServices.AdomdClient.Hosting.IConnectivityOwner owner, Microsoft.AnalysisServices.AdomdClient.ConnectionInfo connectionInfo) [0x000ee] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.OpenTcpConnection (Microsoft.AnalysisServices.AdomdClient.ConnectionInfo connectionInfo, Microsoft.AnalysisServices.AdomdClient.Sspi.SecurityMode securityMode) [0x00025] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.OpenConnectionAndCheckIfSessionTokenNeeded (Microsoft.AnalysisServices.AdomdClient.ConnectionInfo connectionInfo) [0x0005a] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.XmlaClient+<>c__DisplayClass95_0.<OpenConnection>b__0 () [0x00000] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.Security.TransparentUserContext.ExecuteInUserContextImpl[TResult] (System.Func`1[TResult] action) [0x00000] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.Security.UserContext.ExecuteInUserContext[TResult] (System.Func`1[TResult] action) [0x00006] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.OpenConnection (Microsoft.AnalysisServices.AdomdClient.ConnectionInfo connectionInfo, System.Boolean& isSessionTokenNeeded) [0x00020] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.Connect (Microsoft.AnalysisServices.AdomdClient.ConnectionInfo connectionInfo, System.Boolean beginSession) [0x002c6] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.AdomdConnection+XmlaClientProvider.Connect () [0x00064] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.AdomdConnection+XmlaClientProvider.Microsoft.AnalysisServices.AdomdClient.AdomdConnection.IXmlaClientProviderEx.ConnectXmla () [0x00000] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.AdomdConnection.ConnectToXMLA (System.Boolean createSession, System.Boolean isHTTP) [0x00072] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at Microsoft.AnalysisServices.AdomdClient.AdomdConnection.Open () [0x000b9] in <d13c74b6ccb34cf09a1af7bf07e15252>:0
at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <533173d24dae460899d2b10975534bb0>:0
Cause
Folks smarter than me had explained here what is going on
C# to Mono GetConsoleWindow Exception
Attempt 2
Install .netCore Runtime https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu
Install Python.Net 3.0.0a2 (This supported .netCore so we did not need to depend on mono)
Set the runtime with clr_loader that is installed as a dependancy of pythonnet
from clr_loader import get_coreclr
from pythonnet import set_runtime
rt = get_coreclr("path_to_runtime_cong/net_core_rt_conf.json")
set_runtime(rt)
import clr
My Conf Looked like
{
"runtimeOptions": {
"tfm": "net6.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "6.0.3"
}
}
}
Extracted the microsoft.analysisservices.adomdclient.netcore.retail.amd64 nuget package in to GAC of the .netCore installation on the Nix Box (call this pkg_path)
Then Tryied to connect
import pandas as pd
from sys import path
path.append(**pkg_path**)
clr.AddReference('System.Data')
clr.AddReference('Microsoft.AnalysisServices.AdomdClient')
from Microsoft.AnalysisServices.AdomdClient import AdomdConnection, AdomdDataAdapter
conn = AdomdConnection("Data Source=<MY_SERVER>;Catalog=<MY_CUBE>;")
conn.Open()
And I was greeted with
NotSupportedException: This feature is supported for a .NET Core client only on Windows systems.
---------------------------------------------------------------------------
NotSupportedException Traceback (most recent call last)
<command-3652263429411005> in <module>
10
11 conn = AdomdConnection("Data Source=<MY_SERVER>;Catalog=<MY CUBE>;")
---> 12 conn.Open()
13 cmd = conn.CreateCommand()
14 cmd.CommandText = MDX_QUERY
NotSupportedException: This feature is supported for a .NET Core client only on Windows systems.
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.OpenConnectionAndCheckIfSessionTokenNeeded(ConnectionInfo connectionInfo)
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.<>c__DisplayClass94_0.<OpenConnection>b__0()
at Microsoft.AnalysisServices.Security.TransparentUserContext.ExecuteInUserContextImpl[TResult](Func`1 action)
at Microsoft.AnalysisServices.Security.UserContext.ExecuteInUserContext[TResult](Func`1 action)
at Microsoft.AnalysisServices.AdomdClient.XmlaClient.OpenConnection(ConnectionInfo connectionInfo, Boolean& isSessionTokenNeeded)
In a nutshell as of now this seems a bit impossible. At least in a straight forward manner.
I am trying to access a .mdb file which located on my system. My Code look slike this :
import csv
import pyodbc
MDB = '/home/filebug/client/my.mdb'
DRV = '{Microsoft Access Driver (*.mdb)}'
PWD = 'mypassword'
conn = pyodbc.connect('DRIVER=%s;DBQ=%s;PWD=%s' % (DRV,MDB,PWD))
print conn
curs = conn.cursor()
SQL = 'SELECT * FROM InOutTable;' # insert your query here
curs.execute(SQL)
rows = curs.fetchall()
curs.close()
conn.close()
But I am facing following Error :
Traceback (most recent call last):
File "mdb.py", line 8, in <module>
conn = pyodbc.connect('DRIVER=%s;DBQ=%s;PWD=%s' % (DRV,MDB,PWD))
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')
I have pyodbc-3.0.6-py2.7-linux-i686.egg installed on my system. using Ubuntu 12.04
Can any one sya me what is wrong here
I get this info from http://www.easysoft.com/developer/interfaces/odbc/linux.html
In this case unixODBC could not locate the DSN "dsn_does_not_exist" and hence could not load the ODBC driver. Common reasons for this error are:
The DSN "dsn_does_not_exist" does not exist in your USER or SYSTEM ini files.
The DSN "dsn_does_not_exist" does exist in a defined ini file but you have omitted the "Driver=xxx" attribute telling the unixODBC driver manager which ODBC driver to load.
The "Driver=/path_to_driver" in the odbcinst.ini file points to an invalid path, to a path to an executable where part of the path is not readable/searchable or to a file that is not loadable (executable).
The Driver=xxx entry points to a shared object which does not export the necessary ODBC API functions (you can test this with dltest included with unixODBC.
The ODBC driver defined by DRIVER=xxx in the odbcinst.ini file depends on other shared objects which are not on your dynamic linker search path. Run ldd on the driver shared object named by Driver= in the odbcinst.ini file and see what dependent shared objects cannot be found. If some cannot be found than you need to defined your LD_LIBRARY_PATH environment variable to define the paths to any dependent shared objects or add these paths to /etc/ld.so.conf and rerun ldconfig.
Im running mysql, pyodbc, python 2.7 loaded on Fedora 14 x64.
Odbcinst.ini is:
# Example driver definitions
# Driver from the postgresql-odbc package
# Setup from the unixODBC package
#[PostgreSQL]
#Description = ODBC for PostgreSQL
#Driver = /usr/lib/psqlodbc.so
#Setup = /usr/lib/libodbcpsqlS.so
#Driver64 = /usr/lib64/psqlodbc.so
#Setup64 = /usr/lib64/libodbcpsqlS.so
#FileUsage = 1
# Driver from the mysql-connector-odbc package
# Setup from the unixODBC package
[MySQL]
Description = ODBC for MySQL
#Driver = /usr/lib/libmyodbc5.so
#Setup = /usr/lib/libodbcmyS.so
Driver64 = /usr/lib64/libmyodbc5.so
Setup64 = /usr/lib64/libodbcmyS.so
FileUsage = 1
Odbc.ini is :
[MySQL]
Driver = MySQL
Database = mysql
Server = localhost
Socket = /var/lib/mysql/mysql.sock
User = rooter
Password = sshh
Mysql.sock is empty?
/var/lib/mysql/mysql.sock has 0.B
python script is:
import pyodbc
#pyodbc.pooling = False
conn = pyodbc.connect('DRIVER={MySQL};SOCKET=/var/lib/mysql/mysql.sock;UID=rooter;PWD=sshh')
csr = conn.cursor()
csr.execute("SET GLOBAL event_scheduler = ON")
csr.close()
conn.close()
del csr
I cant seem to connect with above script, Using isql i get Connected!
MyERROR msg:
Traceback (most recent call last):
File "/CCX/Py/MySql Event OFF.py", line 4, in <module>
conn = pyodbc.connect('DRIVER={MySQL};SOCKET=/var/lib/mysql/mysql.sock;UID=rooter;PWD=sshh')
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnectW)')
On Win XPx64,
#cxn = MySQLdb.connect (host = "localhost",user="rooter",passwd ="sshh")
Err Msg:
File "C:\Python26\lib\site-packages\MySQLdb\__init__.py", line 19, in <module>
import _mysql
ImportError: DLL load failed: %1 is not a valid Win32 application.
Comparing odbcinst.ini and your odbc.ini show an odd value for driver in odbc.ini, you should have a dynamic library here (I think).
And why don't you use direct mysql connection (without odbc)? :
import _mysql
conn = _mysql.connect(host="localhost", user="rooter",
password="sshh", db="mysql")
conn.query("SET GLOBAL event_scheduler = ON")
conn.close()
(not tested)
File "C:\Python26\lib\site-packages\MySQLdb__init__.py", line 19, in
import _mysql
ImportError: DLL load failed: %1 is not a valid Win32 application.
if Python26 && windows
we need download 3 files : libguide40.dll, libmmd.dll, libmySQL.dll
put the 3 files to C:\Python26\Lib\site-packages
and also put the 3 files to C:\Windows\system32
then restart your system
if Python27 && windows
when you setup the module ,be careful the version
MySQL-python-1.2.3.win32-py2.7.exe
MySQL-python-1.2.4.win32-py2.7.exe
one of the 2 version is you need
source: http://mysql-python.sourceforge.net/MySQLdb.html#some-mysql-examples
code tested:
import _mysql
conn=_mysql.connect(host="localhost", user="root", passwd="***", db="test")
conn.query("select * from test_table")
result = conn.use_result()
print result.fetch_row()
The fetch_row() function has two parameters:
maxrows limits the max number of rows returned.
how: it returns a tuple when how=0 and returns a dictionary when how=1.
For example, result.fetch(maxrows=5, how=1) returns an array (maximum size: 5) of dictionaries where the key is the column name and the value is the column value.