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')
Related
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")
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.
I am running 3 consecutive and dependent SQL queries and I am wondering if my code could be more efficient. I had to create 3 separate cursors to execute my method. What can I do to make it more efficient?
What I am doing in that method is:
Insert a new contributor in my contributors table based on the values send on the form
Get the primary key of that new contribution which is it's contributor_id
Insert a new question on the questions table and the foreign key of that table is the contributor_id from the contributors table
I don't want to use an ORM such as SQLAlchemy.
conn = pymysql.connect(
host = 'localhost',
user = 'root',
passwd = 'xxx!',
db = 'xxx'
)
#app.route('/add_contributor',methods = ['POST', 'GET'])
def add_contributor():
name = request.form.get('contrib_name')
question = request.form.get('question')
sql_1 = "INSERT INTO contributors (name) VALUES (%s)"
sql_2 = "SELECT contributor_id from contributors WHERE name=(%s)"
sql_3 = "INSERT INTO questions (contributor_id, question_text) VALUES (%s, %s)"
cursor = conn.cursor()
cursor.execute(sql_1, name)
cursor.fetchall()
conn.commit()
cursor_2 = conn.cursor()
cursor_2.execute(sql_2, name)
contrib_val = cursor_2.fetchall()
contrib_id = contrib_val[0][0]
cursor_3 = conn.cursor()
cursor_3.execute(sql_3, (contrib_id,question))
cursor_3.fetchall()
conn.commit()
I am trying to query a table stored in SQL using python. I am using mysql.connector package to perform the task.
import mysql.connector
#Creating a connection
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="something",
database='mydatabase',
)
print(mydb)
##Creating a table called customers
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")
#Inserting records to the table
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = [
('Peter', 'Lowstreet 4'),
('Amy', 'Apple st 652'),
('Hannah', 'Mountain 21'),
('Michael', 'Valley 345')]
mycursor.executemany(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
The problem is when I query the database, there is no output displayed.
query=("SELECT name, address FROM customers")
mycursor.execute(query)
for (name, address) in mycursor:
print("{} {}".format(name, address))
Here is a link to what I tried and where I got the code from.
Link:https://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-select.html
Here is the confirmation that the query is storing rows in the database.
The code is running fine. The issue seems to be in the line mycursor.executemany(sql, oval) as the variable is defined as val above. Fixing that should give you the expected output.
I am trying to insert data to the table that was created earlier using python script. Here is the code I am trying to execute. I want to insert data into table with date as well.
date_today = dt.date.today()
conn = psycopg2.connect(host = serverip, port = port, database = database, user = uid, password = pwd)
cursor = conn.cursor()
cursor.execute("INSERT INTO My_TABLE (Date, Class, Total_students, failed_students, Percent_passed_students) VALUES (date_today, 'Class Name', int1, int2, int3)")
print "Data Inserted successfully"
conn.commit()
conn.close()
Here is the error I see from my job. what am i missing here?
psycopg2.ProgrammingError: column "date_today" does not exist
I created the table using different job with the following query:
cursor.execute("""CREATE TABLE MY_TABL(Date date, Lob varchar(30), Total_Students int, failed_students int, Percent_passed_students int)""")
And the table is created with above five columns.
This line:
cursor.execute("INSERT INTO My_TABLE (Date, Class, Total_students, failed_students, Percent_passed_students) VALUES (date_today, 'Class Name', int1, int2, int3)")
Is the incorrect way to dynamically insert values into a database.
Here's a functional and correct example:
cursor.execute("INSERT INTO table VALUES (%s, %s, %s)", (var1, var2, var3))
And applying it in your case...
cursor.execute("INSERT INTO My_TABLE VALUES (%s, %s, %s, %s, %s)", (date_today, 'Class Name', int1, int2, int3))