All right, this is a weird one. I know there are ways in PHP to do this, but this is via a connector that is connecting from an ancient database called "PICK" to MySQL. Essentially, I need to determine if a model number exists in the model number column of the cm table, so that if it doesn't, I can INSERT the model number.
Big issue is that the people who came before me programmed all of this in Python, which is a bit stranger considering they've got a custom connector. Here are the commands it uses:
def Select(self, what, where, how="", extended="", doup=False):
query = "SELECT " + what + " FROM " + where
if how is not "":
query += " WHERE " + how
if query is not "":
query += " " + extended
try:
if doup:
self.loopCursor.execute(query)
return self.loopCursor.fetchall()
else:
self.cursor.execute(query)
return self.cursor.fetchall()
except:
return False
def Update(self, where, what, how="", extended="", doup=False):
query = "UPDATE " + where + " SET " + what
if how is not "":
query += " WHERE " + how
if query is not "":
query += " " + extended
try:
if doup:
self.loopCursor.execute(query)
return self.loopCursor.fetchall()
else:
self.cursor.execute(query)
# print "Number of rows updated: %d" % cursor.rowcount
return self.cursor.fetchall()
except:
return False
def Insert(self, where, what, how):
query = "INSERT INTO " + where + " (" + what + ")" + " VALUE (" + how + ")"
self.Query(query)
I'm not sure I understand how to reference what a SELECT statement would return, or how python would handle this. Would the select statement return a string? An array? A list? Should I make a variable equal the select statement, and then check if the variable is empty?
Edit: There is no commentation on these commands to explain them. Super frustrating.
Edit edit: Requested to put the query I'm attempting to use to determine if the model numer exists. I was considering just using a SELECT to see if it returns an empty or anything at all, but where I'd ask if the mysql_num_rows == 0 in PHP, I'm not sure how to do the equivalent in Python.
SELECT COUNT(modelNumber) FROM commoditymasters WHERE modelNumber = 1234;
or
SELECT * FROM commoditymasters WHERE modelNumber = 1234;
I guess if I do COUNT, it'll return an int, and if it's greater than 0 that means it exists... Right?
EDIT EDIT in regards to someone saying this is a duplicate:
I do not want to incrament anything. It looks like the answer to the question I've been directed to uses the following MySQL code as a possible answer:
INSERT INTO `usage`
(`thing_id`, `times_used`, `first_time_used`)
VALUES
(4815162342, 1, NOW())
ON DUPLICATE KEY UPDATE
`times_used` = `times_used` + 1
Which is great, that sort of does what I need, but I need two more things:
A) Not to incrament. There is no field I wish to add to when this is called because we run this script at the end of the day every day, incramenting a counter is useless information and a waste of space.
B) Put into the python commands below. I'm STILL not sure how to put this in Python. I cannot run it directly from MySQL, as it's running based off of information gathered from a text document being accessed by these Python scripts. If you'd like to see what we're basically doing in these scripts:
from FOLDER import Connector as conn
import os, sys, datetime, math, re, decimal
arg = ""
if len(sys.argv) > 1:
arg = sys.argv[1]
with open('./logs/dbQueryLog', 'w') as newQueryLog: ##DEVNOTE: Create and/or truncate dbQueryLog file in preparation for capturing the output of loop
newQueryLog.close() ##
def UpdateField(item):
if str(item[0]).upper() in models:
for i in range(len(naming)):
if i == 0:
updateString = ""
#if i == 19: #update FBA in amazonitems, determined by whether LOC is "AMZ" or not
if i == 38: #update amazonitems, determined by whether FBA (item[38]) is 1 or not
#if item[i] == 1: # was AMZ for LOC when i was 19
db.Update('amazonitems', "FBA = '" + item[i] + "'", "SKU='" + str(item[0]).upper() + "'")
if i == 39:
db.Update('amazonitems', "FBAPrice = '" + str(item[i]).upper() + "'", "SKU='" + str(item[0]).upper() + "'")
Don't ask why the spacing is so weird. The script required it. I tried cleaning it up and Python yelled at me. I freaking hate this language sometimes. We're ditching this code in a few months or so, but for now, it needs to work, and I'm at a loss.