简体   繁体   English

从数据库中的其他表更新 SQLite3

[英]SQLite3 Update from other table in Database

I have posted the same question in the past, however, I seem to fail on implementing it in my python code.我过去曾发布过同样的问题,但是,我似乎无法在我的 python 代码中实现它。

The objective is to update the table Ships with info already in table Companies.目标是使用表公司中已有的信息更新表 Ships。 Image for reference.图片供参考。 SQLite 表供参考

I have the following code:我有以下代码:

import sqlite3

# connect to database
conn = sqlite3.connect('PSC.sdb')
# create cursor
c = conn.cursor()
c.execute('''
    UPDATE ships
    SET "ISM Performance PM" = (SELECT companies."ISM Performance PM"
                                FROM companies
                                WHERE ships."ISM IMO" = companies."ISM IMO")''')

conn.commit()
conn.close()

When I run it I have no errors but keep having Null values.当我运行它时,我没有错误,但一直有 Null 值。 Sorry for the repost but really need some help.很抱歉重新发布,但确实需要一些帮助。

From the sample data that you posted I see that the condition:从您发布的示例数据中,我看到条件:

WHERE ships."ISM IMO" = companies."ISM IMO"

should succeed and update correctly the table.应该成功并正确更新表。

But what you can try is trim the values before comparing them:但是你可以尝试的是在比较它们之前修剪值:

WHERE TRIM(ships."ISM IMO") = TRIM(companies."ISM IMO")

or convert them to integers:或将它们转换为整数:

WHERE (ships."ISM IMO" + 0) = (companies."ISM IMO" + 0)

so that any existing blank characters are removed and the numbers can be compared.以便删除任何现有的空白字符并可以比较数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM