Jump to content
Blank517

_SQLite_FetchData problem

Recommended Posts

Blank517

Hi, when a client sends /logout to the server i want that the server find the id of the client and set his online status to 0

Using 'default' I have no problems, but when I do this with 'blank517' gives me id 10 and then for the database remains online

Database while Blank517 logout:

id  |  username  |  password |   permissions  |  online  |

0   |  default        |    pass1      |           0             |      0       |

1   |  Blank517    |     pass       |           0              |     1       |

Server recv:

Func _Recv_From_Sockets_()
    For $0 = 1 To $max_connections
        $Recv = TCPRecv ($Socket_Data[$0][0],1024)
        If StringLeft($Recv, 1) = "/" Then
            If StringInStr($Recv, "logout") Then
                _SQLite_Query(-1, "SELECT id FROM Users WHERE username = '" & $Socket_Data[$0][1] & "' AND online = '1';", $hQuery)
                While _SQLite_FetchData($hQuery, $aRow) = $SQLITE_OK
                    $sMsg &= $aRow[0]; <-- $sMsg = 10 after FetchData
                WEnd
                _SQLite_Exec(-1, "UPDATE Users SET online = '0' WHERE id = '" & $sMsg & "';")
                For $000 = 1 To $max_connections
                    TCPSend($Socket_Data[$000][0], $Socket_Data[$0][1] & " ha effettuato il logout")
                Next
                TCPCloseSocket($Socket_Data[$0][0])
                $sMsg = Null
            EndIf
        Else
            _Broadcast_To_Sockets_ ($Recv)
        EndIf
    Next
EndFunc

*excuse me for my bad english >_< *

Share this post


Link to post
Share on other sites
AutoBert

run this func:

Func _Recv_From_Sockets_()
    For $0 = 1 To $max_connections
        $Recv = TCPRecv ($Socket_Data[$0][0],1024)
        If StringLeft($Recv, 1) = "/" Then
            If StringInStr($Recv, "logout") Then
                _SQLite_Query(-1, "SELECT id FROM Users WHERE username = '" & $Socket_Data[$0][1] & "' AND online = '1';", $hQuery)
                While _SQLite_FetchData($hQuery, $aRow) = $SQLITE_OK
                    $sMsg &= $aRow[0]; <-- $sMsg = 10 after FetchData
                WEnd
                $sMsg=StringStripWS($sMsg,3)
                ConsoleWrite($sMsg&@CRLF)
                _SQLite_Exec(-1, "UPDATE Users SET online = '0' WHERE id = '" & $sMsg & "';")
                For $000 = 1 To $max_connections
                    TCPSend($Socket_Data[$000][0], $Socket_Data[$0][1] & " ha effettuato il logout")
                Next
                TCPCloseSocket($Socket_Data[$0][0])
                $sMsg = Null
            EndIf
        Else
            _Broadcast_To_Sockets_ ($Recv)
        EndIf
    Next
EndFunc

and post the output of the console.

Share this post


Link to post
Share on other sites
Blank517

The output is 11

Share this post


Link to post
Share on other sites
AutoBert

So all records with id=11 will be set to offline.

Share this post


Link to post
Share on other sites
Blank517
Just now, AutoBert said:

So all records with id=11 will be set to offline.

Ok, but 'Blank517' has id '1' not '11' 

Share this post


Link to post
Share on other sites
AutoBert
11 minutes ago, Blank517 said:

Ok, but 'Blank517' has id '1' not '11' 

Then are 2 records found and '1'&'1' = '11' so test this:

Func _Recv_From_Sockets_()
    For $0 = 1 To $max_connections
        $Recv = TCPRecv ($Socket_Data[$0][0],1024)
        If StringLeft($Recv, 1) = "/" Then
            If StringInStr($Recv, "logout") Then
                _SQLite_Query(-1, "SELECT id FROM Users WHERE username = '" & $Socket_Data[$0][1] & "' AND online = '1';", $hQuery)
                If _SQLite_FetchData($hQuery, $aRow) = $SQLITE_OK Then
                    $sMsg &= $aRow[0]; <-- $sMsg = 10 after FetchData
                    $sMsg=StringStripWS($sMsg,3)    ;test if needed
                    ConsoleWrite($sMsg&@CRLF)
                    _SQLite_Exec(-1, "UPDATE Users SET online = '0' WHERE id = '" & $sMsg & "';")
                EndIf
                For $000 = 1 To $max_connections
                    TCPSend($Socket_Data[$000][0], $Socket_Data[$0][1] & " ha effettuato il logout")
                Next
                TCPCloseSocket($Socket_Data[$0][0])
                $sMsg = Null
            EndIf
        Else
            _Broadcast_To_Sockets_ ($Recv)
        EndIf
    Next
EndFunc

 

Share this post


Link to post
Share on other sites
Blank517

Thanks, now it works ^^

Share this post


Link to post
Share on other sites
Blank517

Last help for sqlite xP

When a user login, even if it is not registered and therefore hasn't an id, the server accepts the connection

_SQLite_Query(-1, "SELECT id FROM Users WHERE username = '" & $Socket_Data[$0][1] & "' AND password = '" & $Socket_Data[$0][2] & "' AND online = 0;", $hQuery)
            If _SQLite_FetchData($hQuery, $aRow) = $SQLITE_OK Then
                $sMsg &= $aRow[0]
            EndIf
            If $sMsg = "" Then
                ConsoleWrite("[Alert] Connection refused! (" & $Socket_Data[$0][1] & ")")
                TCPSend($Socket_Data[$0][0], "Connection refused!" & @CRLF & "There is a problem with the database:" & @CRLF & " - You're not registered" & @CRLF & " - You're already online")
                TCPCloseSocket($Socket_Data[$0][0])
                Return
            EndIf

 

Share this post


Link to post
Share on other sites
kylomas

Blank517,

You are concatenating the query results to the variable $msg...try just setting $msg to the results of the query.

...on a smart phone so cant test and typing is atrocious...

Kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
Blank517
25 minutes ago, kylomas said:

You are concatenating the query results to the variable $msg...try just setting $msg to the results of the query.

The result does not change it always give me a null id

Share this post


Link to post
Share on other sites
kylomas

Blank517,

Are you using a DB shell to interrogate/develop your DB?  SqliteExpert is an excellent tool if you need one.  And it has a free version.

If you do have a DB viewer/shell, verify that ID contains what you think it should.

Also, it does not make sense that you are getting a "11" from fetchdata (post #3).  I'm not sure that problem has been identified yet.

Can you supply a reproducer, DB schema?

kylomas

edit: or your entire script?

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
Blank517
16 hours ago, kylomas said:

Blank517,

You are concatenating the query results to the variable $msg...try just setting $msg to the results of the query.

...on a smart phone so cant test and typing is atrocious...

Kylomas

 

16 hours ago, Blank517 said:

The result does not change it always give me a null id

 

Solved 

_SQLite_Query(-1, "SELECT id FROM Users WHERE username = '" & $Recv[1] & "' AND password = '" & $Recv[2] & "' AND online = 0;", $hQuery)
            If _SQLite_FetchData($hQuery, $aRow) = $SQLITE_OK Then
                $sMsg = $aRow[0]
            EndIf
            If $sMsg = Null Then; <-- not $sMsg = "" but = Null
                ConsoleWrite("[Alert] Connection refused! (" & $Recv[1] & ")")
                TCPSend($Socket_Data[$0][0], "Connection refused!" & @CRLF & "There is a problem with the database:" & @CRLF & " - You're not registered" & @CRLF & " - You're already online")
                TCPCloseSocket($Socket_Data[$0][0])
                Return
            EndIf

 

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

  • Similar Content

    • Eminence
      By Eminence
      Hello,
      Using SQLite, I was trying to select a specific column with having duplicate entries removed by using the DISTINCT function. 
      Local $sQuery = "SELECT DISTINCT supervisor, COUNT(DISTINCT employee_name) FROM data_db What the above snippet does is just list only one supervisor but with the total count of unique employee names in the whole database when it should be listing out all supervisor entries from the database and list the total count of employees per each supervisors. If I remove the COUNT function, it does list out all unique entries of supervisor names from the list. Attached is a screenshot of an example database as well. 
      Any help will be much appreciated. Thank you!

    • Seminko
      By Seminko
      Hey,
      I have a script that uses SQLite. It worked without a hiccup on my W7 system.
      However, last week I bought a new machine, installed W10 and autoit, and now I'm getting an error during _SQLite_Startup.
      Can anyone advise on how to find what's wrong? Tried checking $__g_hPrintCallback_SQLite but that returns nothing.
       
      Tried downloading the latest version of the dll from the link in the function page and I also replaced the default SQLite.dll.au3 that came with AutoIt with the one that came with the latest SQLite version zip.
       
      Relevant parts of my script:
      #include <SQLite.au3> #include <SQLite.dll.au3> _SQLite_Startup(@ScriptDir & "\Config\sqlite3_x64.dll", False, 1) If @error Then MsgBox(16, "SQLite Error", "SQLite3.dll Can't be Loaded! - " & $__g_hPrintCallback_SQLite & @CRLF & @CRLF & "Exiting application / Zavolej Honzovi") Exit -1 EndIf  
      Thanks
      S.
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I am building a management for the company I work with, and I just imported a real amount of rows ( about 29000 ), in my SQLite DB.
      The thing I am not understanding, is the time that the script takes to build this amount of rows in the ListView.
      I didn't measure it, but I think it took 2 minutes or so to create each ListView item...
      It is normal that it takes so much time?
      What can I do to improve the creation of the items?

      Here's the code I am using to query and to create ListView items...
      ; Articles ListView: Global $lvwArticles = GUICtrlCreateListView("ID|Fornitore|Codice|Descrizione|EU|Prezzo|Sconto Applicato|Note", 14, 87, 1507, 660, BitOR($GUI_SS_DEFAULT_LISTVIEW,$LVS_SORTASCENDING,$LVS_SORTDESCENDING), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES,$LVS_EX_FULLROWSELECT)) ; Query $strQuery = "SELECT * FROM ARTICOLI;" ; Query Execution _SQLite_GetTable2d($objDatabase, $strQuery, $arrResult, $intRows, $intColumns) If @error Then ; Error Handling Else ; Cleaning the ListView _GUICtrlListView_DeleteAllItems($lvwArticles) If @error Then ; Error Handling Else ; No records in the Table If UBound($arrResult) < 2 Then ; Error Handling Else _GUICtrlListView_BeginUpdate($lvwArticles) For $intCounter = 1 To UBound($arrResult) - 1 $strListViewItem = $arrResult[$intCounter][0] & "|" & _ $arrResult[$intCounter][1] & "|" & _ $arrResult[$intCounter][2] & "|" & _ $arrResult[$intCounter][3] & "|" & _ $arrResult[$intCounter][4] & "|" & _ $arrResult[$intCounter][5] & "|" & _ $arrResult[$intCounter][6] & "|" & _ $arrResult[$intCounter][7] $objListViewItem = GUICtrlCreateListViewItem($strListViewItem, $lvwArticles) Next _GUICtrlListView_EndUpdate($lvwArticles) EndIf EndIf EndIf Thanks in advance


      Best Regards.
    • dangr82
      By dangr82
       
      I have created this function for a database, but I can not make it work. I always have two error messages: "not an error" ... and the file created, in the script directory, does not contain anything.
      Global $sDBName = "Hen.db" Func DatabaseTable() Local $sConnDB _SQLite_Startup() If @error Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", "SQLite.dll Can't be Loaded!") Exit -1 EndIf $sConnDB = _SQLite_Open($sDBName) If @error Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", "Can't Load Database!") Exit -1 EndIf If Not _SQLite_Exec($sDBName, 'CREATE TABLE Animal ("Name", "Age");') = $SQLITE_OK Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", _SQLite_ErrMsg()) If Not _SQLite_Exec($sDBName, 'INSERT INTO Animale VALUES ("Charlie","5");') = $SQLITE_OK Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", _SQLite_ErrMsg()) _SQLite_Close($sConnDB) _SQLite_Shutdown() EndFunc  
    • Eminence
      By Eminence
      Hello,
      I've been searching on how to select the first row in my database which includes a time column based on the latest start_time date for today's date.
      This is my current code:
      Local $iCurrentDate = _Now() Local $sqlHandle = _dbOpen($sDbPath) Local $aResult, $iRows, $iCols _SQLite_GetTable2d($sqlHandle, "SELECT start_time FROM " & $srawDb & " ORDER BY datetime(start_time) DESC LIMIT 1", $aResult, $iRows, $iCols) _ArrayDisplay($aResult) _dbClose($sDbpath) What it does is it gets the first row entry in the database however it does not select the latest start_time entry in the database, it always select the first row. I've tried changing DESC to ASC to see if that will do it but the issue still remains. Any thoughts on this? Thanks in advance.
       
      *EDIT
      The date format in the database is by MM/DD/YYYY HH:MM:SS.
×