Page 1 of 1

DataGrid Form and mySQL

Posted: Fri May 10, 2013 2:57 am
by KennyR
Hi everyone once again....I need some advice/help here when it comes to importing my "mySQL" database into my dataGrid form. To make it simple on myself (or so I thought), I am only passing a total of 3 fields from my database called "Orders" into my form. Right now, I am only using 2 records from the DB just to see if it works and again to make it simple to debug. Obviously I am doing this all wrong and need to see if anyone can help me with the code on importing individual records into the form. I have been doing some reading over the past two days and finally broke down and am asking for help. I have checked out the lessons section article --> http://lessons.runrev.com/s/lessons/m/d ... ts-of-data but am confused. I have also attempted to deconstruct the crudeDatabase zip but with no avail. My code is below and all I simply want to do is import my records from my database into my DB form. Any help is greatly appreciated.... Kenny

*Edit--I have no problem importing data from the mySQL database when I use the "Table" version of the DG. Since the data being queried from the server comes back in tab delimited form, it imports nicely in each column. Normally I would be okay with this, but I like the fact that you can customize the DG form with your own buttons and such. I wish it was as easy to import data into the form as it is for the table.

on mouseUp

--Connect to the DB
connectMe

   -- check the global connection ID to make sure we have a database connection
    global gConnectionID
   if gConnectionID is not a number then
       answer error "Please connect to the database first."
       exit to top
   end if

   -- construct the SQL (this selects all the data from the specified table)
    put "SELECT Distinct Name,OrderInfo,Time FROM Orders" into tSQL
   -- query the database
  put revDataFromQuery(tab, cr, gConnectionID, tSQL) into tData

   -- check the result and display the data or an error message
  if item 1 of tData = "revdberr" then
       answer error "There was a problem querying the database:" & cr & tData
    else

--This is where I am confused--

//put ?? into theDataA["Name"]
//put ?? into theDataA["OrderInfo"]
//put ?? into theDataA["Time"]

put img id 3510 card "images" into theDataA["image URL"]
put 1 into theLineNo
set the dgData of group "DataGrid" to empty
dispatch "AddData" to group "DataGrid" with theDataA, theLineNo
put the result into theNewIndex -- integer if successful, error string otherwise
   end if

end mouseUp

Re: DataGrid Form and mySQL

Posted: Fri May 10, 2013 12:07 pm
by Klaus
Hi Kenny,

you need to create a multi-dimensional array for "the DGData":

Code: Select all

  if item 1 of tData = "revdberr" then
    answer error "There was a problem querying the database:" & cr & tData
  else
    
    --This is where I am confused--
    
    ## Wee need to create an ARRAY with ALL the data of the SQL query:
    ## We keep our own counter, so we can use the insanely fast REPEAT FOR EACH...
    put 0 into tLineCounter
    
    ## Data are CR and TAB delimited:
    set itemdel to TAB
    
    ## Optional, but good style to initialize a variably
    put empty into theDataA
    
    repeat for each line tLine in tData
      add 1 to tLineCounter
      put item 1 of tLine into theDataA[tLineCounter]["Name"]
      put item 2 of tLine into theDataA[tLineCounter]["OrderInfo"]
      put item 3 of tLine into theDataA[tLineCounter]["Time"]
      //put ?? into theDataA["Name"]
      //put ?? into theDataA["OrderInfo"]
      //put ?? into theDataA["Time"]
      
      ## Sorry, what does this line do?
      ##      put img id 3510 OF card "images" into theDataA["image URL"]
      ##   put 1 into theLineNo
      
    end repeat
    
    ## Set DGDATA now "en bloc", no need to empty the DG first, this will overwrite eventual data!
    set the dgData of group "DataGrid" to tArray
    
    ## "AddData" will ony work with ONE record in a datagrid of type FORM!
    ##  dispatch "AddData" to group "DataGrid" with theDataA,
    ##   put the result into theNewIndex -- integer if successful, error string otherwise
  end if
...
Hope I got it right :-)


Best

Klaus

Re: DataGrid Form and mySQL

Posted: Fri May 10, 2013 2:35 pm
by KennyR
Klaus!!!! You are a wonderful person! I VERY much appreciate your time on this....you have saved me quite a bit of time and stomach problems!

I am listing my/your code below with a few tweaks so that others may benefit. I made a change where you were clearing the "theDataA" and placed it below the script after the data has populated the DG.

## Optional, but good style to initialize a variably
put empty into theDataA

I thought that if it is being cleared before the repeat statement, then there is nothing to put into an array....am I correct? Secondly, I changed the line "set the dgData of group "DataGrid" to tArray" to "theDataA" since there was no mention of "tArray" in the script. Obviously this was an oversight since nothing was being stored in that variable. On another note, I know I was close in figuring this out because I kept toying with the "repeat for" loop thinking that I had to step through each line of "tData" and put them in an array. I just kept running into problems trying to construct the code. I will say this thought....after looking at what you did, it is plain to see how this works and I will put it in my brain for later use. Simon and Dixie taught me some time ago about how best to use repeat statements and I have been using them in my code ever since. Well at least the (repeat with x=1) version....I am happy you showed me how to use the other version of the repeat. Finally, I wanted to comment about this question you posed in the script....

## Sorry, what does this line do?
## put img id 3510 OF card "images" into theDataA["image URL"]

In my Form, I have an image placeholder called img "Image". I am trying to place a thumbnail of a certain image when the grid is populated with data. To date, I am not having any luck. I have a particular thumbnail on an image card and am referencing the id of that image and attempting to put it in the array for loading. It does not appear to be working and is one of the things on my list to do today. So if you have any suggestions on how to make this work, I would greatly appreciate it.

**Oh by the way...for some reason when I attempt to use the "code" button to block my code on this forum, it doesn't do anything so that is why I've been unable to put my code on this thread the proper way...

Thanks Klaus!


on mouseUp

--Connect to the Database
connectMe

   -- check the global connection ID to make sure we have a database connection
    global gConnectionID
   if gConnectionID is not a number then
       answer error "Please connect to the database first."
       exit to top
   end if
   -- construct the SQL (this selects all the data from the specified table)
  --Query only the fields I want displayed in the Form
    put "SELECT Distinct Name,OrderInfo,Time FROM Orders" into tSQL
   -- query the database
  put revDataFromQuery(tab, cr, gConnectionID, tSQL) into tData

   -- check the result and display the data or an error message
if item 1 of tData = "revdberr" then
    answer error "There was a problem querying the database:" & cr & tData
  else
   
    ## Wee need to create an ARRAY with ALL the data of the SQL query:
    ## We keep our own counter, so we can use the insanely fast REPEAT FOR EACH...
    put 0 into tLineCounter
   
    ## Data are CR and TAB delimited:
    set itemdel to TAB
   
    repeat for each line tLine in tData
      add 1 to tLineCounter
      put item 1 of tLine into theDataA[tLineCounter]["Name"]
      put item 2 of tLine into theDataA[tLineCounter]["OrderInfo"]
      put item 3 of tLine into theDataA[tLineCounter]["Time"]
      put img id 3510 OF card "images" into theDataA["image URL"]
    end repeat
   
    ## Set DGDATA now "en bloc", no need to empty the DG first, this will overwrite eventual data!
    set the dgData of group "DataGrid" to theDataA
    put empty into theDataA
   
  end if
end mouseUp

Re: DataGrid Form and mySQL

Posted: Fri May 10, 2013 2:48 pm
by Klaus
Hi Kenny,

glad you got it working! :-D
And yes "tArray" was a typo of course I meant "theDataA".

But I don't get this:

Code: Select all

...
 repeat for each line tLine in tData
      add 1 to tLineCounter
      put item 1 of tLine into theDataA[tLineCounter]["Name"]
      put item 2 of tLine into theDataA[tLineCounter]["OrderInfo"]
      put item 3 of tLine into theDataA[tLineCounter]["Time"]

     ## This will overwrite the data in -> theDataA["image URL"] in every loop?
     ## put img id 3510 OF card "images" into theDataA["image URL"]

     ## And shouldn't this also go into ALL keys of the array
    put img id 3510 OF card "images" into theDataA[tLinecounter]["image URL"]
    end repeat
...
If you want to have the SAME value for all data in -> theDataA[tLinecounter]["image URL"]
you should do something like this to get the utmost speed ;-)

Code: Select all

...
## Get this value only ONCE!
 put img id 3510 OF card "images" into tImageUrl
 repeat for each line tLine in tData
      add 1 to tLineCounter
      put item 1 of tLine into theDataA[tLineCounter]["Name"]
      put item 2 of tLine into theDataA[tLineCounter]["OrderInfo"]
      put item 3 of tLine into theDataA[tLineCounter]["Time"]

     ## This will overwrite the data in -> theDataA["image URL"] in every loop?
     ## put img id 3510 OF card "images" into theDataA["image URL"]

     ## And shouldn't this also go into ALL keys of the array
    put tImageUrl into theDataA[tLinecounter]["image URL"]
    end repeat
...
The formatting seems broken in the forum, so I added the CODE tags manually.


Best

Klaus

Re: DataGrid Form and mySQL

Posted: Fri May 10, 2013 2:53 pm
by KennyR
Klaus...Yeah I should have removed the reference to the image URL in my code until I figured it out....I noticed that after I posted it but was too lazy to take it out! :) I am going to try your suggestion as soon as I get done here! Thanks!!!!

Re: DataGrid Form and mySQL

Posted: Fri May 10, 2013 3:05 pm
by Klaus
Hi Kenny,

I overlooked thois one:
In my Form, I have an image placeholder called img "Image". I am trying to place a thumbnail of a certain image when the grid is populated with data. To date, I am not having any luck. I have a particular thumbnail on an image card and am referencing the id of that image and attempting to put it in the array for loading. It does not appear to be working and is one of the things on my list to do today. So if you have any suggestions on how to make this work, I would greatly appreciate it.
OK, so you already have:
1. an image on another card that you wnat to show in each record/line of your FROM datagrid.
Is this image imported or referenced? if referenced, you need to store its filename!
...
put the FILENAME img id 3510 OF card "images" into tImageUrl
...
You get the picture...

2. To "put" the image in the array into your placeholder image "Image" (? Come on :-D) you can do this in the "fillinData" handler:
a. imported image:
## ARRAY filled with our latest script, so the array contains the BINARY image data -> put img XYZ into Array[y][x]
...
put tDataArray["image url"] into img "Image" of me
...
b. REFERENCED image, we have its FILENMAE in the Array:
...
set the filename of img "Image" of me to tDataArray["image url"]
...

Maybe you could just use a BUTTON instead of images in your datagrid form,
so you can simply set its icon to the img ID!?

Best

Klaus

Re: DataGrid Form and mySQL

Posted: Fri May 10, 2013 4:16 pm
by KennyR
That is GREAT! Works like a charm...I've learned quite a bit here....I must say, I have been shying away from dataGrids because they seemed tough to work with...and for the most part they are, but this opens my eyes....I really like the form method...much more you can do with them and they look more professional! Thanks Klaus

Re: DataGrid Form and mySQL

Posted: Fri May 10, 2013 6:33 pm
by Klaus
Well, DataGrids ARE complex beasts, everytime I use them, I have to read up the DataGrid PDF again and again and again :-D