Skip to content
Advertisement

INSERT + SELECT data type mismatch on similar fields

I’m running the following SQLite workaround to add a primary key to a table that did not have one. I am getting a datatype mismatch on

However, the fields have exactly the same type. Is it possible that his happens due to running the queries from DbBrowser for SQLite?

Advertisement

Answer

You have defined the column id of the table cities to be INTEGER, but with this:

you insert the string 'pan' as id.
SQLite does not do any type checking in this case and allows it.

Did you mean to insert 2 rows each having the names 'pan' and 'doul'?
If so, you should do something like:

Later you rename the table cities to old_cities and you recreate cities but you do something different: you define id as INTEGER and PRIMARY KEY.
This definition is the only one that forces type checking in SQLite.

So, when you try to insert the rows from old_cities to cities you get an error because 'pan' is not allowed in the column id as it is defined now.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement