首先,我是Python和MySQL的初学者,因此很有可能我以错误的方式做了一些事情而没有意识到。有了这种方式。我的问题是在使用Python创建的数据库中填充表。我已经创建了2个表,然后去填充这些表,但是只有其中一个将数据插入到其中。
以下是填充函数:
def popEst():
my_cursor = mydb.cursor()
sql = "INSERT INTO establishment (name, description) VALUES (%s, %s)"
entries = int(input("Enter number of entries: "))
records = []
for row in range(entries):
records.append([])
for row in range(entries):
for col in range(2):
records[row].append(col)
records[row][col] = 0
for row in range(entries):
for col in range(2):
if col == 0:
records[row][col] = input("Enter name: ")
else:
records[row][col] = input("Enter description: ")
my_cursor.executemany(sql, records)
mydb.commit()
def popLoot():
my_cursor = mydb.cursor()
sql = "INSERT INTO loot (name, description) VALUES (%s, %s)"
entries = int(input("Enter number of entries: "))
records = []
for row in range(entries):
records.append([])
for row in range(entries):
for col in range(2):
records[row].append(col)
records[row][col] = 0
for row in range(entries):
for col in range(2):
#records[row][col] = input("Field: " + str(col+1) + " Entry: " + str(row+1) + " : ")
if col == 0:
records[row][col] = input("Enter name: ")
else:
records[row][col] = input("Enter description: ")
my_cursor.executemany(sql, records)
mydb.commit()
这是根据用户输入调用两个函数之一的代码段:
pop_choice = input("Select Table: ")
if pop_choice.upper() == "LOOT":
popLoot()
else:
popEst()
运行此命令时,由于某种原因,仅调用popLoot()
,因此正在填充战利品表。
非常感谢那些花时间研究我的问题的人。祝您有愉快的一天! :)