Question: Would do I get 'int' object is not iterable error when I run the program below?: import sqlite3 import csv def main(): sql = sqlite3.connect('car.db')
Would do I get 'int' object is not iterable error when I run the program below?: import sqlite3 import csv def main(): sql = sqlite3.connect('car.db') sql.text_factory = str cur = sql.cursor() # manualTester(cur)#temporary for verification of different types tester(cur)#'int' object is not iterable askCommandContinue(cur)
def parser(string, cur): output = None string = string.split() length = len(string) #tblCarMake = pmkMake, tblModel = fnkMake done=0 if length == 0: print("sorry, you have to enter A command") elif length == 1: if string[0].lower() == "make": #command = make # output = cur.execute("SELECT DISTINCT pmkMake FROM tblCarMake") output = cur.execute("SELECT pmkMake FROM tblCarMake") elif string[0].lower() == "model":#command = model output = cur.execute("SELECT DISTINCT fldModel FROM tblModel") elif string[0].lower() == "ceo": #command = ceo output = cur.execute("SELECT pmkMake, fldCEO FROM tblCarMake") elif string[0].lower() == "country": #command = country output = cur.execute("SELECT pmkMake, fldCountry FROM tblCarMake") elif string[0].lower() == "hmpg": #command = hmpg output = cur.execute("SELECT DISTINCT fldHighWay_MPG FROM tblModel") elif string[0].lower() == "cmpg": #command = cmpg output = cur.execute("SELECT DISTINCT fldCity_MPG FROM tblModel") elif string[0].lower() == "year": #command = year output = cur.execute("SELECT DISTINCT fldYear FROM tblModel") elif string[0].lower() == "done": done=1 else: #doesn't recognize the command pass ############################################################# #weird exception handling elif length > 1 and string[0].lower() == 'make': models = list(cur.execute("SELECT DISTINCT fldModel FROM tblModel")) newModelList = [] for i in models: newModelList.append(i[0]) modelStr = ' '.join(string[1:]) if modelStr in newModelList: output = cur.execute("SELECT fnkMake, fldModel FROM tblModel WHERE fldModel = :model", {"model": modelStr}) ############################################################# elif length == 2: #first deal with CEO ##need to somehow validate the makes if string[0].lower() == "ceo": #command = ceo MAKE output = cur.execute("SELECT pmkMake, fldCEO FROM tblCarMake WHERE pmkMake = :make", {"make": string[1]}) elif string[0].lower() == "country": #command = country MAKE output = cur.execute("SELECT pmkMake, fldCountry FROM tblCarMake WHERE pmkMake = :make", {"make": string[1]}) elif string[0].lower() == "hmpg": #command = hmpg MAKE output = cur.execute("SELECT fldHighWay_MPG, fnkMake FROM tblModel WHERE fnkMake = :make", {"make": string[1]}) elif string[0].lower() == "cmpg": #command = cmpg MAKE output = cur.execute("SELECT fnkMake, fldCity_MPG FROM tblModel WHERE fnkMake = :make", {"make": string[1]}) elif string[0].lower() == "model": #command = model MAKE output = cur.execute("SELECT DISTINCT fldModel FROM tblModel WHERE fnkMake = :make", {"make": string[1]}) elif string[0].lower() == "year": #command = year MAKE output = cur.execute("SELECT fldYear, fnkMake, FROM tblModel WHERE fnkMake = :make", {"make": string[1]}) else: #there is an error pass elif length >= 3: ############################################################# models = list(cur.execute("SELECT DISTINCT fldModel FROM tblModel")) newModelList = [] for i in models: newModelList.append(i[0]) modelStr = ' '.join(string[2:]) ############################################################# if string[0].lower() == "hmpg" and modelStr in newModelList: #command = hmpg MAKE MODEL output = cur.execute("SELECT fldHighWay_MPG, fnkMake, fldModel FROM tblModel WHERE fnkMake = :make AND fldModel = :model", {"make": string[1], "model": modelStr}) elif string[0].lower() == "cmpg": #command = cmpg MAKE MODEL output = cur.execute("SELECT fldCity_MPG, fnkMake, fldModel FROM tblModel WHERE fnkMake = :make AND fldModel = :model", {"make": string[1], "model": modelStr}) elif string[0].lower() == "year": #command = year MAKE MODEL output = cur.execute("SELECT fldYear, fnkMake, fldModel FROM tblModel WHERE fnkMake = :make AND fldModel = :model", {"make": string[1], "model": modelStr}) else: #something wrong pass ##simple print function for i in output: print(i) return done def tester(cursor): str_to_test = ['make','model','ceo','country','hmpg','cmpg','year','ceo Audi', 'country Audi', 'hmpg Audi', 'cmpg Audi', 'model Audi', 'year Audi'] for string in str_to_test: xy = parser(string, cursor) for i in xy: print(i) def manualTester(cur): parser('cmpg BMW M4', cur) parser('hmpg BMW Z3', cur) parser('country BMW', cur) parser('ceo BMW', cur) parser('model BMW', cur) parser('make 124 Spider', cur) def askCommandContinue(cur): input_done=0 while (input_done<1): try: print(" Examples of valid commands: " ) print(" cmpg BMW M4") print(" country BMW") print(" ceo BMW") print(' model BMW') print(' make 124 Spider') commandLine=input("Do you want to query again (Y for yes, N for no)?: ") if commandLine.lower()=="y": commandLine=input("Enter what you want to query next: ") input_done=parser(commandLine, cur) else: input_done=1 print("Manual Command Line entries completed") except: print("Abnormal Manual Command Line entry")
main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
