I have this issue with attempting to use recursion in my SQLITE table to automatically insert multiple related words of word objects I created. /u/_User15 Python Education

So I have two recursive functions. One called printall() that works but is kind of meant for my internal use. The second automate() also works when I use print() to see if it recurses and that’s pretty much a copy of it, but I want to have it do recursion and use SQL to insert word values that way into the “related” column of my table which only has two columns: “word” and “related”.

Here’s the full code. Some code is marked by # because I use it for later or have already used it.

import sqlite3 words = sqlite3.connect('words.db') #our database is connected to-immediately. table_name = "words" #name the table_name as words too since our database only has this table. word_cursor = words.cursor() #create a cursor: word_cursor = words.cursor() class Words: def __init__(self, word_node): self.word_node = word_node self.related = None self.start = True self.tostring = f"{self.word_node} -> " def relate(self, concept): if self.related is None: self.related = dict() obj = Words(concept) obj.tostring = self.tostring + f"{obj.tostring}" self.related[concept] = obj def common(self, node2): connected = [] if self.word_node != node2.word_node: for node1Key in self.related.keys(): for node2Key in node2.related.keys(): if node1Key == node2Key: print(f"connected by: {node1Key}") connected.append(node1Key) return connected def printall(self): if self.related is None: print(f"{self.tostring}None") return None values = list(self.related.values()) for value in values: value.printall() def automate(self): if self.related is None: return None word_cursor.execute(f'''INSERT INTO {table_name} VALUES ('{self.word_node}, {self.related.keys()}')''') values = list(self.related.values()) for value in values: value.automate() food = Words("food") food.relate("bread") food.relate("meat") food.relate("cheese") food.relate("tomatoes") food.relate("lettuce") food.relate("onions") food.relate("pepper") food.relate("sauce") food.relate("rice") food.relate("chicken") food.relate("seaweed") food.relate("soy sauce") sandwich = Words("sandwich") sandwich.relate("bread") sandwich.relate("cheese") sandwich.relate("pickles") sandwich.relate("onions") sandwich.relate("meat") sandwich.relate("tomatoes") sandwich.relate("lettuce") sandwich.relate("butter") food.related.get("bread").relate("flour") food.related.get("bread").related.get("flour").relate("wheat") food.related.get("bread").related.get("flour").relate("rye") food.related.get("bread").related.get("flour").relate("corn") food.related.get("bread").related.get("flour").related.get("rye").relate("grain") food.related.get("bread").related.get("flour").related.get("corn").relate("grain") food.related.get("bread").related.get("flour").related.get("wheat").relate("grain") food.related.get("bread").related.get("flour").related.get("corn").related.get("grain").relate("grass") food.related.get("bread").related.get("flour").related.get("rye").related.get("grain").relate("grass") food.related.get("bread").related.get("flour").related.get("wheat").related.get("grain").relate("grass") food.related.get("bread").related.get("flour").related.get("wheat").related.get("grain").related.get("grass").relate( "plant") food.related.get("bread").related.get("flour").related.get("rye").related.get("grain").related.get("grass").relate( "plant") food.related.get("bread").related.get("flour").related.get("corn").related.get("grain").related.get("grass").relate( "plant") food.printall() sandwich.printall() food.automate() #word_cursor.execute(f'''CREATE TABLE {table_name} (word text,related text)''') #word_cursor.execute(f'''INSERT INTO {table_name} VALUES ('{food.word_node}, {food.related}')''') #words = sqlite3.connect(':memory:') for creating a temporary program-executed database #create a table #commit our command words.commit() #close our connection words.close() 

So that’s that and the resulting error is this:

Traceback (most recent call last): File "C:UsersmaximPycharmProjectsWordNetworkmain.py", line 86, in <module> food.automate() File "C:UsersmaximPycharmProjectsWordNetworkmain.py", line 40, in automate word_cursor.execute(f'''INSERT INTO {table_name} VALUES ('{self.word_node}, {self.related.keys()}')''') sqlite3.OperationalError: near "bread": syntax error 

I have no idea why I can’t do this.

submitted by /u/_User15
[link] [comments]

​r/learnpython So I have two recursive functions. One called printall() that works but is kind of meant for my internal use. The second automate() also works when I use print() to see if it recurses and that’s pretty much a copy of it, but I want to have it do recursion and use SQL to insert word values that way into the “related” column of my table which only has two columns: “word” and “related”. Here’s the full code. Some code is marked by # because I use it for later or have already used it. import sqlite3 words = sqlite3.connect(‘words.db’) #our database is connected to-immediately. table_name = “words” #name the table_name as words too since our database only has this table. word_cursor = words.cursor() #create a cursor: word_cursor = words.cursor() class Words: def __init__(self, word_node): self.word_node = word_node self.related = None self.start = True self.tostring = f”{self.word_node} -> ” def relate(self, concept): if self.related is None: self.related = dict() obj = Words(concept) obj.tostring = self.tostring + f”{obj.tostring}” self.related[concept] = obj def common(self, node2): connected = [] if self.word_node != node2.word_node: for node1Key in self.related.keys(): for node2Key in node2.related.keys(): if node1Key == node2Key: print(f”connected by: {node1Key}”) connected.append(node1Key) return connected def printall(self): if self.related is None: print(f”{self.tostring}None”) return None values = list(self.related.values()) for value in values: value.printall() def automate(self): if self.related is None: return None word_cursor.execute(f”’INSERT INTO {table_name} VALUES (‘{self.word_node}, {self.related.keys()}’)”’) values = list(self.related.values()) for value in values: value.automate() food = Words(“food”) food.relate(“bread”) food.relate(“meat”) food.relate(“cheese”) food.relate(“tomatoes”) food.relate(“lettuce”) food.relate(“onions”) food.relate(“pepper”) food.relate(“sauce”) food.relate(“rice”) food.relate(“chicken”) food.relate(“seaweed”) food.relate(“soy sauce”) sandwich = Words(“sandwich”) sandwich.relate(“bread”) sandwich.relate(“cheese”) sandwich.relate(“pickles”) sandwich.relate(“onions”) sandwich.relate(“meat”) sandwich.relate(“tomatoes”) sandwich.relate(“lettuce”) sandwich.relate(“butter”) food.related.get(“bread”).relate(“flour”) food.related.get(“bread”).related.get(“flour”).relate(“wheat”) food.related.get(“bread”).related.get(“flour”).relate(“rye”) food.related.get(“bread”).related.get(“flour”).relate(“corn”) food.related.get(“bread”).related.get(“flour”).related.get(“rye”).relate(“grain”) food.related.get(“bread”).related.get(“flour”).related.get(“corn”).relate(“grain”) food.related.get(“bread”).related.get(“flour”).related.get(“wheat”).relate(“grain”) food.related.get(“bread”).related.get(“flour”).related.get(“corn”).related.get(“grain”).relate(“grass”) food.related.get(“bread”).related.get(“flour”).related.get(“rye”).related.get(“grain”).relate(“grass”) food.related.get(“bread”).related.get(“flour”).related.get(“wheat”).related.get(“grain”).relate(“grass”) food.related.get(“bread”).related.get(“flour”).related.get(“wheat”).related.get(“grain”).related.get(“grass”).relate( “plant”) food.related.get(“bread”).related.get(“flour”).related.get(“rye”).related.get(“grain”).related.get(“grass”).relate( “plant”) food.related.get(“bread”).related.get(“flour”).related.get(“corn”).related.get(“grain”).related.get(“grass”).relate( “plant”) food.printall() sandwich.printall() food.automate() #word_cursor.execute(f”’CREATE TABLE {table_name} (word text,related text)”’) #word_cursor.execute(f”’INSERT INTO {table_name} VALUES (‘{food.word_node}, {food.related}’)”’) #words = sqlite3.connect(‘:memory:’) for creating a temporary program-executed database #create a table #commit our command words.commit() #close our connection words.close() So that’s that and the resulting error is this: Traceback (most recent call last): File “C:UsersmaximPycharmProjectsWordNetworkmain.py”, line 86, in <module> food.automate() File “C:UsersmaximPycharmProjectsWordNetworkmain.py”, line 40, in automate word_cursor.execute(f”’INSERT INTO {table_name} VALUES (‘{self.word_node}, {self.related.keys()}’)”’) sqlite3.OperationalError: near “bread”: syntax error I have no idea why I can’t do this. submitted by /u/_User15 [link] [comments] 

So I have two recursive functions. One called printall() that works but is kind of meant for my internal use. The second automate() also works when I use print() to see if it recurses and that’s pretty much a copy of it, but I want to have it do recursion and use SQL to insert word values that way into the “related” column of my table which only has two columns: “word” and “related”.

Here’s the full code. Some code is marked by # because I use it for later or have already used it.

import sqlite3 words = sqlite3.connect('words.db') #our database is connected to-immediately. table_name = "words" #name the table_name as words too since our database only has this table. word_cursor = words.cursor() #create a cursor: word_cursor = words.cursor() class Words: def __init__(self, word_node): self.word_node = word_node self.related = None self.start = True self.tostring = f"{self.word_node} -> " def relate(self, concept): if self.related is None: self.related = dict() obj = Words(concept) obj.tostring = self.tostring + f"{obj.tostring}" self.related[concept] = obj def common(self, node2): connected = [] if self.word_node != node2.word_node: for node1Key in self.related.keys(): for node2Key in node2.related.keys(): if node1Key == node2Key: print(f"connected by: {node1Key}") connected.append(node1Key) return connected def printall(self): if self.related is None: print(f"{self.tostring}None") return None values = list(self.related.values()) for value in values: value.printall() def automate(self): if self.related is None: return None word_cursor.execute(f'''INSERT INTO {table_name} VALUES ('{self.word_node}, {self.related.keys()}')''') values = list(self.related.values()) for value in values: value.automate() food = Words("food") food.relate("bread") food.relate("meat") food.relate("cheese") food.relate("tomatoes") food.relate("lettuce") food.relate("onions") food.relate("pepper") food.relate("sauce") food.relate("rice") food.relate("chicken") food.relate("seaweed") food.relate("soy sauce") sandwich = Words("sandwich") sandwich.relate("bread") sandwich.relate("cheese") sandwich.relate("pickles") sandwich.relate("onions") sandwich.relate("meat") sandwich.relate("tomatoes") sandwich.relate("lettuce") sandwich.relate("butter") food.related.get("bread").relate("flour") food.related.get("bread").related.get("flour").relate("wheat") food.related.get("bread").related.get("flour").relate("rye") food.related.get("bread").related.get("flour").relate("corn") food.related.get("bread").related.get("flour").related.get("rye").relate("grain") food.related.get("bread").related.get("flour").related.get("corn").relate("grain") food.related.get("bread").related.get("flour").related.get("wheat").relate("grain") food.related.get("bread").related.get("flour").related.get("corn").related.get("grain").relate("grass") food.related.get("bread").related.get("flour").related.get("rye").related.get("grain").relate("grass") food.related.get("bread").related.get("flour").related.get("wheat").related.get("grain").relate("grass") food.related.get("bread").related.get("flour").related.get("wheat").related.get("grain").related.get("grass").relate( "plant") food.related.get("bread").related.get("flour").related.get("rye").related.get("grain").related.get("grass").relate( "plant") food.related.get("bread").related.get("flour").related.get("corn").related.get("grain").related.get("grass").relate( "plant") food.printall() sandwich.printall() food.automate() #word_cursor.execute(f'''CREATE TABLE {table_name} (word text,related text)''') #word_cursor.execute(f'''INSERT INTO {table_name} VALUES ('{food.word_node}, {food.related}')''') #words = sqlite3.connect(':memory:') for creating a temporary program-executed database #create a table #commit our command words.commit() #close our connection words.close() 

So that’s that and the resulting error is this:

Traceback (most recent call last): File "C:UsersmaximPycharmProjectsWordNetworkmain.py", line 86, in <module> food.automate() File "C:UsersmaximPycharmProjectsWordNetworkmain.py", line 40, in automate word_cursor.execute(f'''INSERT INTO {table_name} VALUES ('{self.word_node}, {self.related.keys()}')''') sqlite3.OperationalError: near "bread": syntax error 

I have no idea why I can’t do this.

submitted by /u/_User15
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *