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...
Related
Ive been trying to insert a image(here sample.txt) to mysql database using mysql client. I have created a table (output) in my "test" database. I use LONGBLOB for the field for inserting image.
I just converted the image into binary file by opening the file in rb mode and i encoded the result with base64 and tried to insert using execute command but i encountered with the error i mentioned below. I tried several ways and got tired.
#PROGRAM
import mysql.connector as m
import base64
conn = m.connect(host = "localhost",database = "test",user = "root",password = "root",port = 3306)
cur = conn.cursor()
file = open("photo.jpg","rb").read()
file = base64.b64encode(file)
arg = (file)
query = "INSERT INTO OUTPUT VALUES(%s)"
cur.execute(query,arg)
conn.commit()
OUTPUT:
File "C:\\Users\\Thanos\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\mysql\\connector\\connection.py", line 395, in \_handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s)' at line 1
i can predict the reason of the error is that when converting to binary it will come with b''. I thought due to the quotes present in that b my query quotes ending and tried changing with double quotes also but no use still error occurs.
i also tried using format, f string instead of "%s" but still no use still error occurs. i tried using format function, f string instead of "%s" string. still same error continues.
i want to get rid of the error. i saw some yt video they are also using the same logic of mine but i dont know why its occurring to me alone. i tried running the same code in another pc but no use :(
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.
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!
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.
Is it possible to apply a MySQL batch file using Python mysqldb library. So far I tried to "execute" the content of the file:
cur = connection.cursor()
cur.execute(file(filename).read())
cur.commit() # and without commit
This works only on a single statement. Otherwise I get the error:
Failed to apply content. Error 2014: Commands out of sync; you can't run this command now
I intend supporting any kind of MySQL schema, table changes so parsing the file line by line is not an option. Is there other solution than calling mysql client from Python?
I suppose you are using the cx_oracle?
The issue is due to calling a non existant method in cursor where as it should have been called in connection.
It should have been
cur = connection.cursor()
cur.execute(file(filename).read())
connection.commit()