【问题标题】:If a table has columns equal to columns in a second table insert values in a third table, python - mysql如果一个表的列等于第二个表中的列,则在第三个表中插入值,python - mysql
【发布时间】:2021-09-21 00:03:54
【问题描述】:

我的数据库中有 3 个表:

  • 借款人:贷款金额、利率、借款人 ID (PK)
  • 贷方:出价金额、利率和贷方 ID (PK)
  • Contracts:ContractID PK、金额、利率和两个外键(borrowerID、lenderID)

我使用输入命令在 python 中创建了一个循环,我可以在其中插入新的贷款请求和新的投标报价。 每个插入都与mysql连接,并添加到相应的表中。

例如:

python 中的输入 --> L 1000 1.1 输出 --> 在 Borrowers 表中创建一个自动增量 PK、金额为 1000、利率为 1.1% 的插入

我需要在循环中创建另一个函数,其中对于每个新的请求/报价,如果报价等于请求 (Borrowers.Amount, BorrowersInterestRate = Lenders.Amount, Lenders.InterestRate),它将被创建一个插入具有相同金额和利率的合同表。

【问题讨论】:

  • 那么你的问题是什么?你尝试创作吗?请先显示您的代码。如果对您不起作用,请描述问题

标签: python mysql while-loop foreign-keys insertion


【解决方案1】:

这仅在您使用 mysql python 连接器时有效。据我所知,这是使用 python 使用 mysql 的唯一方法,但我可能错了。

搜索请求是否与报价匹配:

def search_for_request(amount, interest_rate):
    # assuming that columns containing amount and interest rate are named as such
    database.cursor.execute("select amount_of_the_bid, interest_rate from lenders")
    result = database.cursor.fetchall()
    for amount_and_interest in result:
        # not sure if this step is necessary
        amount_and_interest = list(amount_and_interest)
        if amount_and_interst[0] == amount and amount_and_interst[1] == interest_rate:
            # execute query for entering the contract info in contract table
            database.commit()

搜索报价是否匹配请求:

def search_for_offer(amount, interest_rate):
    # assuming that columns containing amount and interest rate are named as such
    database.cursor.execute("select amount_of_the_bid, interest_rate from borrowers")
    result = database.cursor.fetchall()
    for amount_and_interest in result:
        # not sure if this step is necessary
        amount_and_interest = list(amount_and_interest)
        if amount_and_interest[0] == amount and amount_and_interest[1] == interest_rate:
            # execute query for entering the contract info in contract table
            database.commit()

【讨论】:

  • 这几乎是我一直在寻找的答案,但多亏了你,我明白了如何解决我的要求!
猜你喜欢
  • 1970-01-01
  • 2020-10-23
  • 2023-03-29
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-08
相关资源
最近更新 更多