-6

I have a school project that involves programming. I am transferring between batch and python because that is easiest for me.

This is my code written in python:

for i,x in enumerate(result):
    if number in x:
        print "Start of 1"
        cursor.execute(insert1, (number))
        cursor.execute(delete, (number))
        f.close()
        os.system("extracommands.bat 1")
        print "Found Number"
    else:
        print "Start of 2"
        cursor.execute(insert1, (number))
        cursor.execute(insert2, (number))
        cursor.execute(delete, (number))
        f.close()
        os.system("emailfine.py 1")
        print "Finished 2"

My problem is that I can't find a string in a tuple. What happens is that when there is a result, it runs perfectly. But when there isn't a result, nothing happens.

How can I overcome this?

Thank you in advance.


Edit:

I was probably not specific enough when asking my question.

The result variable is in fact a MySQL execution result using the fetchall() command. The whole code of my program is:

import MySQLdb
import sys
import os
print "=========================="
print "Start Registered_Or_Not.py"
print "=========================="

os.chdir("C:/DisabledParkingSpacesImages/textfiles")

f = open("numberplate.txt", "r")
number = f.read()

print number

try:
    db = MySQLdb.connect(
        host = 'localhost',
        user = 'root',
        passwd = 'jordan',
        db = 'disabledparking'
        )
except Exception as e:
    sys.exit("Can't access the database")

print "MySQL Connection OK" 

cursor = db.cursor()
cursor.execute("SELECT registration FROM registered WHERE EXISTS (SELECT 1 FROM limbo WHERE limbo.registration = registered.registration)")
result = cursor.fetchall()

insert1 = "INSERT INTO log (registration, time, date) VALUES(%s, CURRENT_TIME, CURRENT_DATE);"
insert2 = "INSERT INTO not_registered (registration, time, date) VALUES(%s, CURRENT_TIME, CURRENT_DATE);"
delete = "DELETE FROM limbo WHERE registration=%s;"

print "-------------"
print "Result:"
print result
print "-------------"

TrueFalse = False

for i,x in enumerate(result):
    if number in x:
        print "Start of 1"
        cursor.execute(insert1, (number))
        cursor.execute(delete, (number))
        f.close()
        os.system("extracommands.bat 1")
        print "Found Number"
        TrueFalse = True
    elif TrueFalse == False:
        print "Start of 2"
        cursor.execute(insert1, (number))
        cursor.execute(insert2, (number))
        cursor.execute(delete, (number))
        f.close()
        os.system("emailfine.py 1")
        print "Finished 2"

db.commit()
  • This question might help you: http://stackoverflow.com/questions/1969005/enumerations-in-python?rq=1 –  Jul 19 '15 at 10:58
  • Why do you use `enumerate`? You don't do anything with `i`. – Matthias Jul 19 '15 at 11:07
  • 2
    Hey Jordan, we all understand that programming can be frustrating. Can you specify WHAT exactly your problem/challenge is? Where do you have to search a string in a tuple? How is that related to your code? Is `x` the tuple? We have to guess to many things to help you. Please provide more info. – Klaster Jul 19 '15 at 11:27
  • 1
    Even better, provide a MWE (Minimal Working Example). – Tobia Tesan Jul 19 '15 at 11:52
  • The `result` variable is in fact the result of a MySQL execution. Here is the code that runs the command: `cursor.execute("SELECT registration FROM registered WHERE EXISTS (SELECT 1 FROM limbo WHERE limbo.registration = registered.registration)")` – Jordanlegoland Jul 19 '15 at 20:35

1 Answers1

2

OK, So I answered my own question.

Here is the problematic code:

for i,x in enumerate(result):
    if number in x:
        print "Start of 1"
        cursor.execute(insert1, (number))
        cursor.execute(delete, (number))
        f.close()
        os.system("extracommands.bat 1")
        print "Found Number"
    else:
        print "Start of 2"
        cursor.execute(insert1, (number))
        cursor.execute(insert2, (number))
        cursor.execute(delete, (number))
        f.close()
        os.system("emailfine.py 1")
        print "Finished 2"

And here is the non problematic code:

for i,x in enumerate(result):
    if number in x:
        print "Start of 1"
        cursor.execute(insert1, (number))
        cursor.execute(delete, (number))
        f.close()
        os.system("extracommands.bat 1")
        print "Found Number"
        TrueFalse = True
if TrueFalse == False:
    print "Start of 2"
    cursor.execute(insert1, (number))
    cursor.execute(insert2, (number))
    cursor.execute(delete, (number))
    f.close()
    os.system("emailfine.py 1")
    print "Finished 2"