# Module Imports
import mariadb
import sys
import csv
from pathlib import Path
def connect_to_mariaDB(databse, user, passwd):
# Connect to MariaDB Platform
try: conn = mariadb.connect(
user=user,
password=passwd,
host="localhost",
port=3306,
database=databse
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
return conn
def check_if_table_exists_and_overwrite(conn, tableName, database, overwrite):
cur = conn.cursor()
cur.execute(f"SELECT table_name FROM information_schema.tables WHERE table_schema = '{database}';")
for(table_name) in cur:
if table_name[0] == tableName:
if overwrite == "YES":
print("table exists - DROP TABLE")
cur.execute(f"DROP TABLE {tableName}")
return True
else:
return False
return True
def import_file_into_db_table_(
filename, database, user, passwd, tableName,
create_table_statement = "", overwrite = False):
conn = connect_to_mariaDB(database, user, passwd)
cur = conn.cursor()
if conn != None:
print(f"Connection successful to database {database}")
if check_if_table_exists_and_overwrite(conn, tableName, database, overwrite):
cur.execute(create_table_statement)
print("table is created")
path = f"{Path().absolute()}\\{filename}".replace("\\","/")
print(path)
load_data_statement = f"""LOAD DATA INFILE '{path}'
INTO TABLE {tableName}
FIELDS TERMINATED BY ';'
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\\n'
IGNORE 1 LINES
"""
print(load_data_statement)
cur.execute(load_data_statement)
print("load data into table - successful")
else:
print("table exists - no permission to overwrite")
cur.execute("SELECT * FROM student_mat;")
for da in cur:
print(da)
# variables
filename = "student-mat.csv"
database = "dbs2021"
tableName = "student_mat"
# load the create_table_statement
create_table_statement = ""
path = f"{Path().absolute()}\\create_table_statement.txt"
with open(path, newline='') as file:
spamreader = csv.reader(file, delimiter='\n', quotechar='|')
for row in spamreader:
create_table_statement += row[0]
parameters_length = len(sys.argv)
if parameters_length == 3:
user, passwd = sys.argv[1], sys.argv[2]
import_file_into_db_table_(filename, database, user, passwd, tableName, create_table_statement, "YES")
elif parameters_length == 4:
user, passwd, overwrite = sys.argv[1], sys.argv[2], sys.argv[3]
import_file_into_db_table_(filename, database, user, passwd, tableName, create_table_statement, overwrite)
else:
print("wrong parameters\nTry -user -passwd or additional -overwrite")
The code checks if there is a table with the same name in the db and then potentially drops it, creates a new table and loads the data of the csv file into the table.
When executing the code it seems like everything is working but when going in the mariadb command prompt the created table is empty even though when outputting the table in the code it is filled.
By default MariaDB Connector/Python doesn't use autocommit mode.
You need either set autocommit=True when establishing the connection or you have to commit your changes with conn.commit().
Related
After the DatabaseError is catched (try-except block) the previous data in the table is deleted. I cannot figure out this strange behaviour.
this is the code I used
def copy_from_stringio(df, table):
conn = None
try:
# read database configuration
params = config()
# connect to the PostgreSQL database
conn = psycopg2.connect(**params)
# create a new cursor
cur = conn.cursor()
# save dataframe to an in memory buffer
buffer = StringIO()
df.to_csv(buffer, index=False, header=False,sep=';')
buffer.seek(0)
#cursor = conn.cursor()
cur.copy_from(buffer, table, sep=";")
conn.commit()
print("copy_from_stringio() done")
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
Below code is for extracting the Oracle data into xlsx format using Python,
Code run successfully but when i opened the xlsx file giving error:
"file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."
What should i change in below code to get the data without excel crash.
import xlsxwriter
from xlsxwriter import Workbook
import cx_Oracle
import datetime
from datetime import date
dsn_tns = cx_Oracle.makedsn('HOST', 'port', sid='sid')
conn = cx_Oracle.connect(user=r'username', password='password', dsn=dsn_tns)
cursor = conn.cursor()
xlsx_file = open("path.xlsx", "w")
writer = xlsxwriter.Workbook("path.xlsx")
worksheet = writer.add_worksheet()
sql ='''
SELECT *
FROM ( SELECT STRGUID,ACTIVITYUSERID,ACTIVITYSESSIONID,ACTIVITYCODE,SERVERNAME,APPNAME,STARTTIME,ENDTIME,STRDESCRIPTION,
(To_Date('12/30/1899', 'MM/DD/YYYY HH24:MI:SS')+ STARTTIME)Decoded_Date
FROM tablename
)SUB
WHERE SUB.Decoded_Date between to_date('26-APR-2020', 'DD-MON-YYYY')
and to_date('26-JUN-2020', 'DD-MON-YYYY')
'''
cursor.execute(sql)
for r, row in enumerate(cursor.fetchall()):
for c, col in enumerate(row):
worksheet.write(r, c, col)
cursor.close()
conn.close()
xlsx_file.close()
I tried below code and working successfully for exporting the data from oracle into xlsx format using python:
import xlsxwriter
from xlsxwriter import Workbook
import cx_Oracle
import datetime
from datetime import date
import os
import logging
import sys
import getopt
import traceback
## Define Function
def writeToExcel(cur_sor):
workbook = xlsxwriter.Workbook('path.xlsx')
worksheet = workbook.add_worksheet("DATA") #Add a New Worksheet Name -
for row, row1 in enumerate(cur_sor.fetchall()):
for col, col1 in enumerate(row1):
worksheet.write(row, col, col1)
workbook.close()
def setSqlCommand():
sqlCommand = '''
SELECT *
FROM
( SELECT LROWNUM,DTIMESTAMP,LSCENARIO,LYEAR,LPERIOD,
LENTITY,LPARENT,LVALUE,LACCOUNT,LICP,LCUSTOM1,
LCUSTOM2,STRUSERNAME,STRSERVERNAME,
LACTIVITY,DDATAVALUE,BNODATA,
To_Date('12/30/1899','MM/DD/YYYY') +
DTIMESTAMP as Decoded_Date
FROM tablename
) SUB
WHERE SUB.Decoded_Date
<= to_date('28-JUN-2020', 'DD-MON-YYYY')
'''
return sqlCommand
# Function to Execute Sql commands over TNS
def runSqlTNS (sqlCommand, username, password , hostName, portNumber, sID):
dsn_tns = cx_Oracle.makedsn('HOST', 'PORT', sid='SERVICEID')
db = cx_Oracle.connect(user=r'USERNAME', password='PASSWORD', dsn=dsn_tns)
cursor = db.cursor()
cursor.execute(sqlCommand)
return cursor
def main(argv=None):
#Setup Logging Information
logging.basicConfig(filename='PATH/myapp.log', level=logging.INFO, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.info('Started')
username = 'USERNAME'
password = 'PASSWORD'
#Define dsn entries to create a tns connection
hostName = 'HOST'
portNumber = 'PORT'
sid = 'SERVICEID'
#
try:
sqlCommand = setSqlCommand()
except Exception as e:
logging.info('Function - sqlCommand - In Exception')
logging.info(traceback.print_exc())
try:
c = runSqlTNS(sqlCommand, username, password , hostName, portNumber, sid)
except Exception as e:
logging.info('Function - runSql In Exception')
logging.info(traceback.print_exc())
try:
writeToExcel(c) # Send the Cursor to writetoExcel Function
c.close()
except Exception as e:
logging.info('Function - writeToExcel In Exception')
logging.info(traceback.print_exc())
if __name__ == "__main__":
main(sys.argv)
I have a table in SQLite 3 as follows and I am planning on using it to store a variety of files: txt, pdf, images and zip files.
CREATE TABLE zip (filename TEXT PRIMARYKEY NOT NULL, zipfile BLOB NOT NULL);
To store and retrieve I am experimenting with the following python code
#!env/bin/python
import sqlite3 as lite
import os
import sys
def insertfile(_filename):
try:
con = lite.connect('histogram.db', detect_types=lite.PARSE_DECLTYPES)
con.row_factory = lite.Row
cur = con.cursor()
cur.execute('PRAGMA foreign_keys=ON;')
_f = open(_filename,'rb')
_split = os.path.split(_filename)
_file = _split[1]
_blob = _f.read()
cur.execute('INSERT INTO zip (filename,zipfile) VALUES (?,?)', (_file,lite.Binary(_blob)))
_f.close()
con.commit()
cur.close()
con.close()
except Exception as ex:
print ex
def getfile(_filename):
try:
con = lite.connect('histogram.db', detect_types=lite.PARSE_DECLTYPES)
con.row_factory = lite.Row
cur = con.cursor()
cur.execute('PRAGMA foreign_keys=ON;')
cur.execute('SELECT zipfile from zip where filename = ?', (_filename,))
_files = cur.fetchall()
if len(_files) > 0:
_file = open('Test/'+ _filename,'wb')
_file.write(_files[0]['zipfile'])
_file.close()
cur.close()
con.close()
except Exception as ex:
print ex
if __name__ == '__main__':
print 'works'
insertfile(sys.argv[1])
getfile(os.path.split(sys.argv[1])[1])
When I test this on files like .txt, .py, .pdf etc., it works fine.
With Zip files, there is no error while storing into the table but an error while retrieving the file:
Could not decode to UTF-8 column 'zipfile' with text 'PK '
There seems to be some encoding or decoding issue.
I basically tried using the code from one of the questions
Insert binary file in SQLite database with Python
.
It worked originally for the pdf, png, jpg files. But I was still getting the error for Zip files. When I commented out the insertion and just ran the retrieval code it worked. Now the code below works.
def insertfile(_filename):
try:
con = lite.connect('histogram.db', detect_types=lite.PARSE_DECLTYPES)
con.row_factory = lite.Row
cur = con.cursor()
cur.execute('PRAGMA foreign_keys=ON;')
_f = open(_filename,'rb')
_split = os.path.split(_filename)
_file = _split[1]
_blob = _f.read()
cur.execute('INSERT INTO zip (filename,zipfile) VALUES (?,?)', (_file,lite.Binary(_blob)))
_f.close()
con.commit()
cur.close()
con.close()
except Exception as ex:
print ex
def getfile(_filename):
try:
con = lite.connect('histogram.db', detect_types=lite.PARSE_DECLTYPES)
con.row_factory = lite.Row
cur = con.cursor()
cur.execute('PRAGMA foreign_keys=ON;')
cur.execute('SELECT zipfile from zip where filename = ?', (_filename,))
_files = cur.fetchall()
if len(_files) > 0:
_file = open('Downloads/'+ _filename,'wb')
_file.write(_files[0]['zipfile'])
_file.close()
cur.close()
con.close()
except Exception as ex:
print ex
if __name__ == '__main__':
print 'works'
insertfile(sys.argv[1])
getfile(os.path.split(sys.argv[1])[1])
I am trying to bypass "Cannot delete or update a parent row: a foreign key constraint fails" inside my python script.
So I am planning to drop all tables but this error throws up due to inter relationship.
My query is I need to get this automated and i know I am gonna come with the same error, but I know how to bypass it by calling SET FOREIGN_KEY_CHECKS=0; and then once deleted enable the feature again SET FOREIGN_KEY_CHECKS=1;.
Need to know how to automate this inside python
import MySQLdb
import sys
if len(sys.argv) != 4:
print "please enter the Hostname to connect followed by:"
print "mysql username;"
print "mysql db to connect;"
else:
_host = sys.argv[1]
_user = sys.argv[2]
# _pass = sys.argv[3]
_db = sys.argv[3]
cham = raw_input("please enter the command to be executed:- ")
_pass = raw_input("please enter password:- ")
if cham == "drop table":
db = MySQLdb.connect(host = _host, user = _user,db = _db, passwd = _pass )
cursor = db.cursor()
cursor.execute("show tables")
for i in cursor.fetchall():
cursor.execute("drop table" + " " + (i[0]))
print cursor.fetchall()
print "all the tables has been deleted"
db.close()
else:
db = MySQLdb.connect(host = _host, user = _user,db = _db, passwd = _pass )
cursor = db.cursor()
cursor.execute(cham)
print cursor.fetchall()
db.close()
I tried the following snip and it worked, thanks anyways.
if cham == "drop table":
db = MySQLdb.connect(host = _host, user = _user,db = _db, passwd = _pass )
cursor = db.cursor()
cursor.execute("show tables")
for i in cursor.fetchall():
try:
cursor.execute("drop table" + " " + (i[0]))
#print cursor.fetchall()
except:
cursor.execute("SET FOREIGN_KEY_CHECKS=0")
cursor.execute("drop table" + " " + (i[0]))
cursor.execute("SET FOREIGN_KEY_CHECKS=1")
# print "all the tables has been deleted"
db.close()
I'm trying to create a python script that will copy a glob result into postgres. This is what I have:
import psycopg2, glob
sitesizetemp = glob.glob('C:/Data/Sheltered BLPUs/CSVs/sitesize*.csv')
conn = psycopg2.connect("dbname=postgres user=postgres")
cur = conn.cursor()
cur.execute("COPY sitesize FROM" sitesizetemp "DELIMITER ',' CSV;")
conn.commit()
cur.close()
conn.close()
I'm pretty new to all of this so any help is very welcome! thanks
import psycopg2, glob
sitesizetemp = glob.glob('C:/Data/Sheltered BLPUs/CSVs/sitesize*.csv')
conn = psycopg2.connect("dbname=postgres user=postgres")
cur = conn.cursor()
for f in sitesizetemp:
cur.execute("COPY sitesize FROM '%s' DELIMITER ',' CSV;" % f)
conn.commit()
cur.close()
conn.close()