有没有办法在PostgreSQL的python函数中执行多个查询?

时间:2017-06-14 10:15:08

标签: python sql postgresql python-2.7

这是我目前的代码..

def reportMatch(winner, loser):
    """Records the outcome of a single match between two players.

    Args:
      winner:  the id number of the player who won
      loser:  the id number of the player who lost
    """
    db = psycopg2.connect(database=DBNAME)
    c = db.cursor()
    #c.execute("INSERT INTO match_records(winner,loser) VALUES(1,0);")
    c.execute("INSERT INTO score from standing VALUES(1);", "INSERT INTO score from no_standing VALUES(0);")
    db.commit()
    db.close()

我一直在专门针对MySQL获取答案并且阅读cursor.executemany()使用时速度慢两倍(另外,我不知道如何使用executemany()

这涉及的数据库表是

CREATE TABLE players (
id INT NOT NULL PRIMARY KEY,
player_name NOT NULL CHARACTER,
)
CREATE TABLE standing (
id INT NOT NULL references players(id),
winner CHARACTER,
score INT
)
CREATE TABLE not_standing (
id INT NOT NULL references players(id),
loser CHARACTER,
score INT
)

我想问这个代码是否也可以在python函数中执行许多查询:

queries = ["INSERT INTO winner from standing VALUES(1);", "INSERT INTO loser from no_standing VALUES(0);"]
c.execute(queries)

0 个答案:

没有答案
相关问题