TypeError: __init__() got an unexpected keyword argument 'username' in pymysql - pymysql

I'm trying to access the database that I created with MySQL by using pymysql, but I got the error below. Does anyone have any ideas how to remove this error?
Connection = pymysql.connect(host="localhost",db="Student",port=8000,
username="makotonakai",passwd="password")
TypeError: __init__() got an unexpected keyword argument 'username'

Try this:
Port 3306 is the default port for the classic MySQL protocol
import pymysql
conn = pymysql.connect(
host="127.0.0.1", port=8000, user="makotonakai", passwd="password", db="Student"
)
cursor = conn.cursor()
cursor.execute("SELECT VERSION()")
row = cursor.fetchone()
print("Server version:", row[0])
cursor.close()
conn.close()

Change username to user and passwd to password. It should be
Connection = pymysql.connect(host="localhost",db="Student",port=8000,user="makotonakai",password="password")

Related

Is it alright to inlcude connect() inside the lambda_handler in order to close the connection after use?

I wrote one lambda function to access the MySQL database and fetch the data i.e to fetch the number of users, but any real-time update is not fetched, unless the connection is re-established.
And closing the connection inside the lambda_handler before returning, results in connection error upon its next call.
The query which I am using is -> select count(*) from users
import os
import pymysql
import json
import logging
endpoint = os.environ.get('DBMS_endpoint')
username = os.environ.get('DBMS_username')
password = os.environ.get('DBMS_password')
database_name = os.environ.get('DBMS_name')
DBport = int(os.environ.get('DBMS_port'))
logger = logging.getLogger()
logger.setLevel(logging.INFO)
try:
connection = pymysql.connect(endpoint, user=username, passwd=password, db=database_name, port=DBport)
logger.info("SUCCESS: Connection to RDS mysql instance succeeded")
except:
logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
def lambda_handler(event, context):
try:
cursor = connection.cursor()
............some.work..........
............work.saved..........
cursor.close()
connection.close()
return .....
except:
print("ERROR")
The above code results in connection error after its second time usage,
First time it works fine and gives the output but the second time when I run the lambda function it results in connection error.
Upon removal of this line ->
connection.close()
The code works fine but the real-time data which was inserted into the DB is not fetched by the lambda,
but when I don't use the lambda function for 2 minutes, then after using it again, the new value is fetched by it.
So,
In order to rectify this problem,
I placed the connect() inside the lambda_handler and the problem is solved and it also fetches the real-time data upon insertion.
import os
import pymysql
import json
import logging
endpoint = os.environ.get('DBMS_endpoint')
username = os.environ.get('DBMS_username')
password = os.environ.get('DBMS_password')
database_name = os.environ.get('DBMS_name')
DBport = int(os.environ.get('DBMS_port'))
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
try:
try:
connection = pymysql.connect(endpoint, user=username, passwd=password, db=database_name, port=DBport)
except:
logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
cursor = connection.cursor()
............some.work..........
............work.saved..........
cursor.close()
connection.close()
return .....
except:
print("ERROR")
So, I want to know, whether is it right to do this, or there is some other way to solve this problem, I trying to solve this for few-days and finally this solution is working, but not sure whether will it be a good practice to do this or not.
Any problems will occur if the number of connections to database increases?
Or any kind of resource problem?

DB connection(cx_Oracle) function calling from another function gives "AttributeError: 'NoneType' object has no attribute 'cusror'"

I have two python files and each have one function :
a.py (This function is for oracle db connection)
def db_conection(username,password,dbname,encoding):
# print(username,password,dbname,encoding)
try:
connection = cx_Oracle.connect(username, password, dbname, encoding=encoding)
# show the version of the Oracle Database
print(connection.version)
except cx_Oracle.Error as error:
print(error)
finally:
# release the connection
if connection:
connection.close()
b.py
from a import *
def set_schema(user):
con = db_conection(username,password,dbname,encoding)
cur = con.cursor()
print(user)
cur.execute("""alter session set current_schema = {}""".format(user))
cur.close()
user = "ABCDE"
set_schema(user)
The problem/error i am facing when i try to execute set_schema function(b.py)
cur = con.cursor()
AttributeError: 'NoneType' object has no attribute 'cursor'
if i just run below statment in b.py for set_schema function it works
db_conection(username,password,dbname,encoding)
The direct "solution" is:
# a.py
def db_conection(username,password,dbname,encoding):
try:
connection = cx_Oracle.connect(username, password, dbname, encoding=encoding)
# show the version of the Oracle Database
print(connection.version)
return connection
except cx_Oracle.Error as error:
print(error)
and
# b.py
from a import *
def set_schema(user):
con = db_conection(username,password,dbname,encoding)
con.current_schema = user # more efficient then executing ALTER
user = "ABCDE"
set_schema(user)
Of course, once db_connection() has finished the connection will be closed, but hopefully you just gave example code to show your problem. You may want to look at subclassing.
If you are running multi-user, you could use a connection pool and use a session callback to set session attributes.

Error while connecting pyspark to Oracle SQL?

I try to make a connection between pyspark and oracle sql, so I could load tables in this way;
I am using the following code:
from pyspark import SparkConf, SparkContext
from pyspark.sql import SQLContext, Row
import os
spark_config = SparkConf().setMaster("local").setAppName("Project_SQL")
sc = SparkContext(conf = spark_config)
sqlctx = SQLContext(sc)
os.environ['SPARK_CLASSPATH'] = "C:\Program Files (x86)\Oracle\SQL Developer 4.0.1\jdbc\lib.jdbc6.jar"
df = sqlctx.read.format("jdbc").options(url="jdbc:oracle:thin:#<>:<>:<>"
, driver = "oracle.jdbc.driver.OracleDriver"
, dbtable = "account"
, user="...."
, password="...").load()
But I get the following error.
An error occurred while calling o29.load.:
java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
Could anyone help me to fix this? Do you think it is because of the firewall?

How to connect to mongo, using ruby that have credentials?

I am trying to connect to a database that has credentials. I cannot find any useful information online...
require: 'mongo'
begin
db = Mongo::Connection.new(" IP ADDRESS " , PORT ).db("COLLECTION")
db.authenticate("username","password")
rescue StandardError => err
abort("error")
end
C:/Ruby193/lib/ruby/gems/1.9.1/gems/mongo-1.8.2/lib/mongo/networking.rb:306:in `rescue in receive_message_on_socket': Operation failed with the following exception: end of file reached (Mongo::ConnectionFailure)
looks like there is an #add_auth method as well as auths can be passed to the constructor maybe try
auths = [{"db_name" => "COLLECTION",
"username" => YOUR_USERNAME,
"password" => YOUR_PASSWORD}]
Mongo::Connection.new(" IP ADDRESS " , PORT, auths: auths)
OR
auth = {"db_name" => "COLLECTION",
"username" => YOUR_USERNAME,
"password" => YOUR_PASSWORD}
Mongo::Connection.new(" IP ADDRESS " , PORT).add_auth(auth)
and see if that works
Reference Mongo::MongoClient::GENERIC_OPTS and Mongo::MongoClient#setup
BTW that is a old version of the gem and ruby for that matter. have you considered the possibly upgrading?
Newest version (as of now) of Mongo is 2.4.3 and the options are more transparent now e.g.
Mongo::Client.new("IP_ADDRESS:PORT", user: USERNAME, password: PASSWORD, auth_mech: AUTHENTICATION_MECHANISM)
Although based on your comments I am not sure authentication is your issue

Using Oracle Service Names with SQLAlchemy

I've run into a nasty little problem connecting to an Oracle schema via SQLAlchemy using a service name. Here is my code as a script. (items between angle brackets are place holders for real values for security reasons)
from sqlalchemy import create_engine
if __name__ == "__main__":
engine = create_engine("oracle+cx_oracle://<username>:<password>#<host>/devdb")
result = engine.execute("create table test_table (id NUMBER(6), name VARCHAR2(15) not NULL)")
result = engine.execute("drop table test_table")
Where 'devdb' is a service name and not an SID. The result of running this script is the stack trace.
(oracle-test)[1]jgoodell#jgoodell-MBP:python$ python example.py
Traceback (most recent call last):
File "example.py", line 8, in <module>
result = engine.execute("create table test_table (id NUMBER(6), name VARCHAR2(15) not NULL)")
File "/Users/jgoodell/.virtualenvs/oracle-test/lib/python2.6/site-packages/sqlalchemy/engine/base.py", line 1621, in execute
connection = self.contextual_connect(close_with_result=True)
File "/Users/jgoodell/.virtualenvs/oracle-test/lib/python2.6/site-packages/sqlalchemy/engine/base.py", line 1669, in contextual_connect
self.pool.connect(),
File "/Users/jgoodell/.virtualenvs/oracle-test/lib/python2.6/site-packages/sqlalchemy/pool.py", line 272, in connect
return _ConnectionFairy(self).checkout()
File "/Users/jgoodell/.virtualenvs/oracle-test/lib/python2.6/site-packages/sqlalchemy/pool.py", line 425, in __init__
rec = self._connection_record = pool._do_get()
File "/Users/jgoodell/.virtualenvs/oracle-test/lib/python2.6/site-packages/sqlalchemy/pool.py", line 777, in _do_get
con = self._create_connection()
File "/Users/jgoodell/.virtualenvs/oracle-test/lib/python2.6/site-packages/sqlalchemy/pool.py", line 225, in _create_connection
return _ConnectionRecord(self)
File "/Users/jgoodell/.virtualenvs/oracle-test/lib/python2.6/site-packages/sqlalchemy/pool.py", line 318, in __init__
self.connection = self.__connect()
File "/Users/jgoodell/.virtualenvs/oracle-test/lib/python2.6/site-packages/sqlalchemy/pool.py", line 368, in __connect
connection = self.__pool._creator()
File "/Users/jgoodell/.virtualenvs/oracle-test/lib/python2.6/site-packages/sqlalchemy/engine/strategies.py", line 80, in connect
return dialect.connect(*cargs, **cparams)
File "/Users/jgoodell/.virtualenvs/oracle-test/lib/python2.6/site-packages/sqlalchemy/engine/default.py", line 279, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.DatabaseError: (DatabaseError) ORA-12505: TNS:listener does not currently know of SID given in connect descriptor
None None
If 'devdb' were an SID and not a service name this example would work just fine, I've been trying different permutations of the connection string but haven't found anything that works. There also does not appear to be anything in the SQLAlchemy documentation that explicitly explains how to handle SID's verses service names for Oracle connections.
I've found the answer you have to use the same connection string that would be used in a tnsnames.ora file in the connection string after the '#" like so
from sqlalchemy import create_engine
if __name__ == "__main__":
engine = create_engine("oracle+cx_oracle://<username>:<password>#(DESCRIPTION = (LOAD_BALANCE=on) (FAILOVER=ON) (ADDRESS = (PROTOCOL = TCP)(HOST = <host>)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = devdb)))")
result = engine.execute("create table test_table (id NUMBER(6), name VARCHAR2(15) not NULL)")
result = engine.execute("drop table test_table")
This example runs just fine, and you can comment out the drop statement and check the DB to see that the table was created.
import cx_Oracle
dsnStr = cx_Oracle.makedsn('myhost','port','MYSERVICENAME')
connect_str = 'oracle://user:password#' + dsnStr.replace('SID', 'SERVICE_NAME')
The makedns will create a TNS like this:
(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=myhost)(PORT=1530)))(CONNECT_DATA=(SID=MYSERVICENAME)))
replacing "SID" with "SERVICE_TYPE" got it to work for me.
If you are using flask, sqlalchemy, and oracle:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import cx_Oracle
app = Flask(__name__)
dnsStr = cx_Oracle.makedsn('my.host.com', '1530', 'my.service.name')
dnsStr = dnsString.replace('SID', 'SERVICE_NAME')
app.config['SQLALCHEMY_DATABASE_URI'] = 'oracle://myschema:mypassword#' + dnsStr
db = SQLAlchemy(app)
The module sqlalchemy now can handle oracle service_names. Have a look:
from sqlalchemy.engine import create_engine
DIALECT = 'oracle'
SQL_DRIVER = 'cx_oracle'
USERNAME = 'your_username' #enter your username
PASSWORD = 'your_password' #enter your password
HOST = 'subdomain.domain.tld' #enter the oracle db host url
PORT = 1521 # enter the oracle port number
SERVICE = 'your_oracle_service_name' # enter the oracle db service name
ENGINE_PATH_WIN_AUTH = DIALECT + '+' + SQL_DRIVER + '://' + USERNAME + ':' + PASSWORD +'#' + HOST + ':' + str(PORT) + '/?service_name=' + SERVICE
engine = create_engine(ENGINE_PATH_WIN_AUTH)
#test query
import pandas as pd
test_df = pd.read_sql_query('SELECT * FROM global_name', engine)
cx_Oracle supports the passing of a service_name to the makedsn function.
http://cx-oracle.sourceforge.net/html/module.html?highlight=makedsn#cx_Oracle.makedsn
It would be nice if the create_engine() API passed the service_name through to the underlying call it makes to makedsn...something like this:
oracle = create_engine('oracle://user:pw#host:port', service_name='myservice')
TypeError: Invalid argument(s) 'service_name' sent to create_engine(), using configuration OracleDialect_cx_oracle/QueuePool/Engine.
Please check that the keyword arguments are appropriate for this combination of components.

Resources