Python and SQL Anywhere 17 trouble with "Could not load dbcapi" - python

I installed SQL Anywhere 17 client. I ran /opt/sqlanywhere17/bin64/sa_config.sh And still have problem with sqlanydb.InterfaceError: ('Could not load dbcapi. Tried:None,dbcapi.dll,libdbcapi_r.so,libdbcapi_r.dylib', 0)
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
import ctypes
import sqlanydb
conn = sqlanydb.connect(UserID='XXX', Password='XXX', Host='10.10.10.10', DatabaseName='XXX', ServerName='XXX')
curs = conn.cursor()
curs.close()
conn.close()
Log:
Traceback (most recent call last):
File "./systest.py", line 6, in <module>
conn = sqlanydb.connect(UserID='XXX', Password='XXX', Host='10.10.10.10', DatabaseName='XXX', ServerName='XXX')
File "/usr/local/lib/python3.6/site-packages/sqlanydb.py", line 522, in connect
return Connection(args, kwargs)
File "/usr/local/lib/python3.6/site-packages/sqlanydb.py", line 538, in __init__
parent = Connection.cls_parent = Root("PYTHON")
File "/usr/local/lib/python3.6/site-packages/sqlanydb.py", line 464, in __init__
'libdbcapi_r.dylib')
File "/usr/local/lib/python3.6/site-packages/sqlanydb.py", line 456, in load_library
raise InterfaceError("Could not load dbcapi. Tried: " + ','.join(map(str, names)))
sqlanydb.InterfaceError: ('Could not load dbcapi. Tried: None,dbcapi.dll,libdbcapi_r.so,libdbcapi_r.dylib', 0)
Thanks for your help in advance

I faced the same issue and took me 2 days to solve it.
Here is the solution, your solution is in point 2.
If running on Linux or one of the Unix platforms, you'll need to set
up your environment before launching Python. So, download the sql
anywhere12 / 17 and install. ( You have done this)
open the terminal and run sa_config{.sh|.csh} script so your Python application can locate the required libraries (including libdbcapi12.dll).
Make sure that you run sa_config.sh file and app.py in same terminal otherwise you will face the same issue.
Example, I have created the bash file which run first.
. /opt/sqlanywhere17/bin32/sa_config.sh
python app.py
source : Full answer can be found here on sqlanywhere forum

Related

Python cannot connect to DB on XAMPP

I got a python file from another pc, and that pc is using mySQL to be the db. Now I'm trying to use db on XAMPP, but it seems some errors happened. I tried to use Anaconda Prompt to check the problem that shows below:
Traceback (most recent call last):
File "C:\xampp\htdocs\distest.py", line 48, in <module(ignore me)>
conn = pymysql.connect(host='localhost',
File "C:\Users\user\anaconda3\lib\site-packages\pymysql\connections.py", line 311, in init
self.encoding = charset_by_name(self.charset).encoding
AttributeError: 'NoneType' object has no attribute 'encoding'
I've imported something about connection between python and mySQL:
import pymysql
and this is line 48 in python file:
conn = pymysql.connect(host='localhost',
user='root',
passwd='',
db='test1',
charset='utf8_general_ci',
cursorclass = pymysql.cursors.SSCursor)
How can I fix it?
You cannot connect as root. You must use a different account.
Ideally you would create an account for the application with just the permissions it needs, as this is best practice.

reading pg_dump file occurs at open the file

I'm using the pgdumplib lib. Unfortunately there is an error, when I'm trying to open the file. The file is in the same folder as the python script. I'm using Python 3.7
Code:
import pgdumplib
dump = pgdumplib.load('test.dump')
print('Database: {}'.format(dump.toc.dbname))
print('Archive Timestamp: {}'.format(dump.toc.timestamp))
print('Server Version: {}'.format(dump.toc.server_version))
print('Dump Version: {}'.format(dump.toc.dump_version))
for line in dump.table_data('public', 'pgbench_accounts'):
print(line)
Error:
Traceback (most recent call last):
File "C:/Users/user/data/test.py", line 3, in <module>
dump = pgdumplib.load('test.dump')
File "C:\Users\user\venv\data\lib\site-packages\pgdumplib\__init__.py", line 24, in load
return dump.Dump(converter=converter).load(filepath)
File "C:\Users\user\venv\data\lib\site-packages\pgdumplib\dump.py", line 228, in load
raise ValueError('Path {!r} does not exist'.format(path))
ValueError: Path 'test.dump' does not exist
If you are running your code from C:/Users/user/700Joach/project/ and you have the following line in your script:
dump = pgdumplib.load('test.dump')
Then, python would look for the following path to open test.dump:
C:/Users/user/700Joach/project/test.dump
Namely, this part: load('test.dump') internally is forging a relative path to test.dump.
You can do several things to resolve the issue. Either move test.dump to the directory from which you are executing your code. Or, provide an absolute path to your test.dump as follows:
dump = pgdumplib.load('C:/Users/user/700Joach/project/test.dump')

Issue connecting with SQLAnywhere via Python: sqlanydb.OperationalError: Parse error: Cannot find .ini file

I'm afraid this might is just an configuration issue but maybe also some issue with coding. However, I'm trying to connect with a database using Python and sqlanydb. SQLAnywhere is installed within /opt/sybase and Shell is sourcing the sa_config.sh file.
Connection with normal tools are working well: E.g. via
dbisql -c "uid=dba;pwd=sql;eng=mydemo"
However, when I try doing this in Python with
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ctypes
import sqlanydb
def main(args):
db1 = sqlanydb.connect( userid="DBA",
password="sql",
DataSourceName="mytest")
db1.close
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
it's failing with
Traceback (most recent call last):
File "./myfile.py", line 52, in <module>
sys.exit(main(sys.argv))
File "./myfile.py", line 42, in main
DataSourceName="mytest")
File "/usr/local/lib/python2.7/dist-packages/sqlanydb.py", line 461, in connect
return Connection(args, kwargs)
File "/usr/local/lib/python2.7/dist-packages/sqlanydb.py", line 510, in __init__
self.handleerror(*error)
File "/usr/local/lib/python2.7/dist-packages/sqlanydb.py", line 520, in handleerror
eh(self, None, errorclass, errorvalue)
File "/usr/local/lib/python2.7/dist-packages/sqlanydb.py", line 342, in standardErrorHandler
raise errorclass(errorvalue)
sqlanydb.OperationalError: Parse error: Cannot find .ini file
I had a look onto sources of sqlanydb but didn't find any reference to an .ini file looking for at this point. So what is the correct way of enabling the ASA16 to run with sqlanydb?
That's the error you'll get if you have no ~/.odbc.ini file. You are specifying "DataSourceName=mytest", which tells the SQL Anywhere ODBC driver to look in ~/.odbc.ini for a DSN called mytest, but no .ini file was found.
Use the dbdsn utility to create the DSN in question.
Full disclosure: I work for SAP in SQL Anywhere engineering.

ImportError: No localization support for language 'eng' in python

I am getting ImportError: No localization support for language 'eng' when using MySQL Connector in Python.
My Traceback is as below.
Traceback (most recent call last):
File \"DB_Module.py\", line 151, in QueryDatabase
File \"\\share\app\Modules\mysql\connector\__init__.py\", line 44, in Connect
File \"\\share\app\Modules\mysql\connector\connection.py\", line 106, in __init__
File \"\\share\app\Modules\mysql\connector\connection.py\", line 325, in connect
File \"\\share\app\Modules\mysql\connector\connection.py\", line 288, in _open_connection
File \"\\share\app\Modules\mysql\connector\network.py\", line 326, in open_connection
File \"\\sfs\show_time\Showtime_Package\showtime\Modules\mysql\connector\errors.py\", line 160, in __init__
File \"\\share\app\Modules\mysql\connector\locales\__init__.py\", line 52, in get_client_error
ImportError: No localization support for language 'eng'
And my Current used SQL is
SELECT * FROM systemdetails
WHERE System_ID = 'System1' LIMIT 1
Can anyone tell me how to fix this error?
In order for the MySql connector to print the actual error string - it needs the eng/client_error file in the "locales" dir.
I personally, just manually created the dir "eng" and copy-paste the file from this link:
http://pydoc.net/Python/mysql-connector-python/1.0.7/mysql.connector.locales.eng.client_error/
> mkdir eng
> touch eng/__init__.py
> vi eng/client_error.py
> ... copy-paste, save, and exit.
I run the code again, and received:
2013: Lost connection to MySQL server during query
You'll probably get a different error, but hope it helps.
Just put this to import the locale when you compile again with pyInstaller or py2exe.
from mysql.connector.locales.eng import client_error
They will include the modules needed to fix the error.
In my case, changing the latest mysql.connector module to a former version solved the problem. I changed the version from 8.0.30 to 8.0.17.

Connecting to MySQL with Python in Ubuntu (Weird Error)

I'm using ubuntu 10.10, and after making the mistake of installing LAMP, (was unable to connect to a database with it), I came here and read how important it is to use apt-get install python-mysqldb instead. Deleted LAMP, re-installed using apt-get, and am now getting the same error when trying to run a basic server_version.py script.
The script is the server_version.py found here: http://www.kitebird.com/articles/pydbapi.html
My server_version.py script:
# server_version.py - retrieve and display database server version
import MySQLdb
# I have also tried setting host = 'localhost'
conn = MySQLdb.connect (host = "/opt/lampp/var/mysql/mysql.sock",
user = "root",
passwd = "myrealpass",
db = "testdb1")
cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version:", row[0]
cursor.close ()
conn.close ()
The error is:
Traceback (most recent call last):
File "server_version.py", line 10, in
db = "testdb1")
File "/usr/lib/pymodules/python2.6/MySQLdb/init.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 129, in init
from converters import conversions
File "/usr/lib/pymodules/python2.6/MySQLdb/converters.py", line 165, in
from decimal import Decimal
File "/usr/lib/python2.6/decimal.py", line 137, in
import copy as _copy
File "/home/browning/copy.py", line 4, in
ValueError: need more than 1 value to unpack
Just trying to get some basic experience using databases with python here, so I'm not set on MySQL if there is a better option. I have attempted to re-install mysqldb multiple times using apt-get and pip.
It looks like you have a file named copy.py that is being picked up instead of the Python standard library module copy. Rename or delete your file (and copy.pyc if it was created). Or run from a different directory.

Categories