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

Sql insert statement

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

    Sql insert statement

    trying to use an sql insert statement and trying to use on duplicate key update and get an error. here is my string

    Code:
    dim sqlInsertStatement as c 
    sqlInsertStatement = <<%txt%
    INSERT INTO contact  (idContact, fname, lname, phone, fax) VALUES (:idContact, :fname, :lname, :phone, :fax)
    	ON DUPLICATE KEY UPDATE %txt%
    not matter what way I try i still get the error.

    i'm trying to insert data but if the pkey exsists it will update the remaining fields and not cause a dupelicate.

    this is the example i have to work with and mysql sends an error back."" INSERT ... ON DUPLICATE KEY UPDATE Syntax
    If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have identical effect:

    INSERT INTO table (a,b,c) VALUES (1,2,3)
    ON DUPLICATE KEY UPDATE c=c+1;""
    https://www.housingeducator.org
    k3srg

    #2
    Re: Sql insert statement

    Ok i have found some of the problem and ran into another. i needed to be using native sql not protable, and this is what my connect string looks like
    Code:
    dim sqlInsertStatement as c 
    sqlInsertStatement = <<%txt%
    INSERT INTO contact  (idContact, fname, lname, phone, fax) VALUES (:idContact, :fname, :lname, :phone, :fax)
    	ON DUPLICATE KEY UPDATE idContact=:idContact
    the :idcontact is a value stored in a variable and i can't get it to fire correctly it's not inserting the record or updateing can anyone tell me where i'm going wrong.
    https://www.housingeducator.org
    k3srg

    Comment


      #3
      Re: Sql insert statement

      I haven't used the DUPLICATE KEY UPDATE syntax but you haven't described what you want to happen if the duplicate key exists. I believe you need to code the UDPATE clause, e.g.,

      Code:
      sqlInsertStatement = <<%txt%
      INSERT INTO contact  (idContact, fname, lname, phone, fax) VALUES (:idContact, :fname, :lname, :phone, :fax)
      	ON DUPLICATE KEY UPDATE 
      set fname=:fname, lname=:lname, phone=:phone, fax=:fax
      The only difference between the UPDATE statement here and a regular UPDATE is that a WHERE clause
      is not needed, since the record is selected by the presence of a duplicate idContact.

      An alternative syntax is MySQL's REPLACE INTO, which is simpler, and is
      Code:
       
      REPLACE INTO contact  (idContact, fname, lname, phone, fax) VALUES (:idContact, :fname, :lname, :phone, :fax)
      REPLACE INTO does an INSERT INTO if the key doesn't exist or a DELETE FROM and INSERT INTO if it does. I don't know how REPLACE INTO works with autoincrement columns. If you don't have any autoincrement columns and are always inserting/replacing the entire record, then REPLACE INTO may be what you want. Otherwise, ON DUPLICATE KEY UPDATE is probably safer.

      Comment


        #4
        Re: Sql insert statement

        well peter still can't make it do what i want. I'm trying to avoid making a set and forms based on the sets only need certain data open and don't want the whole database opening for 1 record. records start on the first table lead, then once you get to the next step i have a dialog popup and load variables from the xdialog. Then insert record on contact table and if record exsists then to update everything but the contactid.
        https://www.housingeducator.org
        k3srg

        Comment


          #5
          Re: Sql insert statement

          My mistake, the syntax for duplicate key update does not accept "set". Should be

          Code:
          sqlInsertStatement = <<%txt%
          INSERT INTO contact  
          (idContact, fname, lname, phone, fax) 
          VALUES 
          (:idContact, :fname, :lname, :phone, :fax)
          ON DUPLICATE KEY UPDATE 
          fname=:fname, lname=:lname, phone=:phone, fax=:fax

          Comment


            #6
            Re: Sql insert statement

            Ok that mostly works but I keep comming up with empty string passed to query here is the full code maybe I missed something.
            Code:
            'Create an XDialog dialog box to prompt for parameters.
            DIM GLOBAL vfname as C
            DIM GLOBAL vlastname as C
            DIM GLOBAL vphone as C
            DIM GLOBAL vfax as C
            DIM GLOBAL varC_result as C
            ok_button_label = "&OK"
            cancel_button_label = "&Cancel"
            varC_result = ui_dlg_box("Contact Info",<<%dlg%
            {region}
            {Region}
            ;
            {Endregion};
            {Region}
            First Name:| [.40vfname];
            Last Name:| [.40vlastname];
            Phone:| [.40vphone];
            Fax:| [.40vfax];
            {Endregion}; 
            {Region}
            ;
            {Endregion};
            {line=1,0};
            <*15=ok_button_label!OK> <15=cancel_button_label!CANCEL>;
            {endregion};
            %dlg%)
            
            'Execute inline Xbasic code.
            'Get 'Value' property of 'Idleads' in Form 'Call_Go' .
            DIM SHARED vpkey AS n
            vpkey = parentform:Idleads.value
            'Insert a new record into a remote SQL database.
            'DIM a connection variable
            DIM cn as SQL::Connection
            dim flagResult as l 
            flagResult = cn.open("::Name::Connection1")
            if flagResult = .f. then 
            	ui_msg_box("Error","Could not connect to database. Error reported was: " + crlf() + cn.CallResult.text)
            	end 
            end if
            
            'Specify that we are using Portable SQL syntax
            cn.PortableSQLEnabled = .f.
            
            'Dim a SQL arguments object, create arguments and set their values
            DIM args as sql::arguments
            
            if a5_eval_valid_expression("=Var->vpkey",local_variables()) then 
            	args.add("idContact",convert_type(a5_eval_expression("=Var->vpkey",local_variables()),"N"))
            end If
            if a5_eval_valid_expression("=Var->vfname",local_variables()) then 
            	args.add("fname",convert_type(a5_eval_expression("=Var->vfname",local_variables()),"C"))
            end If
            if a5_eval_valid_expression("=Var->vlname",local_variables()) then 
            	args.add("lname",convert_type(a5_eval_expression("=Var->vlname",local_variables()),"C"))
            end If
            if a5_eval_valid_expression("=Var->vphone",local_variables()) then 
            	args.add("phone",convert_type(a5_eval_expression("=Var->vphone",local_variables()),"C"))
            end If
            if a5_eval_valid_expression("=Var->vfax",local_variables()) then 
            	args.add("fax",convert_type(a5_eval_expression("=Var->vfax",local_variables()),"C"))
            end If
            
            dim sqlInsertStatement as c <<%txt%
            sqlInsertStatement = INSERT INTO contact (idContact, fname, lname, phone, fax) 
            VALUES (:idContact, :fname, :lname, :phone, :fax)
            ON DUPLICATE KEY UPDATE fname=:fname, lname=:lname, phone=:phone, fax=:fax 
            %txt%
            
            dim flag as l 
            flag = cn.Execute(sqlInsertStatement,args)
            if flag = .f. then 
            	ui_msg_box("Error","Record was not inserted. Error reported was: " + crlf(2) + cn.CallResult.text,UI_STOP_SYMBOL)
            else
            	if cn.AffectedRows() = 1 then 
            		ui_msg_box("Notice","Record was created.",UI_INFORMATION_SYMBOL)
            	else
            		ui_msg_box("Error","Record was not inserted." ,UI_STOP_SYMBOL)
            	end if 
            end if 
            
            'Now, close the connection 
            cn.close()
            There has to be something I'm missing just not sure where.
            https://www.housingeducator.org
            k3srg

            Comment


              #7
              Re: Sql insert statement

              You're moving back and forth among global variables, local variables, and shared (session) variables. There's no need for your script to use anything other than local variables.

              Try this:
              Code:
              'Create an XDialog dialog box to prompt for parameters.
              DIM  vfname as C
              DIM  vlastname as C
              DIM  vphone as C
              DIM  vfax as C
              DIM  varC_result as C
              ok_button_label = "&OK"
              cancel_button_label = "&Cancel"
              varC_result = ui_dlg_box("Contact Info",<<%dlg%
              {region}
              {Region}
              ;
              {Endregion};
              {Region}
              First Name:| [.40vfname];
              Last Name:| [.40vlastname];
              Phone:| [.40vphone];
              Fax:| [.40vfax];
              {Endregion}; 
              {Region}
              ;
              {Endregion};
              {line=1,0};
              <*15=ok_button_label!OK> <15=cancel_button_label!CANCEL>;
              {endregion};
              %dlg%)
              
              'Execute inline Xbasic code.
              'Get 'Value' property of 'Idleads' in Form 'Call_Go' .
              DIM vpkey AS n
              vpkey = parentform:Idleads.value
              'Insert a new record into a remote SQL database.
              'DIM a connection variable
              DIM cn as SQL::Connection
              dim flagResult as l 
              flagResult = cn.open("::Name::Connection1")
              if flagResult = .f. then 
              	ui_msg_box("Error","Could not connect to database. Error reported was: " + crlf() + cn.CallResult.text)
              	end 
              end if
              
              'Specify that we are NOT using Portable SQL syntax
              cn.PortableSQLEnabled = .f.
              
              'Dim a SQL arguments object, create arguments and set their 'values
              DIM args as sql::arguments
              
              args.add("idContact",vpkey)  
              args.add("fname",vfname)
              args.add("lname",vlname)
              args.add("phone",vphone)
              args.add("fax",vfax)
              
              dim sqlInsertStatement as c <<%txt%
              sqlInsertStatement = INSERT INTO contact (idContact, fname, lname, phone, fax) 
              VALUES (:idContact, :fname, :lname, :phone, :fax)
              ON DUPLICATE KEY UPDATE fname=:fname, lname=:lname, phone=:phone, fax=:fax 
              %txt%
              
              dim flag as l 
              flag = cn.Execute(sqlInsertStatement,args)
              if flag = .f. then 
              	ui_msg_box("Error","Record was not inserted. Error reported was: " + crlf(2) + cn.CallResult.text,UI_STOP_SYMBOL)
              else
              	if cn.AffectedRows() = 1 then 
              		ui_msg_box("Notice","Record was created.",UI_INFORMATION_SYMBOL)
              	else
              		ui_msg_box("Error","Record was not inserted." ,UI_STOP_SYMBOL)
              	end if 
              end if 
              
              'Now, close the connection 
              cn.close()

              Comment


                #8
                Re: Sql insert statement

                It still keeps comming up with empty string passed to query....
                not sure why the variables are comming up empty
                https://www.housingeducator.org
                k3srg

                Comment


                  #9
                  Re: Sql insert statement

                  You have a typo that I copied inadvertently:

                  dim sqlInsertStatement as c <<%txt%
                  sqlInsertStatement = INSERT INTO contact (idContact, fname, lname, phone, fax)
                  VALUES (:idContact, :fname, :lname, :phone, :fax)
                  ON DUPLICATE KEY UPDATE fname=:fname, lname=:lname, phone=:phone, fax=:fax
                  %txt%


                  It needs to be

                  dim sqlInsertStatement as c = <<%txt%
                  sqlInsertStatement = INSERT INTO contact (idContact, fname, lname, phone, fax)
                  VALUES (:idContact, :fname, :lname, :phone, :fax)
                  ON DUPLICATE KEY UPDATE fname=:fname, lname=:lname, phone=:phone, fax=:fax
                  %txt%

                  Comment


                    #10
                    Re: Sql insert statement

                    Sorry it took me so long to get back to this but i still keep comming up with an error that the sql string is empty, somewhere my variables are losing their value and if i change it i get bad syntax on the sql statement. So it's half way there if i can get away from the empty sting.
                    https://www.housingeducator.org
                    k3srg

                    Comment


                      #11
                      Re: Sql insert statement

                      There were actually two typos and I only corrected one. This should work:

                      dim sqlInsertStatement as c=<<%txt%
                      INSERT INTO contact
                      (idContact, fname, lname, phone, fax)
                      VALUES
                      (:idContact, :fname, :lname, hone, :fax)
                      ON DUPLICATE KEY UPDATE
                      fname=:fname, lname=:lname, phone=hone, fax=:fax
                      %txt%

                      Comment


                        #12
                        Re: Sql insert statement

                        You still have some typo's but i caught them and it works Thank you peter for your help.

                        Here is the final code I am using in case someone else needs the direction. I have commented out the errors after the insert statement because I'm not sure how to clean them up. It would tell me the record was not inserted when in fact it wasn't but it was updated.
                        Code:
                        'Date Created: 23-Aug-2009 06:28:00 PM
                        'Last Updated: 28-Aug-2009 03:34:55 AM
                        'Created By  : Steven
                        'Updated By  : Steven
                        Code:
                        'Create an XDialog dialog box to prompt for parameters.
                        DIM  vfname as C
                        DIM  vlastname as C
                        DIM  vphone as C
                        DIM  vfax as C
                        DIM  varC_result as C
                        ok_button_label = "&OK"
                        cancel_button_label = "&Cancel"
                        varC_result = ui_dlg_box("Contact Info",<<%dlg%
                        {region}
                        {Region}
                        ;
                        {Endregion};
                        {Region}
                        First Name:| [.40vfname];
                        Last Name:| [.40vlastname];
                        Phone:| [.40vphone];
                        Fax:| [.40vfax];
                        {Endregion}; 
                        {Region}
                        ;
                        {Endregion};
                        {line=1,0};
                        <*15=ok_button_label!OK> <15=cancel_button_label!CANCEL>;
                        {endregion};
                        %dlg%)
                        
                        'Execute inline Xbasic code.
                        'Get 'Value' property of 'Idleads' in Form 'Call_Go' .
                        DIM vpkey AS n
                        vpkey = parentform:Idleads.value
                        'Insert a new record into a remote SQL database.
                        'DIM a connection variable
                        DIM cn as SQL::Connection
                        dim flagResult as l 
                        flagResult = cn.open("::Name::Connection1")
                        if flagResult = .f. then 
                        	ui_msg_box("Error","Could not connect to database. Error reported was: " + crlf() + cn.CallResult.text)
                        	end 
                        end if
                        
                        'Specify that we are NOT using Portable SQL syntax
                        cn.PortableSQLEnabled = .f.
                        
                        'Dim a SQL arguments object, create arguments and set their 'values
                        DIM args as sql::arguments
                        
                        args.add("idContact",vpkey)  
                        args.add("fname",vfname)
                        args.add("lname",vlastname)
                        args.add("phone",vphone)
                        args.add("fax",vfax)
                        
                        dim sqlInsertStatement as c= <<%txt%
                        INSERT INTO contact 
                        (idContact, fname, lname, phone, fax)
                        VALUES 
                        (:idContact, :fname, :lname, :phone, :fax)
                        ON DUPLICATE KEY UPDATE 
                        fname=:fname, lname=:lname, phone=:phone, fax=:fax
                        %txt%
                        
                        dim flag as l 
                        flag = cn.Execute(sqlInsertStatement,args)
                        'if flag = .f. then 
                        	'ui_msg_box("Error","Record was not inserted. Error reported was: " + crlf(2) + cn.CallResult.text,UI_STOP_SYMBOL)
                        'else
                        	if cn.AffectedRows() = 1 then 
                        		ui_msg_box("Notice","Record was created.",UI_INFORMATION_SYMBOL)
                        	'else
                        		'ui_msg_box("Error","Record was not inserted." ,UI_STOP_SYMBOL)
                        	end if 
                         
                        
                        'Now, close the connection 
                        cn.close()
                        Hope someone other than me finds this usefull
                        Last edited by steve745; 08-28-2009, 03:42 AM.
                        https://www.housingeducator.org
                        k3srg

                        Comment

                        Working...
                        X