Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

MySQL Query fails in loop

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #16
    Re: MySQL Query fails in loop

    Thanks for that. I hadn't selected anything for the List return and therefore got back an interesting array of data. Now that's I've my List to PrimaryKey or a field I get the same as you.

    Glad you can see the error now... and can get past that latter issue... and on to the former.

    Comment


      #17
      Re: MySQL Query fails in loop

      Here is a more detailed example.

      There are 2 tables: posting and postingrules. I want to get data from postingrules, then loop through selected posting rows, constructing a WHERE clause, and query posting table to see the posting record matches the query. If there is a match, the query will return a resultset with one row.

      This code still returns 'Commands out of sync; you can't run this command now' on the second loop.


      Code:
      function testProcessRules as c (e as p)
      dim flag_exe as L
      dim flag as l
      dim flagMainRuleLoop as l
      dim js as c
      dim cn as sql::Connection
      dim sql as c
      dim args as sql::Arguments
      dim rs as sql::ResultSet
      dim rs2 as sql::ResultSet
      
      dim _numLoopsRules as n = 0
      dim _numLoopsPostings as n = 0
      
      dim _billToID as c = e.dataSubmitted.BillTo_IndexPK
      dim _custName as c = e.dataSubmitted.Customer
      
      args.add("A",_billToID)
      args.add("C","Y")
      
      sql = <<%a%
      SELECT Rule_PK, RuleType, Company_IDFK, BillTo_FK, PUcity, PU_ST, DestCity, Dest_ST
      FROM postingrules
      WHERE BillTo_FK = :A AND EnabledYN = :C
      ORDER BY Rule_PK
      %a%
      dim flag_open as l
      flag_open = cn.open("::Name::FreightzoneMySql")
      flag = cn.execute(sql,args)
      rs = cn.ResultSet
      
      dim _enabledYN as c
      dim _rulePK as n
      	
      'These are the fields to "Look for" in the posting record
      'they are used in the "If" part of the expression
      dim R_BillTo_FK as c
      dim R_PUcityPE as c
      dim R_PU_ST as c
      dim R_DestCityPE as c
      dim R_Dest_ST as c
      	
      'These are not actually fields to look for in the posting record.
      'They are the values to use as replacements
      
      dim _whereClause as c	
      
      flagMainRuleLoop = rs.NextRow()
      while flagMainRuleLoop	
      	
      	_numLoopsRules = _numLoopsRules +1
      
      	_rulePK = rs.Data("Rule_PK")	
      
      	R_BillTo_FK = rs.Data("BillTo_FK")
      	R_PUcityPE = rs.data("PUcity")
      	R_PU_ST = rs.data("PU_ST")
      	R_DestCityPE = rs.data("DestCity")
      	R_Dest_ST = rs.data("Dest_ST")
      
      	_whereClause = "WHERE BillTo_FK = " + "'" + R_BillTo_FK + "'" + " AND " 
      		if R_PUcityPE <> "" then
      			_whereClause = _whereClause + "originCity = " + "'" + R_PUcityPE + "'" + " AND "
      		end if
      
      		if R_PU_ST <> "" then
      			_whereClause = _whereClause + "originState = " + "'" + R_PU_ST + "'" + " AND "
      		end if
      	
      		if R_DestCityPE <> "" then
      			_whereClause = _whereClause + "destCity = " + "'" + R_DestCityPE + "'" + " AND "
      		end if
      
      		if R_Dest_ST <> "" then
      			_whereClause = _whereClause + "destState = " + "'" + R_Dest_ST + "'" + " AND "
      		end if
      
      dim listCnt as n
      dim i as n
      dim _postID as c
      listCnt = e.dataSubmitted.PostingList.size()
      
      for i = 1 to listCnt
      		
      	_numLoopsPostings = _numLoopsPostings +1
      
      	_postID = e.dataSubmitted.PostingList[i]
      
      		_whereClause = _whereClause + "Posting_ID = " + "'" + _postID + "'" + " AND "
      
      		dim _pos as n = AT("AND",_whereClause,-1)
      		
      		_whereClause = stuff(_whereClause,_pos,6,"")
      	
      		args.add("B",_postID)
      
      		sql = <<%a%
      		SELECT Posting_ID
      		FROM posting
      		***WHERE***
      		ORDER BY Posting_ID
      		%a%
      	
      		sql = stritran(sql,"***WHERE***",_whereClause)	
       
      successful = cn.execute(sql)
      
      if .not. successful then
      debug(1)
      'send an email to the appropriate person/people
      'put it in a log file	
      sqlerr = cn.callresult.text
      'This tells you what caused the failure.
      
      else
      'put the stuff in here you want done after a successful
      'call to your sql database
      
      end if 
      
      next i		
      
      flagMainRuleLoop = rs.NextRow()
      
      cn.Close()
      
      end while
      
      dim msg as c
      
      msg = "Number of Rule Loops " + _numLoopsRules + crlf() + "Number of times a rule checked a posting " + _numLoopsPostings
      
      js = "alert('" + js_escape(msg) + "');"
      
      testProcessRules = js	
      
      end function

      Comment


        #18
        Re: MySQL Query fails in loop

        Here is an example of a complex filter.

        03 Filter-scrubber.png

        Here is the general layout of the filters.

        Filtering records.png

        So, with next to no effort you can produce very complex filters just using action JavaScript.

        You are free to use "and or" SQL syntax.

        What you are trying to should take no more than 5 minutes to write using QBE search in filtering a list.

        Comment


          #19
          Re: MySQL Query fails in loop

          I have discovered a mistake in the construction of the WHERE clause, that was causing it to fail on the second loop.


          On the second loop, it was still retaining the Posting_ID string from the previous loop, so it was using WHERE BillTo_FK = '441348' AND Posting_ID = '78' Posting_ID = '102'

          Here is a revised section:


          Code:
          dim _whereClause2 as c
          for i = 1 to listCnt
          
          _numLoopsPostings = _numLoopsPostings +1
          
          _postID = e.dataSubmitted.PostingList[i]
          
          _whereClause2 = _whereClause + "Posting_ID = " + "'" + _postID + "'" + " AND "
          
          dim _pos as n = AT("AND",_whereClause2,-1)
          
          _whereClause2 = stuff(_whereClause2,_pos,6,"")
          
          args.add("B",_postID)
          
          sql = <<%a%
          SELECT Posting_ID, originCity
          FROM posting
          ***WHERE***
          ORDER BY Posting_ID
          %a%
          
          sql = stritran(sql,"***WHERE***",_whereClause2)

          This now presents a completely different reason that the code is failing.

          Comment


            #20
            Re: MySQL Query fails in loop

            I do not want to filter a list. I have filters in place for viewing and selecting different sets of records.

            I want to modify the posting table with data from the rules table.

            Here is a more detailed explanation.....

            The posting table contains data about available shipments of commercial freight. So it has name of cities, states, dates, mileage, etc.

            A customer will email a spreadsheet that contains a list of available shipments, and the user will upload the spreadsheet.

            On upload, the spreadsheet is automatically imported into the posting table using FlowHeater https://flowheater.net/en/, which maps the fields, deletes blank rows, sets some default values, and INSERTS INTO the posting table.

            After the import completes, the PostingList is refreshed, a filter is applied (Using action javascript "Filter Records in s List Control").

            At this point the data in the list is still somewhat "raw" because the customer's spreadsheet contains errors or is missing values for certain fields.

            For example, the customer has an city named "Dallsa", which needs to be corrected to "Dallas", or, the customer's spreadsheets does not contain zip codes, and the zip code fields need to be populated.

            The use can create a New Rule in the rules table. Rules apply to specific customers (so the rules table contains a foreign key field that is populated by default with the Customers Primary Key field value).

            The rules table also contains fields with values to "look for" in the posting table in order to decide if the rules applied to a record in the posting table, and the rules table also contains fields with 'replacement values" to use in and UPDATE statement, when the rule "looks for" and finds a matching posting record.

            So the user can literally construct a conditional statement that looks like this:

            If the origin city says "Dallsa" change it to "Dallas". Rules can by much more complex.

            The rules are saved in the rules table, because the customer misspelled city may appear in future spreadsheets. So once a rules is created, it does not need to be re-created.

            So the user uploads a spreadsheet, select all records (which are all the new imported records for this customer), and clicks the "Process Rules" button.

            That is where this code executes. It first gets a list of this customer's all the rules in the rules table.

            Then it loops through the list of selected records from the PostingList control (a multi-select List Control).

            In each loop, the checks the posting table record against the rule record, and if it finds a match, it executes code to to do an UPDATE to the posting record, with the "replace values" from the list.

            So the first rule record is fetched, and loops the every selected posting record.

            Then the second rule record is fetched, and loops through every selected posting record.

            I actually have working code that does all of this, but it fails, - seeming randomly.

            In my attempts to troubleshoot it, I tried to create the simplest possible function that would re-create the failure. I found that it was failing on the second loops for the reasons identified in earlier posts in this topic.

            Now I have found that it was failing on the second loop due to an additional problem with the construction of the WHERE clause, which i believe is not fixed.

            So now I have a loop that runs successfully if it is not looping too many times.

            That is where I am at right now. Sorry for the long post, but felt an explanation would be helpful.

            I can post a video. That might be better.

            Comment


              #21
              Re: MySQL Query fails in loop

              Does a join not select the records you want.

              Say you create a view in this way you can then run an update on the view.

              I am absolutely clueless about xbasic so I always try to work in SQL or Javascript.

              I am a lover of flowheater. It is a great program we have been using it 50 times a day for 4+ years.

              Comment


                #22
                Re: MySQL Query fails in loop

                Michael, now that you mention it, I believe I learned about FlowHeater from one of your posts.

                On the back end of my process, I use FlowHeater to export selected posting records to a txt file that I then ftp to another server.

                Anyway... I am open to an alternate method of comparing these tables and making updates. I think you are correct, a join would select the records that match in both tables.

                Now that I have my xbasic code able to run past the second loop, it works fine for smaller sets of records, but fails sometime after approximately 16,000 loops. Then cn.callresulttext = 'Can't connect to MySQL server on 'localhost' (45444)'. So I must be running up against some limitation.

                I would welcome any advice on how to structure it with a join.

                Comment


                  #23
                  Re: MySQL Query fails in loop

                  Good morning Joe,

                  Here is an alternative to using join.

                  sql = <<%a%
                  select * from postingrules
                  where customer_id in (select customer_id from postingrules)
                  %a%

                  returns all the rules for the customer, then you loop through the results from sql call, updating on any rule match.
                  Gregg
                  https://paiza.io is a great site to test and share sql code

                  Comment


                    #24
                    Re: MySQL Query fails in loop

                    Greg, i am already doing that.



                    dim _billToID as c = e.dataSubmitted.BillTo_IndexPK
                    args.add("A",_billToID)

                    sql = <<%a%
                    SELECT Rule_PK, RuleType, Company_IDFK, BillTo_FK, PUcity, PU_ST, DestCity, Dest_ST
                    FROM postingrules
                    WHERE BillTo_FK = :A AND EnabledYN = :C
                    %a%

                    Comment


                      #25
                      Re: MySQL Query fails in loop

                      then you loop through the results, and don't make another sql call until you're updating a record or getting the rules for the next client.
                      Loop through Reuslts explains it nicely.
                      Gregg
                      https://paiza.io is a great site to test and share sql code

                      Comment


                        #26
                        Re: MySQL Query fails in loop

                        Joe... post a sample UX and table dumps with data and the issue can be worked through. Otherwise it's just a lot of guessing.

                        Comment


                          #27
                          Re: MySQL Query fails in loop

                          I will gladly write a suggested join and test with flowheater.

                          Comment


                            #28
                            Re: MySQL Query fails in loop

                            David, I will post a UX with table data.

                            Michael, I would appreciate that very much.

                            I may not get back to this until tomorrow.

                            I appreciate allthe help.

                            Comment


                              #29
                              Re: MySQL Query fails in loop

                              Here are sample table dumps and some UXs. You should be able to run it with these.

                              Note that this is still in development, so it is a little rough in some areas.

                              Here is a short video to show the problem.


                              https://www.screencast.com/t/oEp8mZjJgHV5
                              Attached Files
                              Last edited by Pizzadude; 03-12-2017, 04:58 PM. Reason: Edited to add a linkl

                              Comment


                                #30
                                Re: MySQL Query fails in loop

                                The trucktype_pe table is missing.

                                Comment

                                Working...
                                X