简体   繁体   中英

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.

The objective is to update the table Ships with info already in table Companies. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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