1

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.

Kitfoxpup
  • 265
  • 1
  • 2
  • 11
  • Most likely it is returning a list of tuples. How you check for your result depends upon your query. Can you add the query to your post? – mechanical_meat Mar 11 '16 at 00:36
  • Sure, i'll add the Query I'm attempting to use to determine whether it exists. – Kitfoxpup Mar 11 '16 at 00:39
  • 1
    Not related to your question, but probably something to be aware of: unless important bits are missing, that code looks like one big SQL injection :-/ – Martin Tournoij Mar 11 '16 at 00:47
  • It's not being run from the web, it's a local script run from our server. It's also a connector- it's an include script that's being used to run those commands you see. The actual script doing things is basically just injecting using those commands, as they're not pre-existing. These were predefined functions that I have no idea how to use effectively, basically – Kitfoxpup Mar 11 '16 at 20:55
  • Well I've added more script because I don't think the "duplicate question" thing is correct. I still don't know what to do with it and I don't have reputation enough to ask on that question. – Kitfoxpup Mar 11 '16 at 21:12
  • The duplicate is correct. If you don't want to increment, then change the SQL query to do something instead of increment. The SQL in it can be executed with `cursor.execute`, as with any other SQL statement. – Colonel Thirty Two Mar 11 '16 at 23:55

0 Answers0