I have a Sqlite database named test.db, which contains two tables with structures like this:
Table1: ID INTEGER PRIMARY KEY AUTOINCREMENT, Name varchar(500), Color varch
The first thing you should do is fix your tables so that the data is correct for your application. It looks like you have data like Apple\n
in your table's name
column.
Run this query to fix your table:
UPDATE Table1 SET Name = rtrim(Name,'\n')
That will remove all \n
from the right side of the value of the Name
column in each row, and then update that row.
Edit:
@Martijn's answer will allow your query to work (assuming you don't have the same problem with Table2.Name
), but you should really fix the data in your tables so that you don't have to remember to do these workarounds everytime.
Use the replace()
SQL function:
cur.execute("SELECT Color, Smell FROM Table1, Table2 "
"WHERE replace(Table1.Name, '\n', '') = Table2.Name")
You can update your whole Table
Name
column to remove the newline characters altogether:
cur.execute("UPDATE Table1 SET Name = replace(Name, '\n', '') "
"WHERE Name like '%\n%'")