Jump to content

SQLite Users DataBase


Andreik
 Share

Recommended Posts

I want to create a database to keep user accounts using SQLite functions. I want to can add and get info about users like in this example that use a simple text file as database. Can someone help me?

$MAIN = GUICreate("UserDB",155,55)
$USER = GUICtrlCreateInput("",5,5,100,20)
$PASS = GUICtrlCreateInput("",5,30,100,20)
$ADD = GUICtrlCreateButton("ADD",110,5,40,20)
$GET = GUICtrlCreateButton("GET",110,30,40,20)
GUISetState(@SW_SHOW,$MAIN)

While True
    $MSG = GUIGetMsg()
    Switch $MSG
        Case -3
            Exit
        Case $ADD
            $USR = GUICtrlRead($USER)
            $PWD = GUICtrlRead($PASS)
            $EXIST = GetUser($USR)
            $DB = FileOpen("mydb.db",1)
            If Not $EXIST Then FileWriteLine($DB, $USR & @TAB & $PWD)
            FileClose($DB)
            GUICtrlSetData($USER,"")
            GUICtrlSetData($PASS,"")
        Case $GET
            $RESULT = GetUser(GUICtrlRead($USER),1)
            If Not IsArray($RESULT) Then
                GUICtrlSetData($USER,"No user found")
                GUICtrlSetData($PASS,"")
            Else
                GUICtrlSetData($USER,$RESULT[1])
                GUICtrlSetData($PASS,$RESULT[2])
            EndIf
    EndSwitch
    Sleep(10)
WEnd

Func GetUser($USER,$MODE=0)
    Local $DB = FileOpen("mydb.db",0)
    Local $EXIST = False
    Local $INDEX = 1
    While True
        $LINE = FileReadLine($DB,$INDEX)
        If @error Then ExitLoop
        $SPLIT = StringSplit($LINE,@TAB)
        If $SPLIT[1] = $USER Then ;User already in DB
            If $MODE = 1 Then
                Return $SPLIT
            Else
                $EXIST = True
                ExitLoop
            EndIf
        EndIf
        $INDEX += 1
    WEnd
    FileClose($DB)
    Return $EXIST
EndFunc

When the words fail... music speaks.

Link to comment
Share on other sites

I want to create a database to keep user accounts using SQLite functions. I want to can add and get info about users like in this example that use a simple text file as database. Can someone help me?

$MAIN = GUICreate("UserDB",155,55)
$USER = GUICtrlCreateInput("",5,5,100,20)
$PASS = GUICtrlCreateInput("",5,30,100,20)
$ADD = GUICtrlCreateButton("ADD",110,5,40,20)
$GET = GUICtrlCreateButton("GET",110,30,40,20)
GUISetState(@SW_SHOW,$MAIN)

While True
    $MSG = GUIGetMsg()
    Switch $MSG
        Case -3
            Exit
        Case $ADD
            $USR = GUICtrlRead($USER)
            $PWD = GUICtrlRead($PASS)
            $EXIST = GetUser($USR)
            $DB = FileOpen("mydb.db",1)
            If Not $EXIST Then FileWriteLine($DB, $USR & @TAB & $PWD)
            FileClose($DB)
            GUICtrlSetData($USER,"")
            GUICtrlSetData($PASS,"")
        Case $GET
            $RESULT = GetUser(GUICtrlRead($USER),1)
            If Not IsArray($RESULT) Then
                GUICtrlSetData($USER,"No user found")
                GUICtrlSetData($PASS,"")
            Else
                GUICtrlSetData($USER,$RESULT[1])
                GUICtrlSetData($PASS,$RESULT[2])
            EndIf
    EndSwitch
    Sleep(10)
WEnd

Func GetUser($USER,$MODE=0)
    Local $DB = FileOpen("mydb.db",0)
    Local $EXIST = False
    Local $INDEX = 1
    While True
        $LINE = FileReadLine($DB,$INDEX)
        If @error Then ExitLoop
        $SPLIT = StringSplit($LINE,@TAB)
        If $SPLIT[1] = $USER Then ;User already in DB
            If $MODE = 1 Then
                Return $SPLIT
            Else
                $EXIST = True
                ExitLoop
            EndIf
        EndIf
        $INDEX += 1
    WEnd
    FileClose($DB)
    Return $EXIST
EndFunc

Hi,

with your example you store Usernames and their passwords. This is working.

It's not clear, what is your problem? Do you want to store more information?

If yes have a look at line

If Not $EXIST Then FileWriteLine($DB, $USR & @TAB & $PWD)

If you want to store more data, just change line and add more 'fields' and some more Inputfields.

;-))

Stefan

Edited by 99ojo
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...