Recent

Author Topic: How to import a txt file to MySQL using Lazarus  (Read 4225 times)

InkaEvil

  • Newbie
  • Posts: 2
How to import a txt file to MySQL using Lazarus
« on: December 05, 2016, 03:07:26 am »
Hello everyone,
How can I import a file in txt format to the database (MySQL) using Lazarus?
The data in the txt file are numbers separated by simple spacings, for example:

01 02 03 04 05
01 02 03 05 06
10 11 12 13 14
...
...

Thank you!

derek.john.evans

  • Guest
Re: How to import a txt file to MySQL using Lazarus
« Reply #1 on: December 05, 2016, 04:11:09 am »
Assuming you have a TDataSet and know how use AppendRecord(), or Append/Post using TDataSet.Fields to set fields, there are a number of ways to read in delimited text.

1) AssignFile/ReadLn/TStringList
Use standard Pascal file reading. Put each line into a TStringList via CommaText/DelimitedText. Iterate the strings and append to your TDataSet

2) Use TCSVParser (unit csvreadwrite). Parse fields and append to TDataSet. An example of this is here:
https://sourceforge.net/p/upascal/code/HEAD/tree/udbf.pas#l136

3) Load the entire file into a TStringList. Use a secondary TStringList to parse out the fields as per (1)

4) Read each line and use FreePascals extract functions to grab each field.
http://www.freepascal.org/docs-html/rtl/strutils/extractdelimited.html

5) Read each line and use your own field extraction code or System.ReadStr()

6) Read each field using System.Read (which requires you to know the number of columns)

The choice is yours. They all have pros/cons. I prefer option (2)

EDIT: The other option is to write or use a prewritten CSV to SQL tool. And then execute the SQL query on your database or though Lazarus.
« Last Edit: December 05, 2016, 04:16:55 am by Geepster »

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: How to import a txt file to MySQL using Lazarus
« Reply #2 on: December 05, 2016, 11:34:59 am »
There's also TCsvDataset ( a BufDataset with csv options) in fcl-db
Specialize a type, not a var.

InkaEvil

  • Newbie
  • Posts: 2
Re: How to import a txt file to MySQL using Lazarus
« Reply #3 on: December 05, 2016, 10:35:14 pm »
Goodnight friends,
Thank you for your attention.
At first, I expected a more direct answer, as a ready function. But I realized how vast and fascinating this is. Like a Lego toy, with a little imagination, you can create what you want.
Despite my haste, I will study every case suggested by you to learn as much as I can, even if I solve my problem on the first suggestion. It is a fascinating language.
Thanks for your time and directions.
God bless you!
« Last Edit: December 05, 2016, 10:37:05 pm by InkaEvil »

 

TinyPortal © 2005-2018