Connecting to Firebird database from Windows local network - python

I have been writing a small Python app for several weeks. The application reads data from a Firebird database and it copies it to another DB. I'm using FDB with Firebird embedded.This is my connection code.
def createConnectionTo(path):
try:
connection = fdb.connect(
database=path,
user='SYSDBA',
password='masterkey',
charset='WIN1252'
)
print("Connessione al database riuscita!\n")
return connection
except fdb.fbcore.DatabaseError as details:
errorMsg = "ERRORE: impossibile connettersi al database!\nPer favore scegliere un altro file.\n\nDETTAGLI\n"+str(details).replace("\\n", "\n")+"\n"
print(errorMsg)
return False
except fdb.fbcore.ProgrammingError as details:
errorMsg = "ERROR: bad parameters value!\nPlease check your connection code.\nDETAILS: "+str(details)+"\n"
print(errorMsg)
return False
except Exception as errorMsg:
print("ERRORE: "+str(errorMsg))
input("Premi un ENTER per chiudere la finestra.")
return -1
This code works for folders inside my computer, but inexplicably it doesn't work for folders shared in our local network. I used os.path.exists() to check whetever Python was able to find the selected shared folders and it always returned True.I keep getting this error and I don't have any clue how to solve it, even if I suspect that it is somewhat related to a slash conversion issue.
('Error while connecting to database:
- SQLCODE: -902
- I/O error during "CreateFile (open)" operation for file "Danea Easyfatt\\ANYMA 2017 dal 06-02-17.eft"
- Error while trying to open file
- Impossibile trovare il percorso specificato. ', -902, 335544344)
I tried all the following way to type the path:
\\CENTRALE\Danea Easyfatt\ANYMA 2017 dal 06-02-17.eft
//CENTRALE/Danea Easyfatt/ANYMA 2017 dal 06-02-17.eft
\\\CENTRALE\\Danea Easyfatt\\ANYMA 2017 dal 06-02-17.eft
None of them worked.

You cannot access databases on a network share. Firebird explicitly disallows this*. However, as far as I can tell, the error you display simply means you're trying to use an invalid path to access file.
If you want to connect to a Firebird database over a network, you should connect to a Firebird server on the system hosting the database. That means running Firebird server, not using Firebird Embedded.
* : You can configure Firebird to allow opening database on a network drive, but it is a great way to corrupt a database if multiple processes try to modify the database from different hosts, it is something you really should not do.

Related

sqlite3 OperationalError(disk I/O error) when accessing a DB with Python inside a Virtual Machine

I have already looked at these (and others):
disk I/O error with SQLite3 in Python 3 when writing to a database
Error: disk I/O error on a newly created database
Might be similar/related to SQLite disk I/O error (3850)
I am parsing an iOS full extraction image (about 61G) from zip file. The file is copied from the zip into a temp folder and is read from there. I do not create the database nor do anything else to it.
So running this code errors inside of VMware 16.2.1 using a Windows 10 Host and a Windows 10 virtual machine. If I run this directly on the host, it will work fine -- change "Z:\" to "D:\VM Shares" + the rest of the path.
import sqlite3
from pathlib import Path
>>> file_path = Path('Z:\\Forensics\\CTF21_Marsha_iPhoneX_FFS_Premium_2021_07_29\\xLEAPP_Reports_2021-11-23_Tuesday_115054\\temp\\filesystem2\\containers\\Shared\\SystemGroup\\30CCFC92-08E6-458A-B78B-DA920EF2EF82\\Library\\Database\\com.apple.MobileBluetooth.ledevices.other.db')
>>> db = sqlite3.connect(f'file:{file_path}?mode=ro', uri=True)
>>> cursor = db.cursor()
# The line below provides a method to check: Did I just open a sqliteDB? Errors if not
# a sqliteDB. I have seen that you can basically open a text file through the connect()
# statement and get a cursor. So this just a check before other code would run.
>>> cursor.execute("PRAGMA page_count").fetchone()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.OperationalError: disk I/O error
Now, if I change the connect() statement to the following:
>>> db = sqlite3.connect(f'file:{file_path}?mode=ro&immutable=1', uri=True)
You can read about immutable object on SQL Uniform Resource Identifiers (section 3.3. This seems to work leading me to believe the DB is locked somehow.
I have reboot the virtual machine and rebooted the host. I did notice copying the DB in the same directly (sometimes) works but copying to another directly (even one higher) always works.
I thought it might be done to the length of the path name. I've tried added "\?", "\\?\", or other variations for the longer paths but this does not work. The path length is under 256 anyway.
This is related to xLEAPP with the problematic code located in abstract.py.
Added a issue on Github for it - xleapp#5

ibm_db connect DB2 using SSLClientKeystoredb in Python

I am trying to connect to a DB2 JDBC database thru Python with providing the SSLClientKeystoredb.
This is how I have been trying to connect to the DB:
import ibm_db
arg1 = "DRIVER={IBM DB2 ODBC DRIVER};" + "DATABASE=databasename;HOSTNAME=" + "server" + ";PORT=" + "111111" + ";PROTOCOL=TCPIP;UID=" + "userId" + ";PWD=" + "password" + ";SECURITY=ssl" + ";SSLClientKeystoredb=" + "C:/Users/path/db2_ssl_keydb.kdb" + ";SSLClientKeystash=" + "C:/Users/path/db2_ssl_keydb.sth"
conn=ibm_db.connect(arg1, "", "")
I keep getting this error:
SQLCODE=-1109M][CLI Driver] SQL1109N The command was not processed because the database manager failed to load the following DLL: "GSKit Error: 202". SQLSTATE=42724
I installed both GSKit8 Crypt and GSKit SSL 64-bit. Any help would be appreciated !
On Db2-client workstations, you can avoid installing/configuring the GSK8 as a separate component, and still have encrypted SSL connections to Db2-LUW servers.
Note that you might need GSK8 on client-workstations for other reasons (other non-Db2 applications), but that is a separate matter.
On MS-Windows, there are two ways to avoid having to install GSK8 for Db2 SSL connections, but in this answer I mention one way.
Technically this feature became available at V10.5 fixpack 5 Db2-clients, but there were some bugs so I suggest to avoid that fixpack and start with fixpack 8 or higher. This functionality also works in V11.1 Db2-clients.
If you have the server-certificate in ARM format, then you can use the SSLSERVERCERTIFICATE and SECURITY keywords in the connection-string to connect with SSL from Python (or from any tool that uses the Db2 CLI libraries).
With this approach you don't need a keystore and stash to be manually created, and you don't need SSLClientKeystoredb etc in the connection string.
You still need to add appropriate security for the ARM file both at rest and during distribution.
This approach may be easier to manage, and example connection is below:
try:
arg1="DATABASE=whatever;HOSTNAME=whatever;PORT=50443;UID=whavever;PWD=whatever;SSLServerCertificate=/path_to/db2server_instance.arm;SECURITY=ssl;"
conn = ibm_db.connect(arg1,"","")
except:
logging.error('Error: Failed to connect to database: %s', ibm_db.conn_errormsg())
sys.exit(1)
Don't know if it helps, but when I launched the gitBash / command prompt via "run as administrator" it worked for me. After I used the
conn=ibm_db.connect("Database=****DB; Hostname=***.***.***.COM; PORT=****; Security=ssl; SSLClientKeystoredb=c:/keystore/ibmca.kdb; SSLClientKeystash=c:/keystore/ibmca.sth;UID= ; PWD= ;",'','')
"202 - GSK_KEYRING_OPEN_ERROR
Unable to open the key file or the Microsoft Certificate Store. Either the path was specified incorrectly or the file permissions did not allow the file to be opened, or the file format is incorrect."
The arg1 passed to ibm.db is badly formatted and you are missing a semi-colon after you assign your SSLClientKeystash. Try following this: IBM Support
The issue was solved by switching to python 2.7.9, and changing the path of the SSL to "C:\SSL" for the certificates. Not sure if changing the path helped but just wanted to mention that for future reference.

Cant connect to superset using athena database

I am new to superset.
Going to Sources > Databases for a new connection to my athena.
I have downloaded JDBC driver and writing following connection line:
awsathena+jdbc://AKIAJ2PKWTZYAPBYKRMQ:xxxxxxxxxxxxxxx#athena.us-east-1.amazonaws.com:443/default?s3_staging_dir='s3://aws-athena-query-results-831083831535-us-east-1/' as SQLAlchemy URI. First parameter being access key and 2nd being secret key(Modified a bit for privacy)
I am getting the error:
ERROR: {"error": "Connection failed!\n\nThe error message returned was:\nCan't load plugin: sqlalchemy.dialects:awsathena.jdbc"}
I really wish to explore the open source visualisation using superset on my databases.
As per Superset documentation, you need to escape/encode at least the s3_staging_dir, i.e.,
s3://... -> s3%3A//...
Have you followed that step?
If you are sure you have done pip install "PyAthenaJDBC>1.0.9" in the same python environment as you start your superset. Try restarting Superset in the same environment.
In my case problem was with the special characters in the aws_secret_key and s3_staging_dir. I solved it by putting the output of quote_plus methods into the URI. No quotes were required.
from urllib.parse import quote_plus
secretkey = quote_plus(aws_secret_access_key)
loc = quote_plus(s3_staging_dir)
Further, make sure the schema_name (i.e. database name) already exists in the s3 path. Hope it helps!

Python pymssql connection string using variables

I'm writing a script that connects to MSSQL DB using pymssql module.
I couldn't find a way to make the connect method to work using variables.
This works:
a = pymssql.connect(host='sqlserver', port=3183,user='admin',password='pass',database='master')
This does not (b1-5 are variables):
a = pymssql.connect(b1,b2,b3 b4,b5)
(Like shown in first example in www.pymssql.org/en/latest/pymssql_examples.html)
I'm getting this error:
File "pymssql.pyx", line 636 in pymssql. connect (pymssql. c:10178)
pymssql.OperationalError: (20009, 'DB-Lib error message 20009,severity
9:\nUnable to connect: Adaptive Server is unavailable or does not
exist\nNet-Lib error during Unknown error (10035)\n')
The database is fine, I can manually log in and the literal connection string works.
my variables (b1-5) include no single nor double quotes.
When I'm using single quotes I'm getting
Connection to database failed for an unknown reason.
Do you have an idea what could be the problem?
You should write:
a = pymssql.connect(host=b1, port=b2,user=b3,password=b4,database=b5)
Where b1 is actually a HOST, b2 is a PORT, and so on...

Python sqlite3 "unable to open database file" on windows

I am working on a windows vista machine in python 3.1.1. I am trying to insert a large number of rows into a SQLite3 db. The file exists, and my program properly inserts some rows into the db. However, at some point in the insertion process, the program dies with this message:
sqlite3.OperationalError: unable to open database file
However, before it dies, there are several rows that are properly added to the database.
Here is the code which specifically handles the insertion:
idx = 0
lst_to_ins = []
for addl_img in all_jpegs:
lst_to_ins.append((addl_img['col1'], addl_img['col2']))
idx = idx + 1
if idx % 10 == 0:
logging.debug('adding rows [%s]', lst_to_ins)
conn.executemany(ins_sql, lst_to_ins)
conn.commit()
lst_to_ins = []
logging.debug('added 10 rows [%d]', idx)
if len(lst_to_ins) > 0:
conn.executemany(ins_sql, lst_to_ins)
conn.commit()
logging.debug('adding the last few rows to the db')
This code inserts anywhere from 10 to 400 rows, then dies with the error message
conn.executemany(ins_sql, lst_to_ins)
sqlite3.OperationalError: unable to open database file
How is it possible that I can insert some rows, but then get this error?
SQLite does not have record locking; it uses a simple locking mechanism that locks the entire database file briefly during a write. It sounds like you are running into a lock that hasn't cleared yet.
The author of SQLite recommends that you create a transaction prior to doing your inserts, and then complete the transaction at the end. This causes SQLite to queue the insert requests, and perform them using a single file lock when the transaction is committed.
In the newest version of SQLite, the locking mechanism has been enhanced, so it might not require a full file lock anymore.
same error here on windows 7 (python 2.6, django 1.1.1 and sqllite) after some records inserted correctly: sqlite3.OperationalError: unable to open database file
I ran my script from Eclipse different times and always got that error. But as I ran it from the command line (after setting PYTHONPATH and DJANGO_SETTINGS_MODULE) it worked as a charm...
just my 2 cents!

Categories