I am trying to open an FDB file, but it doesn't seem to work. Whatever I try, I get this error:
Opening ./20190401_database.fdb
Traceback (most recent call last):
File "test.py", line 16, in <module>
conn = fdb.connect(dsn=local_copy, user='****', password='****', charset='iso8859_1')
File "/usr/local/lib/python3.6/site-packages/fdb/fbcore.py", line 848, in connect
"Error while connecting to database:")
fdb.fbcore.DatabaseError: ('Error while connecting to database:\n- SQLCODE: -551\n- no permission for read-write access to database /var/www/wsgi/data/20190401_database.fdb', -551, 335544352)
I am running this code as user apache, and the same user just copied the FDB file into that location, so I am pretty sure the user has read/write access.
import fdb
from datetime import date, timedelta
DB_PATH="."
yesterday = date.today() - timedelta(days = 1)
yesterday = yesterday.strftime("%Y%m%d")
filename = f'{yesterday}_database.fdb'
local_fullpath = f'{DB_PATH}/{filename}'
local_copy = local_fullpath
print("Opening "+local_copy)
conn = fdb.connect(dsn=local_copy, user='****', password='****', charset='iso8859_1')
conn.close()
Even when running the script as root, I get the same error.
The problem was in the fact that the FDB file has to be read/writable by the firebird user.
Setting that correctly made it work.
Related
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.
i was able to connect to an external db but while trying to generate a qr code from the result i'm getting this error.
import pymssql, pandas as pd, openpyxl, pyqrcode, os
QRCode(content=b'0001-87L58/1', error='H', version=2, mode='alphanumeric')
Traceback (most recent call last):
File "C:/Users/MSE/PycharmProjects/qrcode_gen/CIN_from_Server.py", line 25, in <module>
qr.eps(PIN +'.eps', scale=2.5, module_color='#36C')
File "C:\Users\MSE\PycharmProjects\Python4everyone\venv\lib\site-packages\pyqrcode\__init__.py", line 624, in eps
background, quiet_zone)
File "C:\Users\MSE\PycharmProjects\Python4everyone\venv\lib\site-packages\pyqrcode\builder.py", line 1461, in _eps
f, autoclose = _get_writable(file_or_path, 'w')
File "C:\Users\MSE\PycharmProjects\Python4everyone\venv\lib\site-packages\pyqrcode\builder.py", line 924, in _get_writable
stream_or_path = open(stream_or_path, mode)
FileNotFoundError: [Errno 2] No such file or directory: '0001-87L58/1.eps'
My code:
import pymssql, pandas as pd, openpyxl, pyqrcode, os
#Connecting to Db using Pymssql library
host = "myHost"
username = "Mine"
password = "mine#$"
database = "external"
conn = pymssql.connect(host, username, password, database) #connecting string
read_query = " Select * FROM dbo.M_tblPIN WHERE TI = 'A01' "
mycursor = conn.cursor()
mycursor.execute(read_query)
result = mycursor.fetchall()
for item in result:
PIN = item[12]
# using pyqrcode to create the barcode
qr = pyqrcode.create(PIN)
print(qr)
qr.eps(PIN + '.eps', scale=5, module_color='#36C')
qr.eps(PIN + '.eps', background='#eee')
How to generate the qrcode and save it in an external directory?
last line in traceback is this:
FileNotFoundError: [Errno 2] No such file or directory: '0001-87L58/1.eps'
so the item[12] contains not only a filename, but also a directory name, which does not exist. As such Python is unable to write a file into non-existing directory, and raises an exception. Either create the directory first, or be sure you're in the directory where you think the subdirectory is.
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
I've got the problem using pywin32 library and trying to connect to OLEDB.
Traceback
Traceback (most recent call last):
File "<input>", line 35, in <module>
File "<input>", line 31, in ado
File "<COMObject ADODB.Recordset>", line 4, in Open
the XML parser for analysis: the response Time for the XML for analysis request timed out before it was completed.', None, 0, -2147467259), None)
I've tried to add Connect Timeout=1000 to my connectionstring to no avail.
Code
import win32com.client
import pyodbc
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = CONNECTION_STRING
conn.Open(DSN)
rs = win32com.client.Dispatch(r'ADODB.Recordset')
strsql = u"""
select
...
...
...
"""
h = rs.Open(strsql, conn,0,1)
ts = rs.GetRows()
conn.Close()
return ts
I think that the problem is here:
h = rs.Open(strsql, conn,0,1)
I can't see which parameters should be passed to 'Open'. But I think it must have timeout parameter.
How can I change command timeout?
The problem is solved by adding:
conn.CommandTimeout=3000
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.