Jump to content
maniootek

"Operation not allowed when object is closed" sql update query (no result)

Recommended Posts

maniootek

I am trying to figure out how to detect if ADO object has no result. For some sql select query I just checked $oRecordSet.EOF value and it works, but when I use sql update query then I receive Com error.

$oMyError = ObjEvent("AutoIt.Error", "_ErrFunc")

$oConnection = ObjCreate("ADODB.Connection")
$oConnection.Open("DRIVER={SQL Server};" & $authData & ";")
$SqlQuery = "UPDATE *** SET *** = '1' WHERE *** = ***"
SqlQueryExecute($oConnection, $SqlQuery)

Func SqlQueryExecute($SQL_Connection, $SQL_Query)
    $oRecordSet = $SQL_Connection.Execute($SQL_Query)

    if not IsObj($oRecordSet) then Exit MsgBox($MB_ICONERROR, @ScriptName, "Error while processing query")

    if not $oRecordSet.EOF then ;error com handler says here that object is not closed
        Return $oRecordSet.GetRows
    Else
        ConsoleWrite("Query has been processed successfully but there was no return data" & @CRLF)
        Return 0
    EndIf
EndFunc
    
Func _ErrFunc($oError)
    MsgBox($MB_ICONERROR, @ScriptName, "COM ERROR" & @CRLF & "Description: " & $oError.description & @CRLF "ScriptLine:" & $oError.scriptline)
EndFunc   ;==>_ErrFunc

How to detect if object is closed to avoid com error handler catch the exception?

Share this post


Link to post
Share on other sites
jchd

SQL update statements don't return a resultset.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
maniootek

how to detect it?

Share this post


Link to post
Share on other sites
mLipok

Why you just do not use ADO.au3 UDF ?

Func __ADO_Recordset_IsReady(ByRef $oRecordset)
    __ADO_Recordset_IsValid($oRecordset)
    If @error Then
        Return SetError(@error, @extended, $ADO_RET_FAILURE)
    ElseIf $oRecordset.state = $ADO_adStateClosed Then
        Return SetError($ADO_ERR_ISCLOSEDOBJECT, $ADO_EXT_DEFAULT, $ADO_RET_FAILURE)
    ElseIf $oRecordset.state <> $ADO_adStateOpen Then
        Return SetError($ADO_ERR_ISNOTREADYOBJECT, $ADO_EXT_DEFAULT, $ADO_RET_FAILURE)
;~  ElseIf $oRecordset.status <> $ADO_adRecOK Then
;~      Return SetError($ADO_ERR_ISNOTREADYOBJECT, $oRecordset.status, $ADO_RET_FAILURE)
    EndIf
......

 


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API Forum Rules *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library
 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2018-10-31

Share this post


Link to post
Share on other sites
maniootek

.state method was something I was looking for, thank you mLipok.

I don't want to switch to your UDF as I have too many scripts already used with my function.

Share this post


Link to post
Share on other sites
AMFC
 
I'm in a similar situation.
I connect to a DB2 database on an AS / 400 machine.
Everything works, but I do not know the result of my UPDATE transactions if it is not interrogating each time for the value of the modified row.

I tried to ask $SQLRS.State or $SQLRS.Status, but it does not work.

Can someone please help?

Thank you very much.

;***************************************************************
; How to read Return Code from AS/400 DB2 SQL   UPDATE transaction?.
;***************************************************************

; ODBC error control
$oErrorHandler = ObjEvent("AutoIt.Error", "objError")

; TABLE CONNECTION
$SQLCON=ObjCreate("ADODB.Connection")
If @error Then $SQLCON.Close
    
$SQLCON.Mode= 16 ;Shared
$SQLCON.CursorLocation=2 ;Client Side cursor
$SQLCON.ConnectionTimeout=10

$SQLCON.Open ("DSN=XXX.XXX.XXX.XXX;UID='User1';PWD='Password1';)
If @error Then
   MsgBox(16,"Test","Fail conexion.")
   $SQLCON.Close
   Exit
EndIf


; UPDATE TEST
$SQLRS = ObjCreate("ADODB.Recordset")
If Not @error Then
   $SQLRS.Open("update MY_TABLE set COLUMN2='modificated' where COLUMN2='initial data",$SQLCON)
EndIf

MsgBox(48,"Return Code","State: "&$SQLRS.State&@CRLF&"Status: "&$SQLRS.Status)

$SQLCON.Close
Exit

 

Func objError($oError)
   $T ="Num.Error   : " & $oError.number & @CRLF
   $T&="Description : " & $oError.windescription & @CRLF & @CRLF
   $T&=$oError.description & @CRLF & @CRLF & @CRLF
   $T&="Helpfile : " & $oError.helpfile & @CRLF
   $T&="helpcontext : " & $oError.helpcontext & @CRLF
   $T&="lastDLLerror: " & $oError.lastdllerror & @CRLF & @CRLF& @CRLF
   $T&="Script Line: " & $oError.scriptline & @CRLF
   $T&="Ret.Code    : " & $oError.retcode
   MsgBox(48,"Error Rutine",$T)
EndFunc

*************************************************************

 

 

Share this post


Link to post
Share on other sites
maniootek
1 hour ago, AMFC said:

$SQLRS.Open("update MY_TABLE set COLUMN2='modificated' where COLUMN2='initial data",$SQLCON)

try to change Open method to Execute

$SQLRS = $SQLCON.Execute("update MY_TABLE set COLUMN2='modificated' where COLUMN2='initial data")

 

Edited by maniootek

Share this post


Link to post
Share on other sites
AMFC
Thank you very much Maniootek for your quick response.
I have my work station in the company, tomorrow I will do the test.
I can not wait to see the result.
You're very kind.

Share this post


Link to post
Share on other sites
AMFC

Ooooh.

I have tried your suggestion, but it does not answer a "Return Code" either.
In any case, thank you very much.

Share this post


Link to post
Share on other sites
maniootek
Local $ServerAddress = "192.168.0.199,1455\MYSQL"
Local $DatabaseName = "MyDatabase"
Local $UserName = "admin"
Local $Password = "password123"
Local $SQL_Query = "update MY_TABLE set COLUMN2='modificated' where COLUMN2='initial data'"

Local $oErrorHandler = ObjEvent("AutoIt.Error", "objError")
Local $oSqlConnection = ObjCreate("ADODB.Connection")
$oSqlConnection.Open("DRIVER={SQL Server};SERVER=" & $ServerAddress & ";DATABASE=" & $DatabaseName & ";uid=" & $UserName & ";pwd=" & $Password)

Local $oRecordSet = $oSqlConnection.Execute($SQL_Query)

Switch $oRecordSet.State
    Case 0
        MsgBox(0,0,"state=closed")
    Case 1
        MsgBox(0,0,"state=open")
EndSwitch

Func objError($oError)
   $T ="Num.Error   : " & $oError.number & @CRLF
   $T&="Description : " & $oError.windescription & @CRLF & @CRLF
   $T&=$oError.description & @CRLF & @CRLF & @CRLF
   $T&="Helpfile : " & $oError.helpfile & @CRLF
   $T&="helpcontext : " & $oError.helpcontext & @CRLF
   $T&="lastDLLerror: " & $oError.lastdllerror & @CRLF & @CRLF& @CRLF
   $T&="Script Line: " & $oError.scriptline & @CRLF
   $T&="Ret.Code    : " & $oError.retcode
   MsgBox(48,"Error Rutine",$T)
EndFunc

this code show me state value, please try

Share this post


Link to post
Share on other sites
mLipok

I think he want to get result like in MS SQL managment console.


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API Forum Rules *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library
 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2018-10-31

Share this post


Link to post
Share on other sites
AMFC

Maniootek,

 

Sincerely, thank you very much for your suggestions. I will continue looking for a solution.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×