I've tried with the answers below, but I cannot find a proper way of doing this
- SQLite inner join - update using values from another table
- How do I make an UPDATE while joining tables on SQLite?
- Update table values from another table with the same user name
Here's the problem. I have a sqlite db with two tables. In the code below I use pandas to insert some dummy values
import sqlite3
import pandas
conn = sqlite3.connect('foo.db')
curs = conn.cursor()
df1 = pandas.DataFrame([{'A' : 1, 'B' : 'a', 'C' : None}, {'A' : 1, 'B' : 'b', 'C' : None}, {'A' : 2, 'B' : 'c', 'C' : None}])
df1.to_sql('table1', conn, index = False)
df2 = pandas.DataFrame([{'A' : 1, 'D' : 'x'}, {'A' : 2, 'D' : 'y'}])
df2.to_sql('table2', conn, index = False)
This results in two tables
pandas.read_sql('select * from table1', conn)
A B C
0 1 a None
1 1 b None
2 2 c None
and
pandas.read_sql('select * from table2', conn)
A D
0 1 x
1 2 y
Now I want to join these two tables on column A
and update columns table1.C
with the resulting D
This is what I've tried
Solution 1 in the list above
sql = """
replace into table1 (C)
select table2.D
from table2
inner join table1 on table1.A = table2.A
"""
curs.executescript(sql)
conn.commit()
pandas.read_sql('select * from table1', conn)
A B C
0 1.0 a None
1 1.0 b None
2 2.0 c None
3 NaN None x
4 NaN None x
5 NaN None y
wrong
Solution 2 in the list above
sql = """
replace into table1 (C)
select sel.D from (
select table2.D as D
from table2
inner join table1 on table1.A = table2.A
) sel
"""
curs.executescript(sql)
conn.commit()
pandas.read_sql('select * from table1', conn)
A B C
0 1.0 a None
1 1.0 b None
2 2.0 c None
3 NaN None x
4 NaN None x
5 NaN None y
Solution 3 in the list above
sql = """
update table1
set C = (
select table2.D
from table2
inner join table1 on table1.A = table2.A
)
"""
curs.executescript(sql)
conn.commit()
pandas.read_sql('select * from table1', conn)
A B C
0 1 a x
1 1 b x
2 2 c x
which is clearly wrong since the last row is updated to x
, it should be y
At this point I'm pretty much out of options. Any help is much appreciated