Jump to content

Login System Using Autoit And MYSQL


Recommended Posts

Hi @Fhelipe.

I'm not sure if you realize MySQL requires a server.

I've made an example in SQLite, however it's almost the same with the queries for MySQL.

I know the passwords SHOULD be hashed, but this is just a bare bones example.

#include <SQLite.au3>
#include <Array.au3>

$sSQLite = _SQLite_Startup("sqlite3.dll")
If @error<>0 Then Exit MsgBox(0, "", "SQLite Error", "SQLite3.dll Can't be Loaded!")
$hSQLiteDB = _SQLite_Open(@ScriptName&".sqlite3")
If @error<>0 Then Exit MsgBox(0, "", "Can't open or create a Database!")
_SQLite_Exec($hSQLiteDB, "CREATE TABLE IF NOT EXISTS `Users` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT, `username` TEXT NOT NULL, `password` TEXT NOT NULL)")

;LOGIN example
$username = "Zummey"
$password = "xd90fe10"

Login($username, $password);will fail upon first run
If @error<>0 Then ConsoleWrite("Login failed"&@CRLF)

;inserts user if not exist
Create_User($username, $password)


_ArrayDisplay(Login($username, $password))

_SQLite_Close($hSQLiteDB)
_SQLite_Shutdown()

;Success:
;   Found: Array(ID, username)
;   Not Found: 0
;Failure: -1
Func Login($username, $password)
    Local $hQuery = 0
    Local $aRow = 0
    ;~ _SQLite_Query($hSQLiteDB, "INSERT INTO `Users` values ()"
    _SQLite_Query($hSQLiteDB, "SELECT `ID`, `username` FROM `Users` WHERE `username`=? AND `password`=? LIMIT 1", $hQuery)
    _SQLite_BindText($hQuery, 1, $username)
    _SQLite_BindText($hQuery, 2, $password)
    Local $iResult = _SQLite_FetchData($hQuery, $aRow)
    If $iResult = $SQLITE_OK Then Return $aRow
    Return SetError($iResult, 0, ($iResult=$SQLITE_DONE)?0:-1)
EndFunc

Func Create_User($username, $password)
    Local $hQuery = 0
    _SQLite_Query($hSQLiteDB, "INSERT INTO `Users` (`username`, `password`) SELECT ?, ? WHERE NOT EXISTS (SELECT 1 FROM `Users` WHERE `username`=?)", $hQuery)
    _SQLite_BindText($hQuery, 1, $username)
    _SQLite_BindText($hQuery, 2, $password)
    _SQLite_BindText($hQuery, 3, $username)
    Local $iRval_Step = DllCall($__g_hDll_SQLite, "int:cdecl", "sqlite3_step", "ptr", $hQuery)
    If @error Then Return SetError(1, @error, $SQLITE_MISUSE) ; DllCall error
    $iChanges = _SQLite_Changes($hSQLiteDB)
    _SQLite_QueryFinalize($hQuery)
    If $iRval_Step=$SQLITE_DONE And $iChanges>0 Then Return 1
    Return SetError($iRval_Step, 0, 0)
EndFunc

;Source: https://autoit-script.ru/index.php?topic=21090.15#msg_124711
Func _SQLite_BindText($hQuery, $iRowID, $sTextRow)
    Local $iRval = DllCall($__g_hDll_SQLite, "int:cdecl", "sqlite3_bind_text16", _
            "ptr", $hQuery, _
            "int", $iRowID, _
            "wstr", $sTextRow, _
            "int", -1, _
            "ptr", NULL)
    If @error Then Return SetError(1, @error, $SQLITE_MISUSE) ; DllCall error
    If $iRval[0] <> $SQLITE_OK Then
        Return SetError(-1, 0, $iRval[0])
    EndIf
    Return $iRval[0]
EndFunc

Hope this can help you getting started.

Link to comment
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
 Share

×
×
  • Create New...