Jump to content

[HELP] SQLite


Recommended Posts

Hi, I tried to get result of SQL operation from a custom db but it won't work, why that?

  
connection = dbConnect( "sqlite", "groups.db" ) 
  
function test(account, key) 
    for i, values in ipairs(getAccounts()) do 
        if getAccountName(values) == getAccountName(account) then 
                local query = dbQuery(connection, "SELECT value FROM groups WHERE userid = ".. i .." and key = '".. key .."'") 
            return dbPoll(query, -1) 
        end 
    end 
end-- 
  
addCommandHandler("lol", function(p) 
if not isGuestAccount(getPlayerAccount(p)) then 
for i, v in ipairs(test(getPlayerAccount(p), "Ranks")) do 
outputChatBox(v, root) 
end 
end 
end) 
  

Link to comment

Nope. When I put

  
addCommandHandler("lol", function(p) 
for i, v in ipairs(getDBAccountData(getPlayerAccount(p), "Ranks")) do 
outputChatBox(v, root) 
end 
end) 
  

 

It's show nothing but when I put

 

  
addCommandHandler("lol", function(p) 
outputChatBox(getDBAccountData(getPlayerAccount(p), "Ranks"), root) 
end) 
  
  

It's wrote that the result is table.

Link to comment
addCommandHandler("lol", 
function(p) 
    for i, row in ipairs(getDBAccountData(getPlayerAccount(p), "Ranks")) do 
        for column, value in pairs(row) do 
            outputChatBox(column..": "..value) 
        end 
    end 
end) 

Read the second example in the wiki and you will understand.

https://wiki.multitheftauto.com/wiki/DbPoll

Also you don't need to add the db file in the meta.

Link to comment

Try.

connection = dbConnect( "sqlite", "groups.db" ) 
  
function test(account, key) 
    for i, values in ipairs(getAccounts()) do 
        if values == account then 
            local query = dbQuery(connection, "SELECT value FROM groups WHERE userid =? AND key =?", i, key) 
            return dbPoll(query, -1) 
        end 
    end 
end 
  
addCommandHandler("lol", 
function(p) 
    for i, row in ipairs(getDBAccountData(getPlayerAccount(p), "Ranks")) do 
        for column, value in pairs(row) do 
            outputChatBox(column..": "..value) 
        end 
    end 
end) 

Link to comment
Try.
connection = dbConnect( "sqlite", "groups.db" ) 
  
function test(account, key) 
    for i, values in ipairs(getAccounts()) do 
        if values == account then 
            local query = dbQuery(connection, "SELECT value FROM groups WHERE userid =? AND key =?", i, key) 
            return dbPoll(query, -1) 
        end 
    end 
end 
  
addCommandHandler("lol", 
function(p) 
    for i, row in ipairs(getDBAccountData(getPlayerAccount(p), "Ranks")) do 
        for column, value in pairs(row) do 
            outputChatBox(column..": "..value) 
        end 
    end 
end) 

Sorry, I made mistake, the function is not getDBAccountData but test so I fixed it and it's still not working.

Link to comment

What do you mean by the insert code ? Here is my code:

  
    connection = dbConnect( "sqlite", "groups.db" ) 
      
    function test(account, key) 
        for i, values in ipairs(getAccounts()) do 
            if values == account then 
                local query = dbQuery(connection, "SELECT value FROM groups WHERE userid =? AND key =?", i, key) 
                return dbPoll(query, -1) 
            end 
        end 
    end 
      
    addCommandHandler("lol", 
    function(p) 
        for i, row in ipairs(test(getPlayerAccount(p), "Ranks")) do 
            for column, value in ipairs(row) do 
                outputChatBox(column..": "..value, root) 
            end 
        end 
    end) 
  

Link to comment

It's coming from a script so I don't know, what about that ?

CREATE TABLE `groups` ( 
    `id`    INTEGER, 
    `userid`    INTEGER, 
    `key`   TEXT, 
    `value` TEXT, 
    `type`  INTEGER, 
    PRIMARY KEY(id) 
); 

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...