Jump to content

How To Save With executeSQLQuery ?


#Madara

Recommended Posts

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) 

Link to comment
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 
) 
Link to comment

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
Link to comment

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) 

Link to comment

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 
  
Link to comment

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) 

Link to comment

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) 

Link to comment
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

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