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()
Related
# 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().
I'm beginning using GUI . My first project is to do login system that connected to database mysql using easygui in raspberry pi 3. I already do the coding for enter the password but I dont know how to connect it with database while press ok button. While I run the coding, it will be display the password and username at the cmd. I dont know how to set it with database.
This is my coding:
import easygui as eg
msg = "Enter logon information"
title = "Demo of multpasswordbox"
fieldNames = ["User ID", "Password"]
fieldValues = [] # we start with blanks for the values
fieldValues = eg.multpasswordbox(msg,title, fieldNames)
# make sure that none of the fields was left blank
while 1:
if fieldValues == None: break
errmsg = ""
for i in range(len(fieldNames)):
if fieldValues[i].strip() == "":
errmsg = errmsg + ('"%s" is a required field.\n\n' % fieldNames[i])
if errmsg == "": break # no problems found
fieldValues = multpasswordbox(errmsg, title, fieldNames, fieldValues)
print "Reply was:", fieldValues
You're creating FieldValues as an empty list that will store your inputs. Inputs are stored in the order of your created fields, in your code:
import easygui as eg
msg = "Enter logon information"
title = "Demo of multpasswordbox"
fieldNames = ["User ID", "Password"]
fieldValues = [] # we start with blanks for the values
fieldValues = eg.multpasswordbox(msg,title, fieldNames)
userID = fieldValues[0]
password = fieldValues[1]
from easygui import *
import mysql.connector
TITLE = "Enter logon information"
conn = ""
table = ""
def Login():
global conn
conn = mysql.connector.connect(host='localhost',user='pi',password='1234',db='mydb')
cursor = conn.cursor()
msg = "Enter logon information"
title = "Login"
fieldNames = ["Usercode","Password"]
fieldValues = []
fieldValues = multpasswordbox(msg,title,fieldNames)
usercode, password = fieldValues[0], fieldValues[1]
sql = "SELECT * FROM `data` WHERE usercode = %s AND password = %s"
cursor.execute(sql,(usercode,password))
results = cursor.fetchall()
Login()
I have created a database and needs to check that one for corresponding user name and password... if it is there it should print "success" else not...
I tried many ways, it will print only if the there is a correct username and password... else it shows nothing.
It does not show can't log in or error. It only exits with 0 error without any display.
#!/usr/bin/python
import pymysql
# Open database connection
db = pymysql.connect("localhost","root","","text" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
eg = input("What's your user name? ")
password = input("What's your user password? ")
sql = ("SELECT * FROM login WHERE name = '%s' AND password= '%s' " % (eg,password))
try:
# Execute the SQL command
cursor.execute(sql)
results = None
# Fetch all the rows in a list of lists.
results = cursor.fetchall()
try:
if results is not None:
for row in results:
id = row[0]
name = row[1]
password = row[2]
print("login success")
except:
print("Error you arenot in the planet dude sign up first")
except:
print("Error you arenot in the planet dude sign up first")
# disconnect from server
db.close()
Something seems off with the indentation, try the following segment:
try:
# Execute the SQL command
cursor.execute(sql)
results = None
# Fetch all the rows in a list of lists.
results = cursor.fetchall()
try:
if results is not None :
for row in results:
id = row[0]
name = row[1]
password= row[2]
print ("login success")
except:
print("Error you arenot in the planet dude sign up first")
except:
print ("Error you are not in the planet dude sign up first")
for row in results:
id = row[0]
name = row[1]
password= row[2]
print ("login success")
Should be
success = False
for row in results:
db_id = row[0]
db_name = row[1]
db_password= row[2]
if db_name == eg and db_password == password:
success = True
print ("login success")
The variables created from row[1] and row[2] need to be different from the eq and password variables created above. Once that is done, you just need to compare them to see if they match up.
You can check the success variable later on to perform an action only if the login process was successful or not successful.
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()
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