Display select Mysql query in python with output nice and readable - python

I need python script for display sql query with nice output and readable this not readable for heavy tables...
cnx = mysql.connector.connect(user='root', password='*****',
host='127.0.0.1',
database='dietetique')
c = cnx.cursor()
sys.stdout = open('mysql_data.log', 'w')
c.execute("SELECT * FROM administrations;")
for row in c:
print row

import pypyodbc
ID=2
ConnectionDtl='Driver={SQL Server};Server=WIN7-297;Database=AdventureWorks2014;trusted_connection=yes'
connection = pypyodbc.connect(ConnectionDtl)
print("Retrieve row based on [FirstName]='Mani'")
cursor = connection.cursor()
SQLCommand = ("SELECT [FirstName],[LastName] "
"FROM Person.SampleData "
"WHERE FirstName =?")
Values = ['Mani']
print(SQLCommand)
cursor.execute(SQLCommand,Values)
i=1
for x in cursor :
row = cursor.fetchone()
print str(i) + ". FirstName: " + row[0] + " LastName: " + row[1]
i=i+1
connection.close()

you can execute the same code just by adding limits to the Sql Query.
cnx = mysql.connector.connect(user='root', password='*****',
host='127.0.0.1',
database='dietetique')
c = cnx.cursor()
sys.stdout = open('mysql_data.log', 'w')
limitvalue=1000
for offsetvalue in range(0 , maximum_rows_you_want,1000):
c.execute("SELECT * FROM administrations limit "+ limitvalue + " offset " + offsetvalue +";")
for row in c:
print row

Related

Mysql / Maridb Python Connector is not loading data into table

# 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().

How to prevent 'NoneType' object is not subscriptable error' when MS SQL does not contain data in the table?

Currently I get a ''NoneType' object is not subscriptable error' if I search for data that is not in the MS SQL table.
Instead of stopping Python and outputting this error, I just want it to state that the 'data does not exist' and request another search.
Here is my current code:
cursor = connection.cursor()
SQLCommand = ("SELECT Name, Location_ID "
"FROM dbo.PB_Location "
"WHERE Location_ID = ?")
Values = [choice]
cursor.execute(SQLCommand,Values)
results = cursor.fetchone()
os.system('cls' if os.name == 'nt' else 'clear')
# note: UID column is an integer. You can't normally take an integer and place it in a string.
# so you must add str(results[2])
print (" ")
print("Name: " + results[0] + " Location ID: " + str(results[1]))
connection.close()
The below doesn't work, but would I do something close to this?
cursor = connection.cursor()
SQLCommand = ("SELECT Name, Location_ID "
"FROM dbo.PB_Location "
"WHERE Location_ID = ?")
Values = [choice]
cursor.execute(SQLCommand,Values)
while True:
results = cursor.fetchone()
if results is None:
break
os.system('cls' if os.name == 'nt' else 'clear')
# note: UID column is an integer. You can't normally take an integer and place it in a string.
# so you must add str(results[2])
print (" ")
print("Name: " + results[0] + " Location ID: " + str(results[1]))
connection.close()
Oh, I figured it out...
cursor = connection.cursor()
SQLCommand = ("SELECT Name, Location_ID "
"FROM dbo.PB_Location "
"WHERE Location_ID = ?")
Values = [choice]
cursor.execute(SQLCommand,Values)
results = cursor.fetchone()
if results:
os.system('cls' if os.name == 'nt' else 'clear')
print (" ")
print ("Name: " + results[0] + " Location ID: " + str(results[1]))
connection.close()
else:
print (" Does not exist.")
connection.close()

bypass known exception of mysql in python

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()

sqlite3 not flushing table

conn = sqlite3.connect('./mydb.db')
c = conn.cursor()
with open('./mydb_tmp.sql', 'w') as f:
for row in c.execute('SELECT * FROM FLOWS'):
print >>f, row
c.execute('DELETE FROM FLOWS;')
conn.close()
After that, all rows are still in mydb
You need to commit the command.
conn = sqlite3.connect('./mydb.db')
c = conn.cursor()
with open('./mydb_tmp.sql', 'w') as f:
for row in c.execute('SELECT * FROM FLOWS'):
print >>f, row
c.execute('DELETE FROM FLOWS;')
# Flush your commands to the db with conn.commit().
conn.commit()
conn.close()
Check the documentation : https://docs.python.org/2/library/sqlite3.html

COPY to Postgres using glob result in Python

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()

Categories