Jump to content

How To Save With executeSQLQuery ?


#Madara

Recommended Posts

Posted

Hi guys ,

Today I made Chat Log and i want

to save the Log ( Text ) in the grid list when resource start or stop

SPTDrI.png

how i save that with using executeSQLQuery ?

i need examples please because i want to learn how to connect sql with gridlist .

Posted

It's quite easy, create a table like

executeSQLQuery("CREATE TABLE IF NOT EXISTS logs (name TEXT, message TEXT)") 

Then you can insert the logs with

executeSQLQuery("INSERT INTO logs (name, message) VALUES(?, ?)", getPlayerName(player), message) 

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast
Posted
It's quite easy, create a table like
executeSQLQuery("CREATE TABLE IF NOT EXISTS logs (name TEXT, message TEXT)") 

Then you can insert the logs with

executeSQLQuery("INSERT INTO logs (name, message) VALUES(?, ?)", getPlayerName(player), message) 

ok now how i put what i save in sql in grid list ?

Client :

  
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        Window = guiCreateWindow(373, 189, 519, 426, "..: [ Chat Log v1.0 ] :..", false) 
        guiWindowSetSizable(Window, false) 
        guiSetAlpha(Window, 1.00) 
        guiSetVisible(Window,false) 
        GridList = guiCreateGridList(9, 24, 500, 392, false, Window) 
        plrName = guiGridListAddColumn(GridList, "# Player Name :", 0.4)  
        plrMsg = guiGridListAddColumn(GridList, "# Chat Message :", 0.4)    
    end 
) 
  
addEvent("INSERT!",true) 
addEventHandler("INSERT!",root, 
function(name , msg) 
guiGridListSetItemText (GridList,guiGridListAddRow ( GridList ), plrName,tostring(name), false, false ) 
guiGridListSetItemText (GridList,guiGridListAddRow ( GridList ), plrMsg,tostring(msg), false, false ) 
end 
) 
  
local Key = "F5" 
  
bindKey(Key,"down", 
  
function () 
   guiSetVisible( Window , not guiGetVisible(Window)) 
   showCursor(guiGetVisible(Window)) 
  end 
) 

Server :

addEventHandler("onResourceStart",root, 
function() 
executeSQLQuery("CREATE TABLE IF NOT EXISTS logs (name TEXT, message TEXT)") 
end 
) 
  
addEventHandler("onPlayerChat",root, 
function(msg,_) 
if ( msg ~= "") or ( msg ~= " ") then 
local name = getPlayerName(source) 
triggerClientEvent(source,"INSERT!",source,name,msg) 
executeSQLQuery("INSERT INTO logs (name, message) VALUES(?, ?)", getPlayerName(source), msg) 
end 
end 
) 
Posted

You mean how to fetch data?

executeSQLQuery("SELECT * FROM logs") 

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast
Posted

That will return a table, send it to client and then loop through it and add them to gridlist.

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast
Posted

Nope, that code I sent you will return Lua array and you'll have to send it to client and then loop through it.

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast
Posted
Nope, that code I sent you will return Lua array and you'll have to send it to client and then loop through it.

can u give me example please ?

Posted (edited)

like this ?

Client :

  
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        Window = guiCreateWindow(373, 189, 519, 426, "..: [ Chat Log v1.0 ] :..", false) 
        guiWindowSetSizable(Window, false) 
        guiSetAlpha(Window, 1.00) 
        guiSetVisible(Window,false) 
        GridList = guiCreateGridList(9, 24, 500, 392, false, Window) 
        plrName = guiGridListAddColumn(GridList, "# Player Name :", 0.4)  
        plrMsg = guiGridListAddColumn(GridList, "# Chat Message :", 0.4)    
    end 
) 
  
addEvent("INSERT!",true) 
addEventHandler("INSERT!",root, 
function(name , msg) 
guiGridListSetItemText (GridList,guiGridListAddRow ( GridList ), plrName,tostring(name), false, false ) 
guiGridListSetItemText (GridList,guiGridListAddRow ( GridList ), plrMsg,tostring(msg), false, false ) 
end 
) 
  
local Key = "F5" 
  
bindKey(Key,"down", 
  
function () 
   guiSetVisible( Window , not guiGetVisible(Window)) 
   showCursor(guiGetVisible(Window)) 
  end 
) 
addEvent("Send",true) 
addEventHandler("Send",root, 
function(sql) 
for k , v in pairs (sql) then 
guiGridListSetItemText (GridList,guiGridListAddRow ( GridList ), plrName,tostring(v), false, false ) 
guiGridListSetItemText (GridList,guiGridListAddRow ( GridList ), plrMsg,tostring(v), false, false ) 
end 
) 

Server :

addEventHandler("onResourceStart",root, 
function() 
executeSQLQuery("CREATE TABLE IF NOT EXISTS logs (name TEXT, message TEXT)") 
Save() 
end 
) 
  
addEventHandler('onResourceStop',root, 
function() 
Update() 
end 
) 
addEventHandler("onPlayerChat",root, 
function(msg,_) 
if ( msg ~= "") or ( msg ~= " ") then 
local name = getPlayerName(source) 
triggerClientEvent(source,"INSERT!",source,name,msg) 
executeSQLQuery("INSERT INTO logs (name, message) VALUES(?, ?)", getPlayerName(source), msg) 
end 
end 
) 
  
function Save() 
local Save = executeSQLQuery("SELECT * FROM logs") 
triggerClientEvent(client,'Send'client,Save) 
end 
  
function Update() 
executeSQLQuery('UPDATE `logs` SET message =? WHERE name =?',msg,name) 
end 
  
Edited by Guest
Posted

Example:

Server:

local logs = executeSQLQuery("SELECT * FROM logs") 
if #logs > 0 then 
    triggerClientEvent("addLogs", root, logs) 
end 

Client:

function addLogs(logs) 
    for i, v in ipairs(logs) do 
        guiGridListSetItemText(GridList, guiGridListAddRow(GridList), plrName, v.name, false, false) 
        guiGridListSetItemText(GridList, guiGridListAddRow(GridList), plrMsg, v.message, false, false) 
    end 
end 
addEvent("addLogs", true) 
addEventHandler("addLogs", root, addLogs) 

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast
Posted

DebugScript :

ERROR: Server triggered clientside event addLogs, but event is not added clientside 

Client :

  
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        Window = guiCreateWindow(373, 189, 519, 426, "..: [ Chat Log v1.0 ] :..", false) 
        guiWindowSetSizable(Window, false) 
        guiSetAlpha(Window, 1.00) 
        guiSetVisible(Window,false) 
        GridList = guiCreateGridList(9, 24, 500, 392, false, Window) 
        plrName = guiGridListAddColumn(GridList, "# Player Name :", 0.4)  
        plrMsg = guiGridListAddColumn(GridList, "# Chat Message :", 0.4)    
    end 
) 
  
addEvent("INSERT!",true) 
addEventHandler("INSERT!",root, 
function(name , msg) 
guiGridListSetItemText (GridList,guiGridListAddRow ( GridList ), plrName,tostring(name), false, false ) 
guiGridListSetItemText (GridList,guiGridListAddRow ( GridList ), plrMsg,tostring(msg), false, false ) 
end 
) 
  
local Key = "F5" 
  
bindKey(Key,"down", 
  
function () 
   guiSetVisible( Window , not guiGetVisible(Window)) 
   showCursor(guiGetVisible(Window)) 
  end 
) 
  
function addLogs(logs) 
    for i, v in ipairs(logs) do 
        guiGridListSetItemText(GridList, guiGridListAddRow(GridList), plrName, v.name, false, false) 
        guiGridListSetItemText(GridList, guiGridListAddRow(GridList), plrMsg, v.message, false, false) 
    end 
end 
addEvent("addLogs", true) 
addEventHandler("addLogs", root, addLogs) 

Server :

addEventHandler("onResourceStart",root, 
function() 
executeSQLQuery("CREATE TABLE IF NOT EXISTS logs (name TEXT, message TEXT)") 
Load() 
end 
) 
  
addEventHandler("onPlayerChat",root, 
function(msg,_) 
if ( msg ~= "") or ( msg ~= " ") then 
local name = getPlayerName(source) 
triggerClientEvent(source,"INSERT!",source,name,msg) 
executeSQLQuery("INSERT INTO logs (name, message) VALUES(?, ?)", getPlayerName(source), msg) 
end 
end 
) 
  
function Load() 
local logs = executeSQLQuery("SELECT * FROM logs") 
if #logs > 0 then 
    triggerClientEvent("addLogs", root, logs) 
end 
end 
  
Posted

Client side must be first in meta to load.

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast
Posted
Client side must be first in meta to load.

it's first and debugscript say to me

ERROR: Server triggered clientside event addLogs, but event is not added clientside 

meta

    

Posted

I don't understand, why do you bold your text? It's always the same.

addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        Window = guiCreateWindow(373, 189, 519, 426, "..: [ Chat Log v1.0 ] :..", false) 
        guiWindowSetSizable(Window, false) 
        guiSetAlpha(Window, 1.00) 
        guiSetVisible(Window,false) 
        GridList = guiCreateGridList(9, 24, 500, 392, false, Window) 
        plrName = guiGridListAddColumn(GridList, "# Player Name :", 0.4) 
        plrMsg = guiGridListAddColumn(GridList, "# Chat Message :", 0.4)   
        triggerServerEvent("requestData", localPlayer) 
    end 
) 

function Load() 
local logs = executeSQLQuery("SELECT * FROM logs") 
if #logs > 0 then 
    triggerClientEvent(source, "addLogs", source, logs) 
end 
end 
addEvent("requestData", true) 
addEventHandler("requestData", root, Load) 

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast
Posted

That's because you add rows twice for each column.

guiGridListSetItemText(GridList, guiGridListAddRow(GridList), plrName, v.name, false, false) 
guiGridListSetItemText(GridList, guiGridListAddRow(GridList), plrMsg, v.message, false, false) 

Add it only once.

local row = guiGridListAddRow(GridList) 
guiGridListSetItemText(GridList, row, plrName, v.name, false, false) 
guiGridListSetItemText(GridList, row, plrMsg, v.message, false, false) 

Do you require a paid scripter? Contact me! (Unavailable) Currently I am experienced in Lua, PHP, HTML, CSS, SQL and JS.

Developer and owner of

https://projectbea.st - Project Beast
Posted
That's because you add rows twice for each column.
guiGridListSetItemText(GridList, guiGridListAddRow(GridList), plrName, v.name, false, false) 
guiGridListSetItemText(GridList, guiGridListAddRow(GridList), plrMsg, v.message, false, false) 

Add it only once.

local row = guiGridListAddRow(GridList) 
guiGridListSetItemText(GridList, row, plrName, v.name, false, false) 
guiGridListSetItemText(GridList, row, plrMsg, v.message, false, false) 

ah thank you very much :D

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