Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
How do I connect to mysql database from python when my password contains a "#"
Getting this error:
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'user_name'#'1.2.3.4' (using password: YES)
import mysql.connector
from urllib import parse
host = 'host.rds.xyz.com'
p = parse.quote("pass#2023")
# Connect to the database
mydb = mysql.connector.connect(
host= host,
user="user_name",
password= p
)
You can escape the special character with \ like below:
p = "pass\#2023"
or
p = "'pass#2023'"
The latter one contains the quotations ('pass#2023'). This can come in handy if you need to call a function with a string parameter with quotes.
sorry..this is very embarrassing...my account was locked
I was able to connect without any parsing/escape..
The error deceived me as I had to escape # while connecting to Oracle..I thought mysql worked the same..but it seems mysql.connector takes care of special chars...
import mysql.connector
host = 'host.rds.xyz.com'
p = "pass#2023"
# Connect to the database
mydb = mysql.connector.connect(
host= host,
user="user_name",
password= p
)
Related
This question already has answers here:
Writing a connection string when password contains special characters
(2 answers)
Closed 12 months ago.
I am trying to connect pandas to MySQL DB to write to the tables. My dummy database credentials are:
db_name="ai_db_dev"
db_user="ai_db_user_dev"
db_pwd="abassc#xasas12yz"
db_host="db.rds.amazonaws.com"
db_client="mysql"
I am connecting to the db with fake SSL flag like this:
db_connection_str=db_client+"://"+db_user+":"+db_pwd+"#"+db_host+"/"+db_name
connect_args={'ssl':{'fake_flag_to_enable_tls': True},
'port': 3306}
db_connection = create_engine(db_connection_str,connect_args= connect_args)
and I am trying to insert the db into the table like this:
df.to_sql(con=db_connection, name='ai_table', if_exists='append', index= False)
But due to the '#' in the password, it is not picking up the host properly and hence I am unable to connect to the DB. Any help in solving this issue will be highly appreciated!
SQLAlchemy has a built-in URL.create() method that will build a properly-escaped URL for you:
import sqlalchemy as sa
db_name="ai_db_dev"
db_user="ai_db_user_dev"
db_pwd="abassc#xasas12yz"
db_host="db.rds.amazonaws.com"
db_client="mysql"
connection_url = sa.engine.URL.create(
drivername=db_client,
username=db_user,
password=db_pwd,
host=db_host,
database=db_name
)
print(connection_url)
# mysql://ai_db_user_dev:abassc%40xasas12yz#db.rds.amazonaws.com/ai_db_dev
If you enclose your username and password by simple quotes?
db_connection_str = f"{db_client}://'{db_user}':'{db_pwd}'#{db_host}/{db_name}"
>>> db_connection_str
"mysql://'ai_db_user_dev':'abassc#xasas12yz'#db.rds.amazonaws.com/ai_db_dev"
Attempt 2
from urllib.parse import quote
db_connection_str = f"{db_client}://{db_user}:{quote(db_pwd)}#{db_host}/{db_name}"
Output:
>>> db_connection_str
"mysql://'ai_db_user_dev':'abassc%40xasas12yz'#db.rds.amazonaws.com/ai_db_dev"
Thank you for taking the time to look at my question!
I have a class ODBCSources which returns a SQLAlchemy/pyodbc engine for use with pretty much anything you could want it for. While logged in as a user with database editing permissions and using windows authentication, the code works for some databases on myserver.mycompany.local but not one database in particular. The problem database was created a week or so ago vs years ago for the ones that I can connect to with pyodbc. As the same user, I am able to log into the all of the databases using windows authentication + SSMS, Azure Data Studio, and/or DataGrip.
My code works for other databases on the same server, so I don't think the issue is with my connection string (But, the connection string must have a database name component, not an ODBC name component -- See my answer below). Additionally, the MS ODBC Data Source Administrator gives the feel good 'TESTS COMPLETED SUCCESSFULLY!' message when I test the connection corresponding to the problem database.
Is there a SQL-Server setting that I need to enable to allow python connections to the problem database? (No -- See my answer below)
import getpass
import pyodbc
from sqlalchemy import create_engine
class ODBCSources():
"""
A class to return sql alchemy engine for use in something like `pd.to_sql`
"""
def __init__(self, login = False):
self.sources = pyodbc.dataSources().keys()
self.login = login
self.user = ""
self.pwd = ""
def make_engine(self, odbc_source, driver = 'ODBC Driver 17 for SQL Server'):
if odbc_source not in self.sources:
raise ValueError(f'{odbc_source} is not available.\nAvailable Sources: {self.sources}')
else:
if self.login:
# This just gets the currently logged in user. Not really much more flexible
# than windows auth, but I'm not concerned with this
user = getpass.getuser()
pwd = getpass.getpass()
engine = create_engine(f'mssql+pyodbc://{user}:{pwd}#myserver.mycompany.local/{odbc_source}?driver={driver}', echo = False)
else:
user = self.user
pwd = self.pwd
engine = create_engine(f'mssql+pyodbc://{user}:{pwd}#myserver.mycompany.local/{odbc_source}?driver={driver}', echo = False)
return engine
I resolved the issue by creating a new ODBC datasource having a the same name as my database . . . which was really not necessary at all, but led me to the root cause root cause of my issue: my connection string is suitable for connection via database names, not ODBC names.
It so happened that in the past I created all of my ODBC connections with the same name as their corresponding database, so I was able to get by thinking that my connection string in create_engine had an ODBC component (it does not), because I was actually entering correct database names.
This question already has answers here:
Authentication plugin 'caching_sha2_password' is not supported
(28 answers)
Closed 1 year ago.
trying to create a table on server in pycharm using mysql
import mysql.connector
db = mysql.connector.connect(
host='localhost',
user='root',
passwd='root',
database='Local instance MySQL80',
auth_plugin='mysql_native_password'
)
mycursor = db.cursor()
mycursor.execute('CREATE TABLE Shirt(name VARCHAR(50),price smallint, size smallint, serialnumber int PRIMARY KEY AUTO_INCREMENT)')
but keep getting the same error
mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supported
You installed a deprecated mysql-connector version. Please use mysql-connector-python instead.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Im trying to conennect to an sql database that its already created an that its located on a server. How can I connect to this database using python. Ive tried using java but I cant seem to get working either.
Well depending on what sql database you are using you can pip install pymssql for microsoft sql (mssql), psycopg2 for postgres (psql) or mysqldb for mysql databases
Here are a few examples of using it
Microsoft sql
import pymssql
conn = pymssql.connect(server=server, user=user, password=password, database=db)
cursor = conn.cursor()
cursor.execute("SELECT COUNT(MemberID) as count FROM Members WHERE id = 1")
row = cursor.fetchone()
conn.close()
print(row)
Postgres
import psycopg2
conn = psycopg2.connect(database=db, user=user, password=password, host=host, port="5432")
cursor = conn.cursor()
cursor.execute('SELECT COUNT(MemberID) as count FROM Members WHERE id = 1')
row = cursor.fetchone()
conn.close()
print(row)
mysql
import MySQLdb
conn = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
cursor = conn.cursor()
cursor.execute('SELECT COUNT(MemberID) as count FROM Members WHERE id = 1')
row = cursor.fetchone()
conn.close()
print(row)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
def showallres():
sql = '''SELECT ResidentID,FirstName,SurName,Age,MDisability,History,Impairment,Money,Contact
FROM tblResidentM'''
results = run_sql2(sql)
print(results)
return results
for some reason it just prints 'None'? But it worked before. The table and all the fields are named correctly so I am unsure of what it is.
Here is the code for 'run_sql2'
def run_sql2(sql):
db = db_connect()
c = db.cursor()
c.execute(sql)
results=c.fetchall()
db.commit()
c.close()
db.close()
I am connecting to an online mysql database.
db_connect is as follows
def db_connect():
try:
db = mysql.connector.connect(user = 'user', password = 'pass', host = 'host', database = 'db', port = 'port')
print('connected')
return db
except mysql.connector.Error as error:
print(error)
Your function run_sql2() does not return anything,
in Python a function returns None by default, that's why results is None
def run_sql2(sql):
db = db_connect()
c = db.cursor()
c.execute(sql)
results = c.fetchall()
db.commit()
c.close()
db.close()
return results # <---- you must return the results