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.
Related
This is the error:
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 'database' at line 1
Here is the code that I use to create the database:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd = "password12",
)
my_cursor = mydb.cursor()
my_cursor.execute("CREATE DATABASE database")
my_cursor.execute("SHOW DATABASES")
for db in my_cursor:
print(db)
I tried using a different name for the database, but I still got the same error.
You have an extraneous comma after your password parameter. Change your mydb connection string to the following:
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
password = 'password12'
)
my_cursor = mydb.cursor()
my_cursor.execute("CREATE DATABASE database")
my_cursor.execute("SHOW DATABASES")
for db in my_cursor:
print(db)
You should use the word password over passwd. Even though they're synonymous, according to the MySQL documentation here: Connector/Python Connection Arguments
An asterisk (*) following an argument indicates a synonymous argument
name, available only for compatibility with other Python MySQL
drivers. Oracle recommends not to use these alternative names.
Argument Name
Default
Description
user (username*)
The user name used to authenticate with the MySQL server.
password (passwd*)
The password to authenticate the user with the MySQL server.
password1, password2, and password3
For Multi-Factor Authentication (MFA); password1 is an alias for password. Added in 8.0.28.
database (db*)
The database name to use when connecting with the MySQL server.
host
127.0.0.1
The host name or IP address of the MySQL server.
Also, as mentioned in the comments above, you should not use the word database as your actual database name. I understand you may be doing it as an example, but its worth noting that it is a Reserved Keyword.
We currently use a program that creates and writes large datasets to databases, but the process can take a long time. We are trying to incorporate cursor.fast_executemany = True from sqlalchemy to improve the write times to these databases. My code errors out when I try to create an engine using SQLite3 and pyodbc here:
import pandas as pd
from sqlite3 import connect
import pyodbc
from sqlalchemy import create_engine
engine = create_engine('SQLite3 ODBC Driver+pyodbc:///C:\\Users\\Documents\\PythonScripts\\FLR.sosat')
conn = engine.connect()
c = conn.cursor()
We have tried numerous ways where we specify the driver and server and things like that like the following:
# conn = pyodbc.connect('DRIVER={SQL Server};'
# 'SERVER=localhost;'
# 'DATABASE=C:\\Users\\Documents\\PythonScripts\\FLR.sosat')
The single engine line seems to be the closest to working due to us receiving driver and server errors from the commented out code above. We have downloaded the ODBC driver from http://www.ch-werner.de/sqliteodbc/
We receive the ArgumentError: Could not parse rfc1738 URL from string.
We would appreciate any help or ideas on how to get the SQLite3 database to pyodbc and how to improve the write speed. Thanks!
Note the .sosat file is a database file that uses sqlite3, it should work like any .db file
We tried the fix from here: Connect to SQLite3 server using PyODBC, Python and that did not work for us, we received the driver error:
InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
What you are trying to accomplish will not work for two reasons:
Reason 1:
SQLAlchemy does not support pyodbc as a DBAPI layer for SQLite.
Reason 2:
Even if SQLAlchemy did support sqlite+pyodbc:// the SQLite ODBC Driver would have to support "parameter arrays", an optional ODBC feature that fast_executemany = True uses to do its magic. Not all ODBC drivers support fast_executemany = True as shown here. A quick test with vanilla pyodbc shows that "SQLite3 ODBC Driver" doesn't support it, in fact it crashes the Python interpreter:
crsr.fast_executemany = True
crsr.executemany(
f"INSERT INTO {table_name} (txt) VALUES (?)", [("foo",), ("bar",)]
)
# Process finished with exit code -1073741819 (0xC0000005)
(Error 0xC0000005 is "Access Violation".)
Have you tried
import sqlite3
db = r'C:\Users\Documents\PythonScripts\FLR.sosat'
conn = sqlite3.connect(db)
print('connection established')
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"
there are two methods to connect mysql using python,
1
import mysql.connector
cnx = mysql.connector.connect(user='scott', password='tiger',host='127.0.0.1',database='employees')
cnx.close()
2
import MySQLdb
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="john", # your username
passwd="megajonhy", # your password
db="jonhydb") # name of the data base
cur = db.cursor()
cur.execute("SELECT * FROM YOUR_TABLE_NAME")
I do not know the differences between MySQLdb and mysql connector,when should I use MySQLdb and when should I use mysql connector? Please tell me ,thanks very much.
MySQLdb is a C module that links against the MySQL protocol implementation in the libmysqlclient library. It is faster, but requires the library in order to work.
mysql-connector is a Python module that reimplements the MySQL protocol in Python. It is slower, but does not require the C library and so is more portable.
This question already has answers here:
What do I need to read Microsoft Access databases using Python?
(12 answers)
Closed 9 years ago.
Is there a library for using MS Access database in python? The win32 module is not as easy as the MySQL library. Is there a simpler way to use MS Access with Python?
Depending on what you want to do, pyodbc might be what you are looking for.
import pyodbc
def mdb_connect(db_file, user='admin', password = '', old_driver=False):
driver_ver = '*.mdb'
if not old_driver:
driver_ver += ', *.accdb'
odbc_conn_str = ('DRIVER={Microsoft Access Driver (%s)}'
';DBQ=%s;UID=%s;PWD=%s' %
(driver_ver, db_file, user, password))
return pyodbc.connect(odbc_conn_str)
conn = mdb_connect(r'''C:\x.mdb''') # only absolute paths!
Note: you may download the freely-redistributable new-driver, if you don't have MSOffice installed.
I don't think win32 is hard. Try use its odbc module. Example of code working with ODBC and PostgreSQL database:
import odbc
def get_pg_ver(db_alias):
connection = odbc.odbc(db_alias)
try:
cursor = connection.cursor()
cursor.execute('SELECT version()')
for row in cursor.fetchall():
print row[0]
finally:
connection.close()
get_pg_ver('odbc_name/user/passwd')
This is very similar for every db driver I used in Python and Jython (I work with PostgreSQL, Oracle and Informix).
You can use pypyodbc to easily create an empty Access MDB file on win32 platform, and also compact existing Access MDB files.
It can be as easy as:
import pypyodbc
pypyodbc.win_create_mdb( "D:\\Your_MDB_file_path.mdb" )
More over, as an dbi 2.0 ODBC library, pypyodbc is highly compatible with pyodbc, you can do SQL database queries like SELECT, INSERT, UPDATE with the library.
Here is the full Tutorial about pypyodbc's Access support.
Disclaimer: I'm the developer of pypyodbc.
I had some recent success using pywin32's adodbapi module.
The following snippet was taken from this website:
import adodbapi
database = "db1.mdb"
constr = 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=%s' % database
tablename = "address"
# connect to the database
conn = adodbapi.connect(constr)
# create a cursor
cur = conn.cursor()
# extract all the data
sql = "select * from %s" % tablename
cur.execute(sql)
# show the result
result = cur.fetchall()
for item in result:
print item
# close the cursor and connection
cur.close()
conn.close()