Jump to content

SQL Question


Wei

Recommended Posts

executeSQLCreateTable ( "test", "username TEXT" ) 
  
function addInfoToSQL()  
local sourcename = getPlayerName ( source ) 
    result = executeSQLSelect ( "test", "username", "username = '" .. getAccountName(getPlayerAccount(source)) .. "'" ) 
    if ( result == false ) then 
        outputChatBox ( "This is your first time here! Welcome " .. sourcename .. "!", source ) 
        executeSQLInsert ( "players","'" .. getAccountName(getPlayerAccount(source)) .. "'" ) 
    else 
        outputChatBox ( "Welcome back " .. ??? .. "!", source ) -- what should I put here to get player username ? 
        executeSQLUpdate ( "test", "username = '" .. sourcename .. "'" ) 
    end 
end 
addEventHandler ( "onPlayerLogin", getRootElement(), addInfoToSQL) 

Look at line 10

Link to comment
executeSQLCreateTable ( "test", "username TEXT" ) 
  
function addInfoToSQL()  
local sourcename = getPlayerName ( source ) 
local acc = getAccountName ( getPlayerAccount ( source ) ) 
    result = executeSQLSelect ( "test", "username", "username = '" .. getAccountName(getPlayerAccount(source)) .. "'" ) 
    if ( result == false ) then 
        outputChatBox ( "This is your first time here! Welcome " .. sourcename .. "!", source ) 
        executeSQLInsert ( "players","'" .. getAccountName(getPlayerAccount(source)) .. "'" ) 
    else 
        outputChatBox ( "Welcome back " .. acc .. "!", source ) -- what should I put here to get player username ? 
        executeSQLUpdate ( "test", "username = '" .. sourcename .. "'" ) 
    end 
end 
addEventHandler ( "onPlayerLogin", getRootElement(), addInfoToSQL) 

Link to comment

On this you can find:

"Returns a 2-dimensional table where the results are stored as table [row_index] [column_name]"

I think you can just put:

outputChatBox ( "Welcome back " .. result[1]["username"] .. "!", source ) 

The first row_index in your result table is the first row who matches the SELECT request conditions in your SQL table, the column_name "username" contains what you need to retrieve, I think. I can be wrong, tell me if it works.

Link to comment

This seems to works, I've changed the test condition on result by the one provided in the wiki:

executeSQLCreateTable ( "test", "username TEXT" ) 
function addInfoToSQL()  
local sourcename = getPlayerName ( source ) 
    local result = executeSQLSelect ( "test", "username", "username = '" .. getAccountName(getPlayerAccount(source)) .. "'" ) 
    if ( type( result ) == "table" and #result == 0 ) or not result then 
        outputChatBox ( "This is your first time here! Welcome " .. sourcename .. "!", source ) 
        executeSQLInsert ( "test","'" .. getAccountName(getPlayerAccount(source)) .. "'" ) 
    else 
        outputChatBox ( "Welcome back " .. result[1]["username"] .. "!", source ) -- what should I put here to get player username ? 
        executeSQLUpdate ( "test", "username = '" .. sourcename .. "'" ) 
    end 
end 
addEventHandler ( "onPlayerLogin", getRootElement(), addInfoToSQL) 

Link to comment

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
  • Recently Browsing   0 members

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