How to specifies database name in pyhdb - python

import pyhdb
connect = pyhdb.connect(
host="example.com",
port=30015,
user="user",
password="secret"
)
From the official explanation
Pyhdb only gives four parameters. Without the parameter database name, I can't understand how the system knows which database you want to connect to in this case?
And when i connect in this way, Program error:"pyhdb.exceptions.DatabaseError: authentication failed",
it looks like my password is wrong, so i let friends use JAVA(jdbc) to connect with four parameters,
it failed too, but if he add database name, it worked! so my parameters is right , and question is how to specify database name in pyhdb?
Or there are other ways to connect to Hana, Thankyou!

Looking at the __init__.py file of the pyhdb package shows that DATABASENAME is not supported when creating a connection:
[...]
def connect(host, port, user, password, autocommit=False):
conn = Connection(host, port, user, password, autocommit)
conn.connect()
return conn
[...]
The good news here is that pyhdb is not what you should be using to connect to HANA anyhow as it is the old and unsupported client library.
Use hdbcli instead as described in the documentation.
With hdbcli it's no problem at all to use the DATABASENAME:
from hdbcli import dbapi
connection =dbapi.connect(address="hxehost", port=39013, databasename="HXE", user="xxxxx", password="xxxxx")
cursor = connection.cursor()
cursor.execute("SELECT 'Hello, Python world' FROM DUMMY")
print(cursor.fetchone())
connection.close()

Related

cx_oracle connection without DSN

I am maintaining some code where i came about a curious connection string of following type to an oracle database (from redhat linux):
import cx_Oracle
cx_Oracle.Connection("username/password")
Notably no DSN is specified; User name and password are enough (the connection is working).
How is this possible? How does cx_oracle know where to connect to? Is there some default value/environment variable that is used if no DSN is given? The way I understood the documentation, the DSN is a mandatory argument.
As Devyl mentioned, if you have ORACLE_SID or TWO_TASK environment variables set, they maybe used to make a connection.
E.g. see this answer https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:89412348059
cx_oracle has method to create dsn makedsn
dsn = cx_Oracle.makedsn(host, port, sid=None, service_name=None, region=None, sharding_key=None, super_sharding_key=None)
with cx_Oracle.connect(user=user, password=password,
dsn=dsn,
encoding="UTF-8") as connection:
cursor = connection.cursor()
cursor.execute("insert into SomeTable values (:1, :2)",
(1, "Some string"))
connection.commit()

Connect to MySQL server for client failed

I'm writing an application to upload data for my customer's database. When I tested it on my PC, it works ok but when connecting to the client's database, it has some errors. I'm using this code to connect to MySQL:
def connect_sql(self):
mydb = mysql.connector.connect(
host=host,
passwd=pw,
port=port,
database=db,
user=user,
)
return mydb
When tried to connect to the database on the client's PC, it shows this error:
1115 (42000): Unknown character set: 'utf8mb4'
I've noticed that this error maybe because different database server version between me and my client. While he uses 5.0.67-community-nt (which does not support 'utf8mb4') and I use 10.0.21 MariaDB-log.
Is there any way to fix this error on my site (maybe export .sql file to set the default character set to prevent this error, etc..), or do I need to ask my customer to update his MySQL database's version?
Thanks for any help!!!
Try it as following:
def connect_sql(self):
mydb = mysql.connector.connect(
host=host,
passwd=pw,
port=port,
database=db,
user=user,
charset='utf8',
use_unicode=True
)
return mydb
Here's documentation.

Accessing MySQL DATABASE from Python hosted on cloud

I have a MySQL server installed locally and I have Python code that accesses MySQL Database and executes a simple query:
from mysql.connector import connect
from mysql.connector import ProgrammingError
DB = {
'user':'andrei',
'password':'qwertttyy',
'host':'localhost',
'port':'3306',
'db':'my_database'
}
class Connection:
instance = None
def __new__(cls):
if not cls.instance:
try:
cls.instance = connect(**DB)
except:
raise
return cls.instance
def excuteDQL(query):
cnx = Connection()
cursor = cnx.cursor()
try:
cursor.execute(query)
return cursor.fetchall()
except ProgragrammingError as err:
print('You have an error in your MySQL syntax. Please check and retry')
return []
if __name__ == '__main__':
while True:
query = input('Enter a SQL query: ')
for tuple in executeDQL(query):
print(tuple)
If I go out there and find a cloud MySQL hosting service and pay for it, the access would be as easy as changing the DB mapping with different info?
I think it should be because the connection would still be over standard TCP/IP, except, in this case, it happens to come back the same machine that is emitting. I guess, under the hood, data is packed following TCP/IP rules up to the IP layer, and then these are transferred as IP Packets from the Python process through the OS Networking API to the MySQL Server listening to the port, without further down processing into the Access Layer since the packets never leave the machine, which I understand is the purpose of the Access Layer of the TCP/IP stack, that is, to abstract the physical road the data takes.
Did I say something coherent in my guessing?
If I'm wrong, How can I put a MySQL Server in the cloud?
Yes how you connect to the database would not change. It will be as simple as changing the host name and providing whatever credentials you need ( Access Token , User info, etc). The way you insert data doesn't change once you make a connection to the DB.
Here is a good script which should provide some info: https://gist.github.com/kirang89/7161185

Connect to HANA from Python

I am trying to connect to HANA in order to pull some metadata in a pandas dataframe. There are lots of mixed approaches and I couldn't find anything concrete.
All I have is:
username
password
serverip
servername
and table names.
The admin has provided all the read access to the required account for the specific tables.
What is the quickest way to get this done? I do not have the option of installing anything on SAPs site.
I have tried the below snippets but I get the error 'target machine actively refused it' and to debug at SAPs end is a lost cause. Thank you in advance.
import pyhdb
connection = pyhdb.connect(
host="123.com",
port=123,
user="user",
password="pswrd"
)
cursor = connection.cursor()
cursor.execute("SELECT * FROM Tablename")
cursor.fetchone()
connection.close()
and
from hdbcli import dbapi
conn = dbapi.connect(
address="123.com",
port=123,
user="user",
password="pswrd"
)
cursor = conn.cursor()
Given your server address and port examples, I'm not sure you got the right idea for how to connect to a HANA database.
Since you want to use pandas it is probably a good idea to have a look at the SAP HANA Machine Learning library for Python.
Check the tutorial blog post for this:
https://blogs.sap.com/2019/11/05/hands-on-tutorial-machine-learning-push-down-to-sap-hana-with-python/
To do any of this, there is no need to install or debug anything on the HANA system.
To connect to hana DB :
from hdbcli import dbapi
conn = dbapi.connect(
address="serverhost",
port=39015,
user="UserName",
password="Password",
databasename='DBNAME'
)
Make sure you enter the correct port number
To get the sql results in pandas dataframe:
query = 'select * from table'
df = pd.read_sql_query(query, conn)
df.head()

Can you connect to a database on another pc?

I'm using MySQLdb for python, and I would like to connect to a database hosted on an other PC on the same network/LAN.
I tried the followings for host:
192.168.5.37
192.168.5.37:3306
http://192.168.5.37
http://192.168.5.37:3306
None of the above work, I always get the
2005, Unknown MySQL server host ... (0)
What might be the problem?
Code:
db = MySQLdb.connect(host="192.168.5.37", user = "root" passwd = "password", db = "test1")
You can use MySQL Connector/Python, a standardized database driver for Python.
You must provide username, password, host, database name.
import mysql.connector
conn = mysql.connector.connect(user=username, password=password,
host="192.168.5.37",
database=databaseName)
conn.close()
You can download it from: https://dev.mysql.com/downloads/connector/python/
The IP you posted are local IPs
Give it a try with your external IP (for example on this website)
https://www.whatismyip.com/
If it works with the external IP, then it's maybe a misconfiguration of your firewall.

Categories

Resources