I have been trying to connect to impala using sqlalchemy and seem to be having lots of problem. This is my code:
engine = sqlalchemy.create_engine("impala://",creator= connect(host = "....", port=21050, database="default", auth_mechanism='LDAP',user="...",password="..." ,ca_cert= "..." ))
connection = engine.connect()
The following code throws the following exception "thrift.transport.TTransport.TTransportException: TSocket read 0 bytes".
I am also not completely confident about my auth_mechanism. In the previous version it was simply "authmech=3" so not sure what to put now between these options: "{'NOSASL', 'PLAIN', 'GSSAPI', 'LDAP', 'JWT'}"
connect(): https://github.com/cloudera/impyla/blob/master/impala/dbapi.py
I have tried adjusting the connection string in many ways but seem to always be getting some sort of error. My guess is that the problem is in the connection string of the create_engine function call
Related
Im trying to connect Sybase database with flask SQLalchemy using a ODBC connection .
My connection string :
'SQLALCHEMY_DATABASE_URI' = "sybase+pyodbc://username:passw#rd#host:port/dbname?driver=Adaptive+Server+Enterprise"
Getting this Error :
But I'm pretty sure i'm using the right port in the connection string. But when i try connect to connect to the same instance using this method .
Second method :
con = pyodbc.connect(server=server ,port=port ,username=username ,password=password ,driver=driver)
The connection works perfectly fine now with the same connection details.
Can anyone help me in building the connection string URL and help me fix this. Cause I want to use the "db Object" instead of "cursor Object ".
As noted in the Getting Connected wiki page, "Hostname Connections" are not supported. You can either create an ODBC DSN, or use your pyodbc connection string with an ODBC direct pass-through connection:
import urllib
from sqlalchemy import create_engine
connection_string = (
'DRIVER=SAP ASE ODBC driver;'
'SERVER=centos7-vm01;'
'PORT=5000;'
'UID=scott;PWD=tiger;'
'DATABASE=mydatabase;'
'charset=utf8;'
)
connection_uri = f"sybase+pyodbc:///?odbc_connect={urllib.parse.quote_plus(connection_string)}"
engine = create_engine(connection_uri)
After so much research I found that, this error is due incorrect parameter in the connection string.
Invalid port number error will be also fixed with this block of code .
Code here :
import urllib
connection_string = (
'DRIVER=Adaptive Server Enterprise;'
'SERVER=server;'
'PORT=port;'
'UID=username;PWD=password;'
'DATABASE=dbname;'
)
connection_uri = f"sybase+pyodbc:///?odbc_connect={urllib.parse.quote_plus(connection_string)}"
SQLALCHEMY_DATABASE_URI = connection_uri # connection string for SQLALchemy
This code is best solution .
I am attempting to create a flask web application that uses mysql to store users and some other data. When I use the following code
import MySQLdb
def connection():
conn = MySQLdb.connect(host='localhost', user='root', passwd='password', database='data')
c = conn.cursor()
return c, conn
I get a 500 internal service error. This code will connect on the command line, and my flask site works fine until I add the line
from dbconnect import connection
I have tried using different ways to connect to the database, (python connector, pymysql). They all give the same error. I have also tried updating permissions from inside mysql to the user. I have been following tutorials from https://pythonprogramming.net/flask-connect-mysql-using-mysqldb-tutorial/ but they did not get these errors in the tutorial and I have followed most of the tutorial exactly.
I have been having major trouble connecting my python shell to my postgres. I am doing this on windows. I have downloaded psycopg2 and everything for this to process, however it still is not working.
import psycopg2
conn=psycopg2.connect("dbname = 'test' user ='postgres' host ='localhost' password = 'mypassword'")
It gives me an error telling me that the database "test" does not exist, however it does! If you guys have any advice at all on what I should test out, that would be amazing. Thank you!
You can layout connection parameters as a string and pass it to the connect() function as like:
conn = psycopg2.connect("dbname=test user=postgres password=postgres")
Or you can use a list of keyword arguments like
conn = psycopg2.connect(host="localhost",database="test", user="postgres", password="postgres")
If its still fails then you should check on PostgreSQL side. You should try to connect the db in question using command line and see if error re appears or not. if it appears then something is missing on DB server side.
I am trying to connect to Netezza using SQLalchemy.create_engine(). The reason I want to use SQLAlchmey is because I want to be able to read and write through pandas dataframe.
What works is as follow:
import pandas as pd
import pyodbc
conn = pyodbc.connect('DSN=NZDWW')
df2 = pd.read_sql(Query,conn)
Above code runs fine. But in order to write df dataframe to the Netezza, I need to use the function to_sql(), which needs SQLAlchemy. This is what my code looks like:
from sqlalchemy import create_engine
username = os.getenv('REDSHIFT_USER')
password = os.getenv('REDSHIFT_PASS')
DATABASE = "SHP_TARGET"
HOST = "Netezza1"
PORT = 5480
conn_str = "postgresql://"+username+":"+password+"#"+HOST+':'+str(PORT)+'/'+DATABASE
engine3 = create_engine(conn_str)
df = pd.read_sql(Query, engine3)
When I execute this, I get the following error:
OperationalError: (psycopg2.OperationalError) Invalid - opcode
Invalid - opcodeInvalid packet length (Background on this error at: http://sqlalche.me/e/e3q8)
Any leads will be much appreciated. thanks.
Database: Netezza
Python version: 3.6
OS: Windows
The sqlalchemy dialect for Postges isn't compatible with Netezza.
The error you're receiving is the psycopg2 module, which facilitates the connection, complaining that it can't make sense of what the server is "saying", basically.
There appears to be a dialect for Netezza though. You may want to try that out.
Here's the formal dialect for Netezza has been released.
It can be used as documented here - https://github.com/IBM/nzalchemy#prerequisites
Example
from sqlalchemy import create_engine
from urllib import parse_quote_plus
# assumes NZ_HOST, NZ_USER, NZ_PASSWORD are set
import os
params = parse_quote_plus(f"DRIVER=NetezzaSQL;SERVER={os['NZ_HOST']};"
f"DATABASE={os['NZ_DATABASE']};USER={os['NZ_USER'};"
f"PASSWORD={os['NZ_PASSWORD']}")
engine = create_engine(f"netezza+pyodbc:///?odbc_connect={params}",
echo=True)
I have a database in sqlite and followed this tutorial on how to create it. I checked, the database exists and contains values.
I entered following SQLAlchemy URI in the web interface of the superset: sqlite:///Users/me/Documents/cancellation/item/eventlog.db
and got following error:
ERROR: {"error": "Connection failed!\n\nThe error message returned
was:\n'NoneType' object has no attribute
'get_password_masked_url_from_uri'"}
I do not understand why there should be a password, if in the documentation there are not passwords specified:
http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html#sqlite
Code:
sqlite_file = 'eventlog.db' # the DB file
conn = sqlite3.connect(sqlite_file)
eventlog.to_sql('eventlog', conn, if_exists='replace', index=False)
from sqlalchemy import create_engine
>engine = create_engine('sqlite:////Users/me/Documents/cancellation/item/eventlog.db)
This issue drove me crazy for the last few days. I eventually found that you actually have to save the database config and then return to the page for the "Test Connection" to actually succeed. Attempts to use the "Test Connection" button prior to hitting Save produce the error message that you listed.