I am trying to extract my code from pydev using eclipse to postgresql. For the games table, the FK columns (TeamID and RefereeID) are showing as null. What is the solution for them to actually show the data from other tables?
Also, how can I change the serial to number between 90000 and 90500 with increments of one? Many thanks!
#!/usr/bin/python
# -*- coding: utf-8 -*-
import psycopg2
import sys
import csv
from itertools import count, cycle
from _tkinter import create
from setuptools.dist import sequence
from email.policy import default
path = r'C:\Users\sammy\Downloads\E0.csv'
with open(path, "r") as csvfile:
readCSV = csv.reader(csvfile, delimiter=",")
firstline = 1
con = None
con = psycopg2.connect("host='localhost' dbname='football' user='postgres' password='XXX'")
cur = con.cursor()
cur.execute("DROP TABLE games")
cur.execute("DROP TABLE teams")
cur.execute("DROP TABLE referees")
cur.execute("CREATE TABLE teams (HomeTeamID SERIAL PRIMARY KEY, HomeTeam VARCHAR, AwayTeamID VARCHAR, AwayTeam VARCHAR)")
cur.execute("CREATE TABLE referees (RefereeID SERIAL PRIMARY KEY, RefereeName VARCHAR, AwayTeamID VARCHAR)")
cur.execute("CREATE TABLE games (GAMEID SERIAL PRIMARY KEY, HomeTeamID INTEGER, FOREIGN KEY (HomeTeamID) REFERENCES teams(HomeTeamID), HomeTeam VARCHAR, AwayTeamID VARCHAR, AwayTeam VARCHAR, FTHG INTEGER, ATHG INTEGER, FTR VARCHAR, RefereeID INTEGER, FOREIGN KEY (RefereeID) REFERENCES referees(RefereeID), RefereeName VARCHAR, HY INTEGER, AY INTEGER)")
try:
for row in readCSV:
if firstline:
firstline=0
continue
HomeTeamID = row[2]
HomeTeam = row[2]
AwayTeamID = row[3]
AwayTeam = row[3]
FTHG = row[4]
ATHG = row[5]
FTR = row[6]
RefereeID = row[10]
RefereeName = row[10]
HY = row[19]
AY = row[20]
data1 = (HomeTeam, AwayTeamID, AwayTeam)
data2 = (RefereeName, AwayTeamID)
data3 = (HomeTeam, AwayTeamID, AwayTeam, FTHG, ATHG, FTR, RefereeName, HY, AY)
query1 = "INSERT INTO teams (HomeTeam, AwayTeamID, AwayTeam) VALUES (%s, %s, %s);"
query2 = "INSERT INTO Referees (RefereeName, AwayTeamID) VALUES (%s, %s);"
query3 = "INSERT INTO games (HomeTeam, AwayTeamID, AwayTeam, FTHG, ATHG, FTR, RefereeName, HY, AY) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s);"
print(HomeTeam)
print(AwayTeam)
print (FTHG)
print (ATHG)
print (FTR)
print (RefereeID)
print (RefereeName)
print (HY)
print (AY)
cursor = con.cursor()
cursor.execute(query1, data1)
cursor.execute(query2, data2)
cursor.execute(query3, data3)
except psycopg2.DatabaseError as e:
if con:
con.rollback()
print ("Error %s % e", e)
sys.exit(1)
finally:
if con:
con.commit()
con.close()
print(" ".join(row))
out=open("new_data.csv", "w")
output = csv.writer(out)
for row in data1:
output.writerow(row)
out.close()
Related
I'm using the following to try and insert a record into a postgresql database table, but it's not working. I don't get any errors, but there are no records in the table. I do commit at the end of my code as well. What could it possibly be? It's frustrating since I am not getting any syntax error in my code.
Excuse me for my lack of knowledge, As I am fairly new to Python.
Here is the code:
connection = psycopg2.connect(host=config.DB_HOST, database=config.DB_NAME, user=config.DB_USER, password=config.DB_PASS, port=config.DB_PORT)
cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
cursor.execute("select * from stock where is_etf = TRUE")
etfs = cursor.fetchall()
dates = ['2022-08-23', '2022-08-24']
for current_date in dates:
for etf in etfs:
print(etf['symbol'])
with open(f"new/{current_date}/{etf['symbol']}.csv") as f:
reader = csv.reader(f)
next(reader)
for row in reader:
if len(row) == 8:
ticker = row[3]
if ticker:
shares = row[5]
weight = row[7]
cursor.execute("""
SELECT * FROM stock WHERE symbol = %s
""", (ticker,))
stock = cursor.fetchone()
if stock:
cursor.execute("""
INSERT INTO etf_holding (etf_id, holding_id, dt, shares, weight)
VALUES (%s, %s, %s, %s, %s)
""", (etf['id'], stock['id'], current_date, shares, weight))
connection.commit()
as a result I get this:
This is the unpopulated table:
I'm trying to:
Update the data so that marriage=2 (single) and marriage=3 (others) are merged into 2 (single).
and:
Remove all data records with negative BILL_AMT values (in any of the BILL_AMT1 through BILL_AMT6)
The issue is that I run the code, get the all clear message, check the database and nothing seems to have changed... #1 might be working but I can't figure out how to run it using all the rows and #2 nothing seems to have changed when I look back at the database:
import csv
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="hw6"
)
mycursor = mydb.cursor()
f = open("UCI_Credit_Card.csv")
for row in csv.reader(f):
a= row[0]
b= row[1]
c= row[2]
d= row[3]
e= row[4]
f= row[5]
g= row[6]
h= row[7]
i= row[8]
j= row[9]
k= row[10]
l= row[11]
m= row[12]
n= row[13]
o= row[14]
p= row[15]
q= row[16]
r= row[17]
s= row[18]
t= row[19]
u= row[20]
v= row[21]
w= row[22]
x= row[23]
y= row[24]
sql = "INSERT INTO customers (ID, LIMIT_BAL, SEX, EDUCATION, MARRIAGE, AGE, PAY_0, PAY_2, PAY_3, PAY_4, PAY_5, PAY_6, BILL_AMT1, BILL_AMT2, BILL_AMT3, BILL_AMT4, BILL_AMT5, BILL_AMT6, PAY_AMT1, PAY_AMT2, PAY_AMT3, PAY_AMT4, PAY_AMT5, PAY_AMT6, default_payment_next_month) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
val = (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
mycursor = mydb.cursor(buffered=True)
print("Before updating a record ")
sql_select_query = """select * from customers"""
mycursor.execute(sql_select_query)
record = mycursor.fetchone()
print(record)
# Update single record now
sql_update_query = """UPDATE CUSTOMERS SET SEX = 2 where SEX = 3"""
mycursor.execute(sql_update_query)
mydb.commit()
print("Record Updated successfully ")
print("After updating record ")
mycursor.execute(sql_select_query)
record = mycursor.fetchone()
print(record)
# Delete record now
mycursor = mydb.cursor(buffered=True)
sql_update_query = "DELETE FROM customers WHERE (BILL_AMT1<0)AND(BILL_AMT2<0)AND(BILL_AMT3<0)AND(BILL_AMT4<0)AND(BILL_AMT5<0)AND(BILL_AMT6<0) """
sql_delete_query = """select * from customers"""
mycursor.execute(sql_update_query)
mydb.commit()
print("Record Delete successfully ")```
The UPDATE statement looks fine.
But when it comes to the second requirement:
Remove all data records with negative BILL_AMT values (in any of the BILL_AMT1 through BILL_AMT6)
Presumably, you want ored conditions rather than and in the delete query:
DELETE FROM customers
WHERE
BILL_AMT1 < 0
OR BILL_AMT2 < 0
OR BILL_AMT3 < 0
OR BILL_AMT4 < 0
OR BILL_AMT5 < 0
OR BILL_AMT6 < 0
I created a table with mysql.connector like this:
CREATE TABLE %s (ID int NOT NULL AUTO_INCREMENT,
date VARCHAR(200) NOT NULL default'{}',
s_time VARCHAR(30) NOT NULL default'{}',
shukkin VARCHAR(30) NOT NULL default'{}',
taikin VARCHAR(30) NOT NULL default'{}',
t_time VARCHAR(30) NOT NULL default'{}',
shucchou VARCHAR(30) NOT NULL default'{}',
shucchou_time VARCHAR(30) NOT NULL default'{}',
shucchou_kaeri_time VARCHAR(30) NOT NULL default'{}',
PRIMARY KEY (ID))" %val_s
And I'm trying to insert there and date now with this code block
now2 = datetime.datetime.now()
now = now2.strftime("%m/%d/%Y")
but when I insert to date VARCHAR(200) it becomes something like this
0.000742942050520059
And I dont know where is a problem... I tried inserting directly like this 06/04/2019 but when I selected * from table it shows same number as above.
Can someone please tell me where is a problem?
now2 = datetime.datetime.now()
now = now2.strftime("%m/%d/%Y")
now_t = now2.strftime("%H:%M:%S")
# For showing image of above settings --OPTION--
# show the output image
#cv2.imshow("Image", image)
#cv2.waitKey(0)
# SQL for "shukkin"
try:
connection = mysql.connector.connect(host='localhost', database='ninsho', user='root', password='0308', unix_socket="/var/run/mysqld/mysqld.sock")
cursor = connection.cursor()
valler = name.copy()
val_s = valler.replace(" ", "")
stmt = "SHOW TABLES LIKE '%s'" %val_s
cursor.execute(stmt)
result = cursor.fetchone()
if result:
print("je")
dates = now
# print ("date=", dates, "now=", now)
# Check if there is record from today ("shukkin")
query = "SELECT date FROM %s WHERE date = %s AND shukkin = %s" % (val_s, dates, str("'"+name+"'"))
try:
# print("rorororo")
cursor.execute(query)
myresult = cursor.fetchall()
# print(myresult)
for x in myresult:
#print("ttt")
a = x[0]
print(a)
if a == now:
# If there is record from today - Update it
names.set(name + "さん" + "\n" + "エラー:もう登録済")
memo.set("今日はすでに出勤を登録しました")
# If there is no record from today - Create it
else:
now2 = datetime.datetime.now()
now = now2.strftime("%m/%d/%Y")
val = name
val_s = val.replace(" ", "")
sql_insert_query = "INSERT INTO `%s`(`date`, `s_time`, `shukkin`) VALUES (%s, %s, %s)" % (val_s, now, now_t, name)
cursor = connection.cursor()
result = cursor.execute(sql_insert_query)
connection.commit()
#print ("Record inserted successfully into table")
except:
print("except")
now2 = datetime.datetime.now()
now3 = now2.strftime("%m/%d/%Y")
val = name
val_s = val.replace(" ", "")
sql_insert_query2 = "INSERT INTO `%s`(`date`, `s_time`, `shukkin`) VALUES (%s, %s, %s)" % (val_s, now3, str("'"+now_t+"'"), str("'"+name+"'"))
print(val_s, now3, now_t, name)
cursor = connection.cursor()
result = cursor.execute(sql_insert_query2)
print("except2")
connection.commit()
else:
print("nieje")
val = name
val_s = val.replace(" ", "")
query = "CREATE TABLE %s (ID int NOT NULL AUTO_INCREMENT, date VARCHAR(200) NOT NULL default'{}', s_time VARCHAR(30) NOT NULL default'{}', shukkin VARCHAR(30) NOT NULL default'{}', taikin VARCHAR(30) NOT NULL default'{}', t_time VARCHAR(30) NOT NULL default'{}', shucchou VARCHAR(30) NOT NULL default'{}', shucchou_time VARCHAR(30) NOT NULL default'{}', shucchou_kaeri_time VARCHAR(30) NOT NULL default'{}', PRIMARY KEY (ID))" %val_s
cursor.execute(query)
myresult = cursor.fetchall()
gettr()
except mysql.connector.Error as error :
connection.rollback() #rollback if any exception occured
#print("Failed inserting record into table {}".format(error))
finally:
if(connection.is_connected()):
cursor.close()
connection.close()
#print("MySQL connection is closed")
Im trying to export MySQL select query results to CSV file.
Im using lambda for this, and my database is in RDS.
I can able to connect and run the queries.
But when I try to save the select query results in CSV format, its not working.
Need python developers help to write this script.
Note: Unicodewritter will not work on lambda.
Lambda function:
import sys
import logging
import rds_config
import pymysql
#rds settings
rds_host = "connection_link"
name = rds_config.db_username
password = rds_config.db_password
db_name = rds_config.db_name
logger = logging.getLogger()
logger.setLevel(logging.INFO)
try:
conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=5)
except:
logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
sys.exit()
logger.info("SUCCESS: Connection to RDS mysql instance succeeded")
def handler(event, context):
"""
This function fetches content from mysql RDS instance
"""
item_count = 0
with conn.cursor() as cur:
cur.execute("create table Employee3 ( EmpID int NOT NULL, Name varchar(255) NOT NULL, PRIMARY KEY (EmpID))")
cur.execute('insert into Employee3 (EmpID, Name) values(1, "Joe")')
cur.execute('insert into Employee3 (EmpID, Name) values(2, "Bob")')
cur.execute('insert into Employee3 (EmpID, Name) values(3, "Mary")')
conn.commit()
cur.execute("select * from Employee3")
for row in cur:
item_count += 1
logger.info(row)
#print(row)
The below standard method should work to write all rows :
with conn.cursor() as cur:
cur.execute("create table Employee3 ( EmpID int NOT NULL, Name varchar(255) NOT NULL, PRIMARY KEY (EmpID))")
cur.execute('insert into Employee3 (EmpID, Name) values(1, "Joe")')
cur.execute('insert into Employee3 (EmpID, Name) values(2, "Bob")')
cur.execute('insert into Employee3 (EmpID, Name) values(3, "Mary")')
conn.commit()
cur.execute("select * from Employee3")
res = cur.fetchall()
with open('output.csv','w') as fileout:
writer = csv.writer(fileout)
writer.writerows(res)
If you need to process lines row by row, you can use the writerow instead :
cur.execute("select * from Employee3")
with open('output.csv','w') as fileout:
writer = csv.writer(fileout)
for row in cur:
writer.writerow(row)
I have the following Sqlite setup trying to update Tablename table with Keys items as both column name and value
The problem is that i want to add all items into single record where Name doesn't change but Items change to take all Items in Keys
My code will do this over for loop but with multiple records , All i want is a single records with all Items
Keys=['a','b',etc]
Tablename='Asfoor'
Name='Deek'
for item in keys:
data=[Name,Item]
cur.executemany("INSERT INTO "+Tablename+" (Name,"+Item+") VALUES(?,?)",(data,))
Complete code :
import sqlite3
import sys
tablename='asfoor'
table_data=['Name','a','b','c']
try:
con = sqlite3.connect('dbtrials.db')
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS "+tablename+"")
cur.execute("CREATE TABLE "+tablename+" (ID INTEGER PRIMARY KEY AUTOINCREMENT ,Name TEXT, "+table_data[1]+" TEXT, "+table_data[2]+" TEXT, "+table_data[3]+" TEXT )")
keys=['a','b','c']
name='deek'
for item in keys:
data=[name,item]
cur.executemany("INSERT INTO "+tablename+" (name,"+item+") VALUES(?,?)",(data,))
cursor = cur.execute("select * from "+tablename+" ")
for row in cursor :
print(row)
except sqlite3.Error as e:
print ("Error %s:" % e.args[0])
sys.exit(1)
finally:
if con:
con.close()