How to make the functions of necessary database operations? - python

How do I convert this code into proper function, means create table should be in seperate function, making connection should be seperate and inserting , viewing records too, I am trying but getting error, if anybody can help me:
import psycopg2
import pandas as pd
df = pd.read_csv('dataframe.csv')
# creating connection
conn = psycopg2.connect(database = "postgres",
user = "postgres",
password = "12345",
host = "127.0.0.1",
port = "5432")
print("Opened database successfully")
# Creating table stockmarketforecasting
cur = conn.cursor()
cur.execute('''CREATE TABLE STOCK_MARKET_FORECASTING
(date VARCHAR(30) NOT NULL,
open float NOT NULL,
high float NOT NULL,
low float NOT NULL,
close float NOT NULL);''')
print("Table created successfully")
# Inserting records in database
for i in range(0 ,len(df)):
values = (df['date'][i], df['open'][i], df['high'][i], df['low'][i], df['close'][i])
cur.execute("INSERT INTO STOCK_MARKET_FORECASTING (date, open, high, low, close) VALUES (%s, %s, %s, %s, %s)",
values)
conn.commit()
print("Records created successfully")
# View the records
cur.execute("SELECT * from STOCK_MARKET_FORECASTING")
rows = cur.fetchall()
for row in rows:
print(row)
print("Operation done successfully")
conn.close()
Below code is which I am trying to convert into functions but getting error, Please tell me that what thing I am doing wrong.
My code:
import psycopg2
import pandas as pd
df = pd.read_csv('2_months_dataframe.csv')
# creating connection
def create_connection():
conn = psycopg2.connect(database = "postgres",
user = "postgres",
password = "12345",
host = "127.0.0.1",
port = "5432")
print("Opened database successfully")
return conn
conn = create_connection()
# Creating table stockmarketforecasting
def create_table(conn):
conn = create_connection()
cur = conn.cursor(conn)
cur.execute('''CREATE TABLE STOCK_MARKET_FORECASTING
(date VARCHAR(30) NOT NULL,
open float NOT NULL,
high float NOT NULL,
low float NOT NULL,
close float NOT NULL);''')
print("Table created successfully")
conn.commit()
return conn, cur
conn, cur = create_table(conn)
# Inserting records in database
def insering_records(df, conn, cur):
for i in range(0 ,len(df)):
values = (df['date'][i], df['open'][i], df['high'][i], df['low'][i], df['close'][i])
cur.execute
("INSERT INTO STOCK_MARKET_FORECASTING (date, open, high, low, close) VALUES (%s, %s, %s, %s, %s)",
values)
conn.commit()
print("Records created successfully")
return cur, conn
conn, cur = insering_records(df, conn, cur)
# View the records
def viewing_records(conn, cur):
cur.execute("SELECT * from STOCK_MARKET_FORECASTING")
rows = cur.fetchall()
for row in rows:
print(row)
print("Operation done successfully")
return rows
conn.close()
rows = viewing_records(conn, cur)

I've made some corrections to your code, and it should work in this form. Let me know if you have any issues.
import psycopg2 as ps
import pandas as pd
# Here you can put your database credentials
DB_HOST = ''
DB_NAME = ''
DB_USER = ''
DB_PASS = ''
df = pd.read_csv('dataframe.csv')
# Here you create a temporary connection, that closes automatically when the code stops
with ps.connect(dbname=DB_NAME, user=DB_USER, password=DB_PASS, host=DB_HOST) as conn:
with conn.cursor() as cur:
print("Opened database successfully")
# Creating table stockmarketforecasting
cur.execute('''CREATE TABLE STOCK_MARKET_FORECASTING
(date VARCHAR(30) NOT NULL,
open float NOT NULL,
high float NOT NULL,
low float NOT NULL,
close float NOT NULL);''')
print("Table created successfully")
# Inserting records in database
for i in range(0 ,len(df)):
values = (df['date'][i], df['open'][i], df['high'][i], df['low'][i], df['close'][i])
cur.execute("INSERT INTO STOCK_MARKET_FORECASTING (date, open, high, low, close) VALUES (%s, %s, %s, %s, %s)",
values)
conn.commit()
print("Records created successfully")
# View the records
cur.execute("SELECT * from STOCK_MARKET_FORECASTING")
rows = cur.fetchall()
for row in rows:
print(row)
print("Operation done successfully")

Related

sqlite SELECT statement returns None

I have an issue returning a string from my database query.
First step was to create a database:
def create_database():
# database setup
try:
con = sqlite3.connect('db/mydb.db')
cur = con.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS user (id INTEGER NOT NULL PRIMARY KEY, balance REAL NOT NULL, text TEXT NOT NULL)')
con.commit()
con.close()
except Error as e:
print('Failed to setup database.\n' + e)
exit(1)
def get_connection():
try:
con = sqlite3.connect('db/mydb.db')
return con
except:
print('Unable to connect to database. Please try again later.\n')
exit(1)
My second step was creating a user and add him with INSERT to my database:
def create_user(user_id : int):
balance = 0.0; # base unit = USD
text = create_text()
# connect to database
con = get_connection()
cur = con.cursor()
cur.execute('INSERT INTO user (id, balance, text) VALUES (?, ?, ?)', (user_id, balance, text))
database = cur.execute('SELECT * FROM user').fetchone()
print(database)
con.commit()
con.close()
def create_text():
# do some stuff which creates my text
# the text is something like 'LNURL...'
return text
This is how the result of my database query looks like:
(393120847059091456, 0.0, 'LNURL1DP68GURN8GHJ7URP09JX7MTPDCHXGEF0D3H82UNVWQHKZURF9AMRZTMVDE6HYMP0XGMQA9V7RT')
If I try to query this database for my text it returns nothing/None. My print(text) just produces an empty new line.
def get_text(user_id : int):
# connect to database
con = get_connection()
cur = con.cursor()
cur.execute('SELECT text FROM user WHERE id=?', (user_id,))
text = cur.fetchone()
con.commit()
con.close()
print(text)
return text
I think my sqlite database used 32bit int values by default. So forcing it to use 64 bit when creating the table fixed my issue:
cur.execute('CREATE TABLE IF NOT EXISTS user (id INT8 PRIMARY KEY NOT NULL, balance REAL NOT NULL, text TEXT NOT NULL')
Than I can return my result of the query with this: return text[0]

Mysql table name getting unwanted quotes resulting table does not exist error

import mysql.connector
def add_features_to_db(stockname, timeframe, date, feature):
try:
conn = mysql.connector.connect(
user='root', password='', host='localhost', database='fx003')
cursor = conn.cursor()
dbtable = stockname + timeframe
mySql_insert_query = """INSERT INTO `%s` (date, trend) VALUES ( `%s`, `%s` )"""
record = (dbtable, date, feature)
cursor.execute(mySql_insert_query, record)
conn.commit()
print("Record inserted successfully")
except mysql.connector.Error as error:
print("Failed to insert into MySQL table {}".format(error))
finally:
if conn.is_connected():
cursor.close()
conn.close()
print("MySQL connection is closed")
add_features_to_db("aud-cad", "_30mins", "2021-09-24 21:00:00", "Short")
I have the code above and giving me the below error:
Failed to insert into MySQL table 1146 (42S02): Table 'fx003.'aud-cad_30mins'' doesn't exist
aud-cad_30mins table does exist and an insert query like below doing its job:
mySql_insert_query = """INSERT INTO aud-cad_30mins (date, trend) VALUES ( "2021-09-24 21:00:00","Short" )"""
So when I try to use variables in the query, it gives the error. Why the table name getting unwanted quotes? Checked several tutorials but couldn't find a solution, any ideas?
The table name should be hardcoded in the query string instead of having it there as a placeholder %s, which is meant for the values to be inserted. So if you have the table name in the variable, you can replace it via format() before calling cursor.execute()
dbtable = stockname + timeframe
mySql_insert_query = """INSERT INTO {} (date, trend) VALUES ( %s, %s )""".format(dbtable)
see the examples in the docs
edit: as Bill mentioned in the comment, dont add the backticks around the %s placeholders.

Failed inserting record into consumer table not all arguments converted during string formatting

I'm trying to insert data that I got from a csv file to tables that I previously created using sqlalchemy in python. However, when I try running the following code I get an error message that says that not all arguments were converted during string formatting.
Could you help me identify what my error is and how can I fix it?
#Importing the csv input file
df = pd.read_csv('APAN5310_HW6_DATA.csv')
print (df)
print (df.columns)
df.dtypes
#Splitting the data for the first table
first_name = df['first_name']
last_name = df['last_name']
email = df['email']
df[['cell_phone','home_phone']] = df.cell_and_home_phones.str.split(";",expand=True,)
cell_phone = df['cell_phone']
home_phone = df['home_phone']
consumer_list = [first_name, last_name, email, cell_phone, home_phone]
import psycopg2
def bulkInsert(records):
try:
connection = psycopg2.connect(user="postgres",
password="123",
host="localhost",
port="5432",
database="Pharmacy")
cursor = connection.cursor()
sql_insert_query = """ INSERT INTO consumer (consumer_list)
VALUES (%s,%s,%s,%s,%s) """
# executemany() to insert multiple rows
result = cursor.executemany(sql_insert_query, records)
connection.commit()
print(cursor.rowcount, "Record inserted successfully into consumer table")
except (Exception, psycopg2.Error) as error:
print("Failed inserting record into consumer table {}".format(error))
finally:
# closing database connection.
if connection:
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
records = consumer_list
bulkInsert(records)```
Error Message I get
"Failed inserting record into consumer table not all arguments converted during string formatting
PostgreSQL connection is closed"
#Javier you have to convert all arguments to columns.... as #mechanical_meat said:
""" INSERT INTO consumer (column1, column2, column3, column4, column5)
VALUES (%s, %s, %s, %s, %s) """
But I suppose that you found that in a year.

mysql does not insert correct value

I am trying to insert a new value into a database but it inserts a number that is 1 to 5 places off. for ex. (inserting 1000900 instead of 1000985). the new_stat value does not get inserted properly
query = '''SELECT `%s` FROM `memberstats` WHERE `member_id` = %s''' % (cat, mem_id)
cursor.execute(query)
for sql_stat in cursor:
if df['member_id'].str.contains(mem_id).any():
cursor1 = db.cursor()
query = '''UPDATE `memberstats` SET `%s` = %s WHERE (`member_id` = %s)''' % (cat, new_stat, mem_id)
cursor1.execute(query)
db.commit()
cursor.close()
cursor1.close()
db.close()
db = mysql.connect(
host = "localhost",
user = "root",
password = "xxxxx",
database = "xxxxx"
)
cursor2 = db.cursor()
query = '''SELECT `%s` FROM `memberstats` WHERE `member_id` = %s''' % (cat, mem_id)
cursor2.execute(query)
for new_sql_stat in cursor2:
await message.channel.send(f':white_check_mark:\n\nYour new record for *{cat}* is now: {new_sql_stat[0]}!')
Column was a FLOAT in mysql table which does not keep precision in digits over 1 million. Changed column to INT fixed it. Could have also used DECIMAL to keep precision and digit after period.

PostgreSQL creating table with Python failed

For some unknown reason table in database was not created. How to solve it?
import psycopg2
conn = psycopg2.connect(user='postgres', host='localhost', password='123456')
conn.autocommit = True
cur = conn.cursor()
cur.execute('CREATE DATABASE example;')
cur.execute("""CREATE TABLE HARMKA
(ID INT PRIMARY KEY NOT NULL,
PHARMACY_LICENSE CHAR(100),
STREET CHAR(150),
CITY CHAR(30));""")
cur.execute("INSERT INTO HARMKA VALUES(%s, %s, %s, %s)", (1, '12345', 'street', 'Denwer'))
cur.close()
conn.close()
You need to connect to example database and then create tables and manipulate data.
conn = psycopg2.connect(database='example', user='postgres', host='localhost', password='123456')

Categories

Resources