06-01-2013, 05:41 PM
To get the values out, you just use a SELECT sql command:
Then, there's the Lua binding, which dictates how to get to the values that the SQL database returns. It turns out the binding is pretty straightforward. You call the "exec" function on the database object, it has three parameters, the sql command, callback and userdata. It then calls the callback for each row of data that the sql command produced. The userdata parameter is given to the callback as well, it is a way to pass any kind of information through. So, for example, this will be a skeleton code for you:
Code:
SELECT * FROM password WHERE name='Niels';
Then, there's the Lua binding, which dictates how to get to the values that the SQL database returns. It turns out the binding is pretty straightforward. You call the "exec" function on the database object, it has three parameters, the sql command, callback and userdata. It then calls the callback for each row of data that the sql command produced. The userdata parameter is given to the callback as well, it is a way to pass any kind of information through. So, for example, this will be a skeleton code for you:
-- Somewhere global: PwdDB = {}; -- The password database object -- In the Initialize function: PwdDB, ErrCode, ErrMsg = sqlite3.open("pwd.sqlite"); -- TODO: Handle errors in ErrCode -- In the login hook: local UserName = ...; -- Username of the player logging in local PasswordGiven = ...; -- What password is the user logging in with? local ShouldAllowLogin = false; local function ProcessRow(UserData, NumCols, Values, Names) for i = 1, NumCols do if (Names[i] == "Password") then -- "Password" is the column name ShouldAllowLogin = (Values[i] == PasswordGiven); end end return 0; end local Res = PwdDB:exec("SELECT * FROM Passwords WHERE Username=\"" .. UserName .."\"", ProcessRow, nil); if (Res ~= sqlite3.OK) then LOG("SQL query failed: " .. Res .. " (" .. PwdDB:errmsg() .. ")"); end; -- ShouldAllowLogin now has the bool value saying whether to allow the user in or not