Topic: Date and Time in import CSV

Hello Dmitry and MVD fans,

In my little project (in attachment), i'd like to import a csv file (called Journal2.csv).

When I click button Import, I get two error messages :

Item Date : "Date" is not a valid Date
Item Time : to many parameters at 30:63

What would be the good syntax to import correctly Date and Time ?

Thanks for you help

JB

Post's attachments

Attachment icon CSV.zip 6.97 kb, 430 downloads since 2016-10-04 

2 (edited by mathmathou 2016-10-04 23:27:49)

Re: Date and Time in import CSV

Hello Jean


You have a row (number 0) in your CSV file with the name of your columns. This is the one causing the error.


Instead of starting your loop at 0, start it at 1 in order to skip the first row.


c := sl.Count;
          for i := 1 to c - 1 do
          begin....

As for the rest, huuuuuuu.... I'm still looking :-)


Have fun


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Date and Time in import CSV

Okayyyyy,


Don't known why you wanted to convert a string into another string with FormatDateTime, while this function takes a TDate on entry and you had... a string.... smile (I'm not laughing at you but with you, no offense).


I took the shortest route as date and time are stored as Strings in the sqlite database, and swapped the parts of the sDate to get a valid date to store. (sql format is yyyy-MM-dd for us with MVD I think, adn displayed dd-MM-yyyy in tablegrids for us because of our local windows settings).


Also added a "little extra" that's you'll probably notice if you try to import "Journal3.csv" that is more than 1000 lines long.


Have fun and take care


Cheers


Mathias

Post's attachments

Attachment icon CSV2.zip 338.04 kb, 447 downloads since 2016-10-05 

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Date and Time in import CSV

Hello Mathmathou

Thanks for your answer and fixed project.

I always have a hard time to extricate myself with dates and times (and I long program).
I thought breaking down the date by day, month, year, hour and minute, but I still had a doubt.

So, I put the script in my snippets.

Also, "cerise sur le gâteau", the ProgressBar nice. Such, in snippets.
It gives tone to the application.

Mathmathou, thank you again for your help.

JB

Re: Date and Time in import CSV

Hello Mathmathou

A little probleme seen whenI  import a csv file.

On opening project, tablegrid displays datas for file journal1 (8 records).
Then I import Journal2.csv qui contains 9 records.

The problem is that Journal2 records are incrementad with Journal1 records (that to say my grid displays 17 records.

My goal is when importing Journal2 file, Journal1.csv disappears from the grid without alter the database.
What I would succeed is as if I used function LoadFromFile for a text file.

Your previous answer works fine, the only problem is it increments instead of replacing the first file by the second in the tableGrid.

I don't know if I am clear

Thanks for your answer .

JB

Re: Date and Time in import CSV

Hello Jean


I see two approaches to achieve what you are after, one with a tiny database modifcation, the other not


//---------- WITH DATABASE MOD:


- add a Boolean field called is_last_import to the table (or what you want), with a default value of 1
- after opening the CSV file but before looping through the lines, update the table to set Boolean field to 0
- update TableGrid to filter on is_last_import = 1


THIS WAY :

- on opening the applictaion, the is_last_import = 1 records are shown in the tablegrid
- they are all reset to 0 and the new batch of import is saved with is_last_import = 1 (default value)
- the tablegrid is refreshed and only shows the last imports


//---------- WITHOUT DATABASE MOD

-your tablegrid must be blank with no settings, it will be filled by query AFTER the import
- after opening the CSV file but before looping through the lines, find MAX(id) from you table and store it in a variable
- after saving to database your new records, find the new MAX(id) in the same table
- fill you tablegrid with the standard script query and a filter condition on journal.id > first_max ID AND journal.id <= second_max_id


Only downside of this method is that, on launch of your software, nothing differentiate the records in your table, so I see no way to show just the last record saved, the tablegrid only fills after a CSV import


Hope this helps and maybe you'lle get better answers from the others


Cheers



Mathias

Post's attachments

Attachment icon CSV2-with-DB-modification.zip 338.65 kb, 464 downloads since 2016-10-06 

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Date and Time in import CSV

and second example because only one attachment per post smile

Post's attachments

Attachment icon CSV2-without-DB-modification.zip 338.7 kb, 469 downloads since 2016-10-06 

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Date and Time in import CSV

Hello Mathmathou

Excellent idea to set a kind of flag for different files imported.

Thanks for this, I'm testing it

Thanks again
JB

Re: Date and Time in import CSV

Hello Jean,


Happy that this idea suits you. One thing though....


You might have noticed while importing the 1000+ lines CSV file, that the ProgressBar does not fill up to 100% but stops at around 70 to 75% and the process is finished.


This is a Windows deffect, and there is a workaround to correct the ProgressBar fill to 100%


Find in your code the

Progressbar.Max := c; //-----> Max length

And replace the code below with :

ProgressBar.Position := ProgressBar.Position + 2; //-----> Increment 2 step
ProgressBar.Position := ProgressBar.Position - 1; //-----> Decrement 1 step
Form1.Label4.Caption := 'Processed : '+IntToStr(i)+'/'+IntToStr(c-1);

The trick is, in the same loop, to increment 2 steps and decrement one. Don't ask me why, but now the ProgressBar will fill up to 100% on process completion.


Cheers



Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor