Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 01/18/2024 in all areas

  1. Andreik

    SQLite Foreign Keys

    Following your suggestions. Added two examples also: First example is what might be default behavior of _SQLite_Open() where foreign keys are disabled by default. Second example is how the new parameter $bFKEnable would work to enable foreign keys when the database is opened. _SQLite_ForeignKeys() with $bFKEnable = Default returns current foreign_keys setting or if $bFKEnable is provided as a boolean value will enable/disable foreign keys support. Success: returns current foreign_keys setting as boolean value (enabled/disabled) Failure: returns a value that can be compared against $SQLITE_* constants @error = 1 - Cannot retrieve current foreign_keys setting (@extended contains the error code returned by _SQLite_QuerySingleRow). @error = 2 - Cannot set foreign_keys setting (@extended contains the error code returned by _SQLite_Exec). #include <SQLite.au3> _SQLite_Startup() ;~ ################################################################# ;~ # Example #1 # ;~ ################################################################# ConsoleWrite('Running example #1' & @CRLF) $hDB = _SQLite_Open2() ; Current value ConsoleWrite('Current foreign_keys setting: ' & _SQLite_ForeignKeys() & @CRLF) ; Enable foreign keys _SQLite_ForeignKeys($hDB, True) ConsoleWrite('Current foreign_keys setting: ' & _SQLite_ForeignKeys() & @CRLF) ; Disable foreign keys _SQLite_ForeignKeys($hDB, False) ConsoleWrite('Current foreign_keys setting: ' & _SQLite_ForeignKeys() & @CRLF) _SQLite_Close($hDB) ;~ ################################################################# ;~ ################################################################# ;~ # Example #2 # ;~ ################################################################# ConsoleWrite('Running example #2' & @CRLF) $hDB = _SQLite_Open2(Default, Default, Default, True) ; Current value ConsoleWrite('Current foreign_keys setting: ' & _SQLite_ForeignKeys() & @CRLF) _SQLite_Close($hDB) ;~ ################################################################# _SQLite_Shutdown() Func _SQLite_ForeignKeys($hDB = Default, $bFKEnable = Default) If $hDB = Default Then $hDB = -1 ; $bFKEnable = Default - Return current PRAGMA foreign_keys setting If $bFKEnable = Default Then Local $aRow Local $iQueryRet = _SQLite_QuerySingleRow($hDB, 'PRAGMA foreign_keys;', $aRow) If @error Then Return SetError(1, @error, $iQueryRet) Return SetError(0, 0, $aRow[0] = 1 ? True : False) EndIf ; $bFKEnable = True - Enable foreign keys ; $bFKEnable = False - Disable foreign keys Local $iExecRet = _SQLite_Exec($hDB, 'PRAGMA foreign_keys = ' & ($bFKEnable ? 'ON' : 'OFF') & ';') If $iExecRet = $SQLITE_OK Then Return SetError(0, 0, $bFKEnable ? True : False) Else Return SetError(2, @error, $iExecRet) EndIf EndFunc ;==>_SQLite_ForeignKeys Func _SQLite_Open2($sDatabase_Filename = Default, $iAccessMode = Default, $iEncoding = Default, $bFKEnable = Default) If Not $__g_hDll_SQLite Then Return SetError(3, $SQLITE_MISUSE, 0) If $sDatabase_Filename = Default Or Not IsString($sDatabase_Filename) Then $sDatabase_Filename = ":memory:" Local $tFilename = __SQLite_StringToUtf8Struct($sDatabase_Filename) If @error Then Return SetError(2, @error, 0) If $iAccessMode = Default Then $iAccessMode = BitOR($SQLITE_OPEN_READWRITE, $SQLITE_OPEN_CREATE) Local $bOldBase = FileExists($sDatabase_Filename) ; encoding cannot be changed if base already exists If $iEncoding = Default Then $iEncoding = $SQLITE_ENCODING_UTF8 EndIf Local $avRval = DllCall($__g_hDll_SQLite, "int:cdecl", "sqlite3_open_v2", "struct*", $tFilename, _ ; UTF-8 Database filename "ptr*", 0, _ ; OUT: SQLite db handle "int", $iAccessMode, _ ; database access mode "ptr", 0) If @error Then Return SetError(1, @error, 0) ; DllCall error If $avRval[0] <> $SQLITE_OK Then __SQLite_ReportError($avRval[2], "_SQLite_Open") _SQLite_Close($avRval[2]) Return SetError(-1, $avRval[0], 0) EndIf $__g_hDB_SQLite = $avRval[2] __SQLite_hAdd($__g_ahDBs_SQLite, $avRval[2]) If Not $bOldBase Then Local $aEncoding[3] = ["8", "16", "16be"] _SQLite_Exec($avRval[2], 'PRAGMA encoding="UTF-' & $aEncoding[$iEncoding] & '";') EndIf ; Foreign keys support ; ##################################### If $bFKEnable = Default Then $bFKEnable = False If $bFKEnable Then _SQLite_ForeignKeys($avRval[2], $bFKEnable) If @error Then Local $iError = @error _SQLite_Close($avRval[2]) Return SetError(4, $iError, 0) EndIf EndIf ; ##################################### Return SetExtended($avRval[0], $avRval[2]) EndFunc ;==>_SQLite_Open One more example with foreign keys enabled. #include <SQLite.au3> _SQLite_Startup() $hDB = _SQLite_Open2(Default, Default, Default, True) _SQLite_Exec($hDB, 'CREATE TABLE Authors(idAuthor INTEGER, AuthorName VARCHAR(64), PRIMARY KEY("idAuthor" AUTOINCREMENT));') _SQLite_Exec($hDB, 'CREATE TABLE Books(idBook INTEGER, idAuthor INTEGER , BookName VARCHAR(64), PRIMARY KEY("idBook" AUTOINCREMENT), FOREIGN KEY("idAuthor") REFERENCES Authors("idAuthor"));') _SQLite_Exec($hDB, 'INSERT INTO Authors(AuthorName) VALUES("Arthur Conan Doyle");') $idAuthor = _SQLite_LastInsertRowID($hDB) ; This should work _SQLite_Exec($hDB, 'INSERT INTO Books(idAuthor, BookName) VALUES(' & $idAuthor & ', "The Lost World");') ; This should fail with message: FOREIGN KEY constraint failed $idAuthor += 1 _SQLite_Exec($hDB, 'INSERT INTO Books(idAuthor, BookName) VALUES(' & $idAuthor & ', "The Final Problem");') Local $aResult, $iRows, $iCols _SQLite_GetTable2D($hDB, 'SELECT * FROM Books;', $aResult, $iRows, $iCols) _SQLite_Display2DResult($aResult) _SQLite_Close($hDB) _SQLite_Shutdown() Func _SQLite_ForeignKeys($hDB = Default, $bFKEnable = Default) If $hDB = Default Then $hDB = -1 ; $bFKEnable = Default - Return current PRAGMA foreign_keys setting If $bFKEnable = Default Then Local $aRow Local $iQueryRet = _SQLite_QuerySingleRow($hDB, 'PRAGMA foreign_keys;', $aRow) If @error Then Return SetError(1, @error, $iQueryRet) Return SetError(0, 0, $aRow[0] = 1 ? True : False) EndIf ; $bFKEnable = True - Enable foreign keys ; $bFKEnable = False - Disable foreign keys Local $iExecRet = _SQLite_Exec($hDB, 'PRAGMA foreign_keys = ' & ($bFKEnable ? 'ON' : 'OFF') & ';') If $iExecRet = $SQLITE_OK Then Return SetError(0, 0, $bFKEnable ? True : False) Else Return SetError(2, @error, $iExecRet) EndIf EndFunc ;==>_SQLite_ForeignKeys Func _SQLite_Open2($sDatabase_Filename = Default, $iAccessMode = Default, $iEncoding = Default, $bFKEnable = Default) If Not $__g_hDll_SQLite Then Return SetError(3, $SQLITE_MISUSE, 0) If $sDatabase_Filename = Default Or Not IsString($sDatabase_Filename) Then $sDatabase_Filename = ":memory:" Local $tFilename = __SQLite_StringToUtf8Struct($sDatabase_Filename) If @error Then Return SetError(2, @error, 0) If $iAccessMode = Default Then $iAccessMode = BitOR($SQLITE_OPEN_READWRITE, $SQLITE_OPEN_CREATE) Local $bOldBase = FileExists($sDatabase_Filename) ; encoding cannot be changed if base already exists If $iEncoding = Default Then $iEncoding = $SQLITE_ENCODING_UTF8 EndIf Local $avRval = DllCall($__g_hDll_SQLite, "int:cdecl", "sqlite3_open_v2", "struct*", $tFilename, _ ; UTF-8 Database filename "ptr*", 0, _ ; OUT: SQLite db handle "int", $iAccessMode, _ ; database access mode "ptr", 0) If @error Then Return SetError(1, @error, 0) ; DllCall error If $avRval[0] <> $SQLITE_OK Then __SQLite_ReportError($avRval[2], "_SQLite_Open") _SQLite_Close($avRval[2]) Return SetError(-1, $avRval[0], 0) EndIf $__g_hDB_SQLite = $avRval[2] __SQLite_hAdd($__g_ahDBs_SQLite, $avRval[2]) If Not $bOldBase Then Local $aEncoding[3] = ["8", "16", "16be"] _SQLite_Exec($avRval[2], 'PRAGMA encoding="UTF-' & $aEncoding[$iEncoding] & '";') EndIf ; Foreign keys support ; ##################################### If $bFKEnable = Default Then $bFKEnable = False If $bFKEnable Then _SQLite_ForeignKeys($avRval[2], $bFKEnable) If @error Then Local $iError = @error _SQLite_Close($avRval[2]) Return SetError(4, $iError, 0) EndIf EndIf ; ##################################### Return SetExtended($avRval[0], $avRval[2]) EndFunc ;==>_SQLite_Open
    1 point
  2. sorry guys... I forget to take... $sCapabilities $sCapabilities = SetupChrome() $sSession = _WD_CreateSession($sCapabilities) Now its working...
    1 point
  3. Method 1 fails because, as it says HERE, "The onreadystatechange callback function was not implemented as a COM automation event in the IXMLHTTPRequest and IServerXMLHTTPRequest components." As you can see HERE, the IServerXMLHTTPRequest interface does not have any events defined. Therefore, there are no events for ObjEvent() to implement. Method 2 most likely fails because, even if the syntax for setting the .OnReadyStateChange property was correct, I'm not sure you can use DllCallBackRegister/DllCallbackGetPtr in this particular case. I'll leave the discussion of what's wrong with Method 3 to someone else. If your primary goal is to be able to identify ReadyState changes, then using ServerXMLHTTP, I would do something like the this: #AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d #include <Constants.au3> async_serverxmlhttp_get_example() Func async_serverxmlhttp_get_example() Local $oComErr = Null #forceref $oComErr Local $sURL = "https://reqbin.com/echo/get/json" Local $hTimer = -1 Local $iPreviousReadyState = -1, _ $iCurrentReadyState = -1 ;Set local COM error handler $oComErr = ObjEvent("AutoIt.Error", "com_error_handler") If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Failed to set COM error handler") ConsoleWrite(@CRLF & "ServerXMLHTTP Request: " & $sURL & @CRLF) With ObjCreate("MSXML2.ServerXMLHTTP") ;Open asynchronous GET request and send it .Open("GET", $sURL, True) .Send() ;Loop until ready state is complete (4) or timeout occurs (5 seconds) $hTimer = TimerInit() While $iCurrentReadyState <> 4 And TimerDiff($hTimer) < 5000 $iCurrentReadyState = .readystate ;If ready state has changed, then save & display new ready state If $iPreviousReadyState <> $iCurrentReadyState Then $iPreviousReadyState = $iCurrentReadyState ConsoleWrite("Response Ready State: " & get_ready_state_desc($iCurrentReadyState) & @CRLF) EndIf Sleep(10) WEnd If $iCurrentReadyState <> 4 Then Exit ConsoleWrite("Timeout occurred waiting for response" & @CRLF) ;Display response info ConsoleWrite("Response Status: " & StringFormat("%i (%s)", .Status, .StatusText) & @CRLF) ConsoleWrite("Response Body: " & .ResponseText & @CRLF) EndWith EndFunc Func get_ready_state_desc($iReadyState) Local $sDesc = "" Switch $iReadyState Case 0 $sDesc = "Uninitialized" Case 1 $sDesc = "Loading" Case 2 $sDesc = "Loaded" Case 3 $sDesc = "Interactive" Case 4 $sDesc = "Complete" Case Else $sDesc = "Unrecognized" EndSwitch Return StringFormat("%s (%i)", $sDesc, $iReadyState) EndFunc Func com_error_handler($oError) With $oError ConsoleWrite(@CRLF & "COM ERROR DETECTED!" & @CRLF) ConsoleWrite(" Error ScriptLine....... " & .scriptline & @CRLF) ConsoleWrite(" Error Number........... " & StringFormat("0x%08x (%i)", .number, .number) & @CRLF) ConsoleWrite(" Error WinDescription... " & StringStripWS(.windescription, $STR_STRIPTRAILING) & @CRLF) ConsoleWrite(" Error RetCode.......... " & StringFormat("0x%08x (%i)", .retcode, .retcode) & @CRLF) ConsoleWrite(" Error Description...... " & StringStripWS(.description , $STR_STRIPTRAILING) & @CRLF) EndWith Exit EndFunc Console output from the script above: >Running:(3.3.14.5):C:\Portable Apps\AutoIt3\autoit3_x64.exe "C:\Projects\Personal\AutoIt\Test\A3Temp\a3_temp.au3" +>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop. ServerXMLHTTP Request: https://reqbin.com/echo/get/json Response Ready State: Loading (1) Response Ready State: Complete (4) Response Status: 200 (OK) Response Body: {"success":"true"} +>15:31:17 AutoIt3.exe ended.rc:0 +>15:31:17 AutoIt3Wrapper Finished. >Exit code: 0 Time: 0.6689
    1 point
×
×
  • Create New...