Connecting to Postgres sql with Python - python

I am running postgressql on a docker container. I am trying to connect to postgres via python and display the tables below is the code that I am using to connect to postgres:
import psycopg2
conn_string = "host='192.168.99.100:15432' dbname='PREDICTIVE_DS_POSTGRESQL'
user='ds_user' password='ds_user'"
print("Connecting to database\n ->%s" % (conn_string))
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
print("Connected!\n")
Then I use the below Python code to display the existing tables within postgres:
def table_exists(con, table_str):
exists = False
try:
cur = con.cursor()
cur.execute("select exists(select relname from pg_class where relname='"
+ table_str + "')")
exists = cur.fetchone()[0]
print("exists")
cur.close()
except psycopg2.Error as e:
print(e)
return exists
def get_table_col_names(con, table_str):
col_names = []
try:
cur = con.cursor()
cur.execute("select * from " + table_str + " LIMIT 0")
for desc in cur.description:
col_names.append(desc[0])
cur.close()
except psycopg2.Error as e:
print(e)
However, it is not working at all. It says that it cannot connect translate host name "192.168.99.100:15432" to address: Unknown host. However, the container is up and running and that is the host name. Additionally, I don't know whether the rest of the code will work once it connects.

Have your database credentials defined in a separate file.
For example, have a file called database.ini and define it like this:
[creds]
host=192.168.99.100
port=15432
database=PREDICTIVE_DS_POSTGRESQL
user=ds_user
password=ds_user
Have another config parser file to parse this. Call it config.py
#!/usr/bin/python
try:
import configparser
except:
from six.moves import configparser
def config(section,filename='database.ini',):
parser = configparser.ConfigParser()
parser.read(filename)
db = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1}
file'.format(section, filename))
return db
Now, in your main file, import your config function like this:
from config import config
and connect like this:
dbParams = config("creds")
con = psycopg2.connect(**dbParams)

Related

AttributeError: module 'mysql.connector' has no attribute 'connect' using mysql-python-connector

actually I was using mysql-python connector using code below
cnx = mysql.connector.connect(user='root', password='passw',
host='localhost',database='dhruv_12')
cnx.close()
but when I executed the above code, it shows a error as below
File ~\Desktop\I.P class 12\practical file class 12\untitled0.py:3 in <module>
cnx = mysql.connector.connect(user='root', password='tiger',
AttributeError: module 'mysql.connector' has no attribute 'connect'
I reinstalled python , reinstalled python-mysql-connector using pip and even tried changing file name but nothing happen .
I executed same code in python IDLE , it worked properly . But it doesn't worked in spyder . My spyder version is up to date . Please help me with this , I really need help because of my project
at school. Please help me as soon as possible , Its my request....
Try importing connect as shown below.
Use a with statement to automatically close the connection.
"""Demonstrates creating a database connection and inserting and reading data."""
from mysql.connector import connect, Error
class SqlTest:
"""Demo class to connect, insert, and query data from a database."""
def __init__(self, db_host, db_port, db_name, db_user_name, db_password):
"""Initialize object properties."""
# Fields
self._db_port = db_port
self._db_name = db_name
self._db_host = db_host
self._db_user_name = db_user_name
self._db_password = db_password
self.db_connection = None
# Constants
self.SELECT_ALL = 'SELECT id, item, count FROM items'
self.INSERT = 'INSERT INTO items (item, count) VALUES(%s, %s)'
def insert_item(self, item, count):
try:
with connect(
host=self._db_host,
user=self._db_user_name,
password=self._db_password,
database=self._db_name,
port=self._db_port
) as connection:
cursor = connection.cursor()
cursor.execute(self.INSERT, (item, count))
connection.commit() # Very important!
cursor.close()
except Error as e:
print(e)
def query_all(self):
results = None
try:
with connect(
host=self._db_host,
user=self._db_user_name,
password=self._db_password,
database=self._db_name,
port=self._db_port
) as connection:
cursor = connection.cursor()
cursor.execute(self.SELECT_ALL)
results = cursor.fetchall()
except Error as e:
print(e)
return results

I'm having trouble creating an API Post using Python flask, SQL workbench and Postman

I have a table named 'gem' in SQL Workbench and already have a working connection. When I try to add another gem using Postman, I get an error "tuple" object has no attribute 'encode'. I'm still a beginner and can't tell what I'm doing wrong. Can anyone help me?
These are my imports for the files I used. 'creds' is a file with my credentials to connect with my username and password to my db. 'sql' is the file that has the connection to MYSQL Workbench.
import flask
from flask import jsonify
from flask import request
from sql import create_connection
from sql import execute_read_query
import creds
This is my sql file for the connection the db
import mysql.connector
from mysql.connector import Error
def create_connection(host_name, user_name, user_password, db_name):
connection = None
try:
connection = mysql.connector.connect(
host=host_name,
user=user_name,
passwd=user_password,
database=db_name
)
print("Connection to MySQL DB successful")
except Error as e:
print(f"The error '{e}' occurred")
return connection
def execute_read_query(connection, query):
cursor = connection.cursor(dictionary=True)
result = None
try:
cursor.execute(query)
result = cursor.fetchall()
return result
except Error as e:
print(f"The error '{e}' occurred")
#app.route('/api/gem', methods=['POST'])
def add_example():
request_data = request.get_json()
newid = request_data['id']
newgemtype = request_data['gemtype']
newgemcolor = request_data['gemcolor']
newcarat = request_data['carat']
newprice = request_data['price']
myCreds = creds.Creds()
conn = create_connection(myCreds.conString, myCreds.userName, myCreds.password, myCreds.dbName)
sql = "INSERT INTO gem(id, gemtype, gemcolor, carat, price) VALUES(%s, %s, %s, %s, %s)", (newid, newgemtype, newgemcolor, newcarat, newprice)
gem = execute_read_query(conn, sql)
results = []
gem.append({'id': newid, 'gemtype': newgemtype, 'gemcolor': newgemcolor, 'carat': newcarat, 'price': newprice})
return 'add request successful'
cursor.execute expects either a single argument - a string - or two arguments, a string and a sequence of parameters.
sql = "INSERT INTO gem(id, gemtype, gemcolor, carat, price) VALUES(%s, %s, %s, %s, %s)", (newid, newgemtype, newgemcolor, newcarat, newprice) is a single tuple. cursor.execute recognises that it has received a single argument and tries to encode it, leading to the error.
The solution is to unpack the arguments:
cursor.execute(*query)
or pass them separately:
cursor.execute(sql_string, params)

Logging SQL Exceptions on Jupyter Notebook With mysql.connector

I'm coding up a Python file that inserts rows into a MySQL table from a dataframe using mysql.connector. I'm trying to log each successful request and also the exceptions- I'm using Jupyter Notebook to execute the program. However, I'm not able to see any logging on the notebook- I have manually to go into the MySql database and check what has gone in, and I have no idea which entries haven't been successfully entered. Here is my code:
import os
import pandas as pd
import mysql.connector
import logging
import math
def upload_to_db(host, database, user, password,
tbl_name, col_str, dataframe):
try:
conn = mysql.connector.connect(
host=host, database=database, user=user, password=password)
cursor = conn.cursor()
cursor.execute("drop table if exists %s;" % (tbl_name))
cursor.execute("create table %s (%s);" % (
tbl_name, col_str))
data_list = dataframe.to_numpy().tolist()
for i in range(0, len(data_list)-1):
row_values = convert_list(data_list[i])
sql_statement = 'INSERT INTO %s VALUES (%s);' % (
tbl_name, row_values)
cursor.execute(sql_statement)
logging.info("SQL statement [" + sql_statement + "] successful")
conn.commit()
cursor.close()
except mysql.connector.Error as err:
logging.info("Exception: {}".format(err))
Why doesn't the python logging class show the exceptions or successes on the Notebook?

Accessing local Postgresql server from Jupyter notebook

I have a postgresql database "Test2" hosted in my localhost. I am able to see the tables using pgAdmin. I want to fetch the data of the DB from Jupyter Notebook. I tried to connect to the DB by following the steps shown in "2) of Part 2" of https://towardsdatascience.com/python-and-postgresql-how-to-access-a-postgresql-database-like-a-data-scientist-b5a9c5a0ea43
Thus, my code is --
import config as creds
import pandas as pd
def connect():
# Set up a connection to the postgres server.
conn_string = "host="+ creds.PGHOST +" port="+ "5432" +" dbname="+ creds.PGDATABASE +" user=" + creds.PGUSER \
+" password="+ creds.PGPASSWORD
conn = psycopg2.connect(conn_string)
print("Connected!")
# Create a cursor object
cursor = conn.cursor()
return conn, cursor
#Connecting to DB
conn, cursor = connect()
#SQL command to create inventory table
abc = ("""SELECT * FROM clubs""")
#Execute SQL Command and commit to DB
cursor.execute(abc)
results = cursor.fetchall()
print(results)
conn.commit()
My config.py looks like this -->
PGHOST = 'localhost'
PGDATABASE = 'Test2'
PGUSER = '#####'
PGPASSWORD = '#####'
I able to get the output when the table name has all lowercase characters but for table names which has mixed character like "clubCategory", it throws an error stating "relation "clubcategory" does not exist"
I tried
abc = ("""SELECT * FROM 'clubCategory' """)
but its still throws error.
Any help please?
Try using double quotes:
abc = ('''SELECT * FROM "clubCategory" ''')
Also see this answer: https://stackoverflow.com/a/21798517/1453822

python MySQLdb won't INSERT

I'm using python-mysql, attacked below is the code snippet I'm using to insert into a database table. For some reasons, the code is not populating any rows in the database. There are no exceptions raised and the SELECT queries work fine. On copying the code inside execute and running in phpmyadmin, the database is populated fine.
import MySQLdb as mdb
try:
con = mdb.connect(host='localhost', user='', passwd='', db='indoor')
cur = con.cursor()
cur.execute("INSERT INTO locationdata VALUES('1','1','1','1','1','1')")
numrows = cur.execute("SELECT * FROM locationdata")
print str(numrows) + " : total Rows"
print cur.fetchone()
if con.open:
print "Hello DB"
except mdb.Error, e:
Print "Error " + e. args [0]
Any ideas what am I missing?

Categories