I'm having trouble casting data types in TEXT
columns.
I HAVE to upload all data from a custom CSV file. There is no guarantee that I'll even get all the columns I expect let alone the right data types so I start with a table where all columns are of type TEXT and put empty strings in the blank spots.
Ex. Table looks like this
CREATE TABLE tbl1 ( col1 TEXT, col2 TEXT, col3 TEXT);
After loading tbl1
from a file I run this.
SELECT * FROM tbl1;
Select results:
'1','String1', '2.0'
'2','String2', '3.14'
'3','String3', '6.77776'
'h','Stringh', 'h.h'
'','String', ''
Now I want to take the data from tbl1
and use it to populate this table.
CREATE TABLE tbl2 ( col1 INTEGER, col2 TEXT, col3 REAL);
And I try it like this.
INSERT INTO tbl2 SELECT CAST(tbl1.col1 as INTEGER), tbl1.col2, CAST(tbl1.col3 AS REAL) FROM tbl1;
After that I run this,
SELECT * FROM tbl2;
Select results:
1,'String1', 2.0
2,'String2', 3.14
3,'String3', 6.77776
0,'Stringh', 0
0,'String', 0
What I really want is to grab what I would consider 'Good' casts and insert them into tbl2
And then take all the values I would consider 'Bad' casts and put them in a 'tbl3' that would look like this.
CREATE TABLE tbl3 (col1 TEXT, col2 TEXT, col3 TEXT, REASON_FOR_REJECTION TEXT);
tbl3
would be for reporting and possibly trouble shooting bad data.
Do I need to preprocess this data in C++ before I insert it into tbl2
or does SQLite support some kind of querying function that allows me to catch 'Bad' casts?
UPDATE:
By adding CL.'s query to the end of this I could distinguish which records had "Bad" Casts and clean up tbl2
and add bad data rows to tbl3
.