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 Loop & Update

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

    MySQL Loop & Update

    There's got to be a better way to update records in a loop. As I understand it, the only way to update (not insert) a loop is to fetch through the result set and then run a SQL update command for each iteration on some unique criteria of the record you are fetching. This is especially problematic if the table doesn't have a unique key (I know it should, but never mind that). You would thing that rs.nextRow() would have a built in counter (somehow) or a method to address that row directly w/o doing a where clause for each single record (130K or more each day).

    Any suggestions?
    Peter
    AlphaBase Solutions, LLC

    [email protected]
    https://www.alphabasesolutions.com



    #2
    Re: MySQL Loop & Update

    I'm not sure of exactly what you are doing. I used to do updates the way you described but after I discovered procedures I was able to just call a procedure and let mysql do the work. I think you could do the same with sql
    Win 10 64 Development, Win 7 64 WAS 11-1, 2, Win 10 64 AA-1,2, MySql, dbForge Studio The Best MySQL GUI Tool IMHO. http://www.devart.com/dbforge/mysql/studio/

    Comment


      #3
      Re: MySQL Loop & Update

      Peter, can you give an example of what you want to update? You can update the whole table with something as simple as:

      UPDATE mytable SET field1 = 'abc'

      or

      UPDATE myable SET field1 = (field1 * .10) + field2
      Steve Wood
      See my profile on IADN

      Comment


        #4
        Re: MySQL Loop & Update

        Thanks guys. Steve, it needs to be a loop. I agree, it would be easy if it were a global update. But each row may change depending upon previous values. We are actually converting a VFP script to xbasic. Below is part 1 of that (VFP) script which we are working on now.

        Code:
        PUBLIC m.bldtype,cpart
        m.bldtype='BLD'
        cpart=ALLTRIM(PART)
        GO TOP
        
        DO WHILE !EOF()
        	IF cpart<>PART
        		cpart=PART
        		m.bldtype='BLD'
        	ENDIF
        	IF bnote='***********' AND SUBSTR(bpart,1,3)='EAR' AND m.bldtype<>'EAR'
        		m.bldtype='EAR'
        *!*		ELSE
        *!*			m.bldtype=''	
        	ENDIF
        	IF bnote='***********' AND SUBSTR(bpart,1,3)='KIT' AND m.bldtype<>'KIT'
        		m.bldtype='KIT'
        *!*		ELSE
        *!*			m.bldtype=''	
        	ENDIF
        	IF bnote='***********' AND m.bldtype='KIT' AND EMPTY(bpart) AND m.bldtype<>'MISC'
        		m.bldtype=''
        *!*		ELSE
        *!*			m.bldtype=''	
        	ENDif	
        
        	DO CASE
        		CASE m.bldtype='BLD'
        			REPLACE bldtype WITH 'BLD'
        		CASE m.bldtype='EAR'
        			REPLACE bldtype WITH 'EAR'
        		CASE m.bldtype='KIT'
        			REPLACE bldtype WITH 'KIT'
        		CASE m.bldtype='BOX'
        			REPLACE bldtype WITH 'BOX'
        
        	ENDCASE
        	SKIP
        ENDDO
        So 100,000+ records will be real slow and this script will be run daily.
        Peter
        AlphaBase Solutions, LLC

        [email protected]
        https://www.alphabasesolutions.com


        Comment


          #5
          Re: MySQL Loop &amp; Update

          UPDATE mytable SET field1 = 'abc' IS a loop, it runs through each record in turn; just omit any WHERE statement. If you Google "SQL UPDATE CASE statements" you can find examples that come close to what you have to write.
          Steve Wood
          See my profile on IADN

          Comment


            #6
            Re: MySQL Loop &amp; Update

            I know it's a loop, but as you can see, I can't just set "abc", each row is different. Buy I'll Google your suggestion to see what show.
            Peter
            AlphaBase Solutions, LLC

            [email protected]
            https://www.alphabasesolutions.com


            Comment


              #7
              Re: MySQL Loop &amp; Update

              Following up on Steve's suggestion, here are two of the most simplest variations I can't get to work in a sample table of two fields:

              This one uses MySql syntax:
              SQL = "UPDATE Loop SET no = '44' IF item = 'boo'"
              conn.execute(SQL)


              This uses Alpha's portable SQL syntax:
              SQL = "UPDATE Loop SET no = IF(item = 'boo','44','00')"
              conn.execute(SQL)



              I get this:
              1064 - 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Loop SET no = If(item = 'boo','44','00')' at line 1'
              Any ideas where the error is?
              Peter
              AlphaBase Solutions, LLC

              [email protected]
              https://www.alphabasesolutions.com


              Comment


                #8
                Re: MySQL Loop &amp; Update

                Peter,

                I think you should be using the WHERE clause instead of IF.

                SQL = "UPDATE Loop SET no = '44' WHERE item = 'boo'"
                conn.execute(SQL)
                Bill Griffin
                Parkell, Inc

                Comment


                  #9
                  Re: MySQL Loop &amp; Update

                  Bill,

                  Thanks, but that's exactly what I am trying to avoid as per my above posts.
                  Peter
                  AlphaBase Solutions, LLC

                  [email protected]
                  https://www.alphabasesolutions.com


                  Comment


                    #10
                    Re: MySQL Loop &amp; Update

                    Back to what Steve said - google "MYSQL UPDATE CASE Statements" plenty of examples.... - check this out. converting multiple update queries into case statement IF is not going to work as you used it.
                    Last edited by Bill Griffin; 12-15-2011, 02:22 PM.
                    Bill Griffin
                    Parkell, Inc

                    Comment


                      #11
                      Re: MySQL Loop &amp; Update

                      Thanks Bill. Already had been there. But I found out my immediate problem - in Post #7 above, my simple example failed, because "loop" is a reserved word in MySql. So now, at least, I have the Alpha syntax "If" method working. Progress....
                      Peter
                      AlphaBase Solutions, LLC

                      [email protected]
                      https://www.alphabasesolutions.com


                      Comment


                        #12
                        Re: MySQL Loop &amp; Update

                        Cool. So much (too much) to learn :)
                        Bill Griffin
                        Parkell, Inc

                        Comment


                          #13
                          Re: MySQL Loop &amp; Update

                          And native Sql works too as in...

                          Code:
                          SQL = <<%str%
                              UPDATE LoopTest 
                              SET no = 
                              CASE
                              WHEN item = 'alpha' THEN '7'
                              WHEN item = 'hello' THEN '99'
                              WHEN item = 'yikes' THEN '333'
                              END;
                          %str%
                          
                          conn.PortableSQLEnabled = .f.
                          conn.execute(SQL)
                          More progress...
                          Peter
                          AlphaBase Solutions, LLC

                          [email protected]
                          https://www.alphabasesolutions.com


                          Comment


                            #14
                            Re: MySQL Loop &amp; Update

                            Pete,

                            thanks for posting the results. So many unresolved threads, it's nice to seeing the resolution posted.
                            Bill Griffin
                            Parkell, Inc

                            Comment


                              #15
                              Re: MySQL Loop &amp; Update

                              Peter,

                              It would be faster to build a single statement (using *concat or *concat_lines) and run one update. Use the loop to create a number of CASE statements, and when you get enough, then run one update.

                              Speaking of updates, I see in your last post that you are already doing this? You should be able to get at least 100 case statements in one update, perhaps more.
                              Pat Bremkamp
                              MindKicks Consulting

                              Comment

                              Working...
                              X