Welcome, Guest. Please login or register.

Author Topic: Importing from a text file.  (Read 45 times)

Offline Columbo

  • Administrator
  • Hero Member
  • *****
  • Posts: 511
  • Basic is alive and well!
    • View Profile
    • Divi-Help (For Divi Builder users.
Importing from a text file.
« on: May 09, 2019, 04:21:51 PM »
Here is a challenge!  I have a text file that contains a number of Meal-Master recipes.  What I need to do is read the file, grab the title, category, yield, ingredients and directions and assign them to variables which will then be written to a database.  In other words I want to import all of the recipes in the text file, one at a time, into a database.

Here is the first 2 recipes in the file to show an example of the Meal-Master format:

Code: [Select]

---------- Recipe via Meal-Master (tm) v8.02
 
      Title: CAJUNIZED ORIENTAL PORK CHOPS
 Categories: Meats
      Yield: 6 servings
 
      6 ea Thick pork chops
      1 x  Salt & red cayenne pepper
  1 1/2 c  Dry white wine
      1 c  Bell pepper, chopped
      1 c  Onions, chopped
      1 ea Clove garlic, chopped
      3 T  Soy sauce
      1 cn Pinapple chunks (15 oz.)
 
  Salt and red pepper the chops.  Brown them slowly in a skillet.
  Add wine, bell pepper, onion, and garlic,  Cover and simmer for
  25 to 30 minutes.  Remove pork chops, being sure to keep them warm.
  Add the soy sauce and syrup from the pineapple.  Stir and simmer
  until more or less thick.  Add the pineapple chunks and bring to
  a boil.  Serve over pork chops and hot cooked rice.
  From Justin Wilson's "Gourmet and Gourmand Cookbook"
 
-----
 
---------- Recipe via Meal-Master (tm) v8.02
 
      Title: BARBECUED PORK CHOPS
 Categories: Meats
      Yield: 8 servings
 
    1/2 c  Water
    1/4 c  Vinegar
      2 T  Dry mustard
      3 T  Brown sugar
      4 T  Chili sauce
      8 ea Pork chops
 
  Combine ingredients and mix well.  Pour over seasoned chops
  in glass baking dish, cover and bake at 400 F degrees for
  1-1/2 hours, uncovering last 30 minutes.  Water may be added
  to make gravy.  Delicious with ribs also. 
-----



We know where each recipe begins because it starts with the following header line to indicate that it is the start of a Meal-Master recipe.
Code: [Select]
---------- Recipe via Meal-Master (tm) v8.02

The line with the 5 dashes (-----) indicates the end of that particular recipe.

I am trying to use ReadFile(#srcFile, srcFile)to read the text file a line at a time and While Eof(#srcFile) = 0 to look for the end of the file and fileLIne = Trim(ReadString(#srcFile)) to get the line read however, the problem is that I obviously do not want to save the header line, any blank lines, or the end of file marker, ď-----ď to the database. Also, the bigger problem is that the ingredients and the directions can have any number of lines which would have to be captured and written to the database.

The ingredients are preceded and followed by a blank line which Iím thinking I might be able to use to indicate the start and end of the ingredients.  The same idea for the directions.  Once the title, category, yield, ingredients and directions are assigned to variables and written to a database, it would then do the same thing again for each of the following recipes in the text file.  The database is recipes, the table is recipe and the columns are scat, recipe, ingredients, directions, serves which would be written to  category,  title, yield, ingredients and directions.


I have tried several ways to do this but I am unable to achieve the desired result.  I am able to get the title, category, and yield assigned to variables but I canít seem to come up with a way to get all of the lines in the ingredients and all of the lines in the directions saved to variables so that they can all be saved into the database.

Can someone give me an example of how best to achieve this?

Thanks.





New forum for DIVI Builder users. - http://www.divi-help.com

Offline TI-994A

  • Sr. Member
  • ***
  • Posts: 242
  • You can do it in BASIC!
    • View Profile
Re: Importing from a text file.
« Reply #1 on: May 10, 2019, 02:21:53 PM »
Hi John. I saw your post in the PureBasic forum and replied there.

> Reading a text file to import into a database
Texas Instruments 99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too!

Offline Columbo

  • Administrator
  • Hero Member
  • *****
  • Posts: 511
  • Basic is alive and well!
    • View Profile
    • Divi-Help (For Divi Builder users.
Re: Importing from a text file.
« Reply #2 on: May 10, 2019, 06:53:32 PM »
Yes,  thanks Syed. 

I was looking at your code and it works with your recipes.txt file but since Meal-Master files use the .mmf extension, I changed the file to recipes.mmf and changed line 29 in the code to If OpenFile(0, "recipes.mmf") but now it doesn't import anything.  Not sure why?  I checked the code and I don't see any other lines that reference the text file name that should also be changed.  The other thing is that when using the text file as recipes.txt and your code uses While Not Eof(0), it only imports 3 recipes even though there are a fairly large number of recipes in the file.  ??

Also, just so that I can understand what is happening in the code, to set the data to be inserted into the database, are you using bind variables?  (Not sure because I have never used them before).

"INSERT INTO " +
"recipes (recipe, scat, serves, ingredients, directions) " +
"VALUES (?, ?, ?, ?, ?)"     


John
« Last Edit: May 10, 2019, 10:40:24 PM by Columbo »
New forum for DIVI Builder users. - http://www.divi-help.com

Offline TI-994A

  • Sr. Member
  • ***
  • Posts: 242
  • You can do it in BASIC!
    • View Profile
Re: Importing from a text file.
« Reply #3 on: May 11, 2019, 10:25:49 AM »
...to set the data to be inserted into the database, are you using bind variables?

Yes. The PureBasic SetDatabaseString() function binds values to the placeholders (question marks) in the preceding query. This is ideal as the function sanitises the values (stripping and escaping characters) before inserting them into the database.

Regarding the read failures, it appears that the MealMaster .mmf files have standard layouts but not delimiters. Apparently the lines that separate the recipes (eg: --- Recipe via Meal-Master (tm) v8.02) differ from author to version.

However, since they would still have to conform to the .mmf layout specifications, it seems easy enough to automatically determine and extract the delimiters being used in each file. I've modified the example to do this, and have made a new post over at the PureBasic forum.

> MealMaster Recipe Extractor


Texas Instruments 99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too!

Offline Columbo

  • Administrator
  • Hero Member
  • *****
  • Posts: 511
  • Basic is alive and well!
    • View Profile
    • Divi-Help (For Divi Builder users.
Re: Importing from a text file.
« Reply #4 on: May 11, 2019, 08:00:20 PM »
Thanks Syed.  Works great now. Another quick question for you if you don't mind.  I know that you can run an external program from within Purebasic but once the external program ends, does it return to the Purebasic program that called it?

Thanks and as always very much appreciated.
New forum for DIVI Builder users. - http://www.divi-help.com

Offline TI-994A

  • Sr. Member
  • ***
  • Posts: 242
  • You can do it in BASIC!
    • View Profile
Re: Importing from a text file.
« Reply #5 on: May 12, 2019, 10:37:05 AM »
...you can run an external program from within Purebasic but once the external program ends, does it return to the Purebasic program that called it?

If you'd need it to. Here's the usual method for launching an external application:
Code: [Select]
MessageRequester("RunProgram():", "Click OK to launch system calculator...")
RunProgram("calc.exe", "", "", #PB_Program_Open)
MessageRequester("RunProgram():", "System calculator ruuning and I'm outta here!")

And here's the method to wait for the application to terminate:
Code: [Select]
MessageRequester("RunProgram():", "Click OK to launch system calculator...")
RunProgram("calc.exe", "", "", #PB_Program_Open | #PB_Program_Wait)
MessageRequester("RunProgram():", "System calculator terminated.")

There are quite a few other interesting flags to play with, if you're interested:

> PureBasic Manual - RunProgram()


Texas Instruments 99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too!

Offline Columbo

  • Administrator
  • Hero Member
  • *****
  • Posts: 511
  • Basic is alive and well!
    • View Profile
    • Divi-Help (For Divi Builder users.
Re: Importing from a text file.
« Reply #6 on: May 12, 2019, 11:03:16 PM »
Thanks Syed.  Actually I was thinking of having a button in my recipe program that would allow me to run your Meal-Master program to import recipes from .mmf files.  That is why I was wondering if it would return to my Master Chef program after it finished importing the recipes.  I gues befor I try that I have to find out why when importing a ,mmf file it over writes the files that are already in the database.

John

« Last Edit: May 12, 2019, 11:47:28 PM by Columbo »
New forum for DIVI Builder users. - http://www.divi-help.com

Offline TI-994A

  • Sr. Member
  • ***
  • Posts: 242
  • You can do it in BASIC!
    • View Profile
Re: Importing from a text file.
« Reply #7 on: May 13, 2019, 01:34:02 AM »
The MealMaster code posted in the PureBasic forum is more of a working POC. It recreates the database on every import; not append to it.

Anyway, quite easily done. Please give me a while to convert my code into a module so that you could plug it directly into your application with some simple function calls.

I'll try to get back to you ASAP.
Texas Instruments 99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too!

Offline Columbo

  • Administrator
  • Hero Member
  • *****
  • Posts: 511
  • Basic is alive and well!
    • View Profile
    • Divi-Help (For Divi Builder users.
Re: Importing from a text file.
« Reply #8 on: May 13, 2019, 10:15:16 AM »
No problem Syed,  As I mentioned in the post at Purebasic Forum, I didn't understand that it recreated the database each time it was run.  It does work really well though.  Thanks

Cheers!

John
New forum for DIVI Builder users. - http://www.divi-help.com