Jump to content

How To get value from Database?


bookkc

Recommended Posts

i need to get value from database

i try this

local function Chaaat(event, player)
local result = CharDBQuery("SELECT date FROM premium WHERE AccountId = "..player:GetAccountId())

if result then
 repeat
        local Dateee = result:GetUInt32(1)
        until not result:NextRow()
end
end
RegisterPlayerEvent(18, Chaaat) 

i need to put date in local Dateee. how to do this??

Link to comment
Share on other sites

try to make this

local DatabaseCache = {}
 
local function LoadDatabase(event, player)
    local ExampleQuery = CharDBQuery("SELECT date FROM premium WHERE AccountId = "..player:GetAccountId());
    if(ExampleQuery)then
        repeat
            DatabaseCache[ExampleQuery:GetUInt32(0)] = {
            AccountID = ExampleQuery:GetUInt32(1),
            active = ExampleQuery:GetUInt32(2)
            };
        until not ExampleQuery:NextRow()
    end
end

RegisterPlayerEvent(18, LoadDatabase)

but have error

2018-04-18 12:36:19 ERROR Eluna: lua_scripts/date.lua:8: bad argument #1 to 'GetUInt32' (trying to access invalid field index 1. There are 1 fields available and the indexes start from 0)

image.png.6d68bbf9cfb80ea348b58158dc0a0622.pngthis my DB

Link to comment
Share on other sites

okay, i try this

local DatabaseCache = {}
 
local function LoadDatabase(event, player)
    local ExampleQuery = CharDBQuery("SELECT date FROM premium WHERE AccountId = "..player:GetAccountId());
    if(ExampleQuery)then
        repeat
            DatabaseCache[ExampleQuery:GetUInt32(0)] = {
            AccountID = ExampleQuery:GetUInt32(1),
            active = ExampleQuery:GetUInt32(2),
            datee = ExampleQuery:GetUInt32(3)
            };
        until not ExampleQuery:NextRow()
    end
end

RegisterPlayerEvent(18, LoadDatabase)

but get

2018-04-18 12:45:58 ERROR Eluna: lua_scripts/date.lua:8: bad argument #1 to 'GetUInt32' (trying to access invalid field index 1. There are 1 fields available and the indexes start from 0)

wth?

Link to comment
Share on other sites

You need to update your query to select all the necessary information that you need, you're currently only selecting date, not everything else you need as well 

local DatabaseCache = {}
 
local function LoadDatabase(event, player)
    local ExampleQuery = CharDBQuery("SELECT active, date FROM premium WHERE AccountId = "..player:GetAccountId());
    if(ExampleQuery)then
        repeat
            DatabaseCache[player:GetAccountId()] = {
            active = ExampleQuery:GetUInt32(0),
            datee = ExampleQuery:GetUInt32(1)
            };
        until not ExampleQuery:NextRow()
    end
end

RegisterPlayerEvent(18, LoadDatabase)
Link to comment
Share on other sites

local DatabaseCache = {}
 
local function LoadDatabase(event, player)
    local ExampleQuery = CharDBQuery("SELECT active, date FROM premium WHERE AccountId = "..player:GetAccountId());
    if(ExampleQuery)then
        repeat
            DatabaseCache[player:GetAccountId()] = {
            active = ExampleQuery:GetUInt32(0),
            datee = ExampleQuery:GetUInt32(1)
            };
        until not ExampleQuery:NextRow()
    end
    print(datee)
end

RegisterPlayerEvent(18, LoadDatabase)

But i have nul in datee too... can you help??

Link to comment
Share on other sites

i do this, and this is work!

local function LoadDatabase(event, player)
local resultado = CharDBQuery("SELECT active, date FROM premium WHERE AccountId = "..player:GetAccountId())
        if(resultado) then
            repeat
                local puntos = 0
                active = resultado:GetString(0);
                datee = resultado:GetUInt32(1);
            until not resultado:NextRow()    
        end
        print (datee)
end

RegisterPlayerEvent(18, LoadDatabase)

 

Link to comment
Share on other sites

Yes, what you did below would also work, the original example however stores the data specifically to a Lua table so the information can be used/re-used at a later time without having to query the database again. Instead of printing "datee" directly like you tried to, you would use the table DatabaseCache with the account ID as a key to call the specific information, like shown below:

local DatabaseCache = {}
 
local function LoadDatabase(event, player)
    local ExampleQuery = CharDBQuery("SELECT active, date FROM premium WHERE AccountId = "..player:GetAccountId());
    if(ExampleQuery)then
        repeat
            DatabaseCache[player:GetAccountId()] = {
            active = ExampleQuery:GetUInt32(0),
            datee = ExampleQuery:GetUInt32(1)
            };
        until not ExampleQuery:NextRow()
    end

    print(DatabaseCache[player:GetAccountId()].datee)
end

RegisterPlayerEvent(18, LoadDatabase)

 

If you just want a single one-time use query, then you should do this:

local function LoadDatabase(event, player)
        local resultado = CharDBQuery("SELECT active, date FROM premium WHERE AccountId = "..player:GetAccountId())
        
        if(resultado) then
                local active, date = resultado:GetString(0), resultado:GetUInt32(1);
        end

        print (datee)
end

RegisterPlayerEvent(18, LoadDatabase)
Link to comment
Share on other sites

So can you help with this error ??

local function LoadDatabase(event, player)
local result = CharDBQuery("SELECT active, date FROM premium WHERE AccountId = "..player:GetAccountId())
        if(result) then
            repeat
                active = result:GetString(0);
                datedb = result:GetUInt32(1);
            until not result:NextRow()    
        end
        print (datedb)
        local curdate=os.date("%Y%m%d")
        print(curdate)
if curdate <= datedb then
print "OK!"
else
print "NO OK!"
end
end

RegisterPlayerEvent(18, LoadDatabase)

 

2018-04-18 19:21:16 ERROR Eluna: lua_scripts/exchanger.lua:12: attempt to compare string with number

 

probem with this line 

if curdate <= datedb then

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use