AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Suggestions / Requests (https://forums.alliedmods.net/forumdisplay.php?f=12)
-   -   Solved Send text to database (https://forums.alliedmods.net/showthread.php?t=316275)

4ever16 05-15-2019 05:15

Send text to database
 
When i say .text then a text message is sent to database.
When new .text is send the the old one is deleted.

say .text "this is my message"

Godofwar 05-15-2019 13:38

Re: Send text to database
 
u should be more exactly. I am sure, nobody understand your request.

AmXDusT 05-15-2019 13:54

Re: Send text to database
 
Hi, this should work:
Quote:

.text my message
will save "my message"
PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <sqlx>


#define TEXT_ACCESS ADMIN_BAN
new const host[] = ""
new const user[] = ""
new const pass[] = ""
new const db[] = ""
new const table[] = "sql_text"


#define PLUGIN "SQL Save Note"
#define VERSION "1.0"

new Handle:g_Tuple
public plugin_init(){
    
register_plugin(PLUGINVERSION"DusT")
    
register_clcmd("say""sqlSay")
}

public 
plugin_cfg(){
    
set_task(0.5"connectDb")
}

public 
sqlSay(id){
    new 
arg[6]
    
read_argv(1argcharsmax(arg))
    if(!
equali(arg".text"))
        return 
PLUGIN_CONTINUE
    
    
if(!(get_user_flags(id) & TEXT_ACCESS))
        return 
PLUGIN_HANDLED
    
new message192 ], length strlen(arg) + 1
    read_args
(messagecharsmax(message))
    
remove_quotes(message)
    
remove_quotes(message[length])
    if(!
message[length])
        return 
PLUGIN_HANDLED

    updateDb
(idmessage[length])
    
client_print(idprint_chat"* Message Saved")
    
    return 
PLUGIN_HANDLED
}

public 
connectDb(){
    
g_Tuple SQL_MakeDbTuple(hostuserpassdb)
    
    new 
query[512]
    
formatex(querycharsmax(query), "CREATE TABLE IF NOT EXISTS %s(admin VARCHAR(31) NOT NULL,admin_steamid VARCHAR(35) NOT NULL,datetime DATETIME NOT NULL,message VARCHAR(192) NOT NULL)"table)
    
SQL_ThreadQuery(g_Tuple"checkTable"query)
}
public 
checkTable(failStateHandle:query, const error[], errNum)
{
    if (
errNum)
        
set_fail_state(error)
}
public 
resetDb(){
    new 
query[128]
    
formatex(querycharsmax(query), "DELETE FROM %s"table)
    
SQL_ThreadQuery(g_Tuple"ignoreHandle"query)
}
public 
updateDb(idmessage[]){
    
resetDb()
    new 
name[32], escName[64], steamid[35], escMessage280 ]
    
get_user_name(idnamecharsmax(name))
    
SQL_QuoteString(Empty_HandleescNamecharsmax(escName), name)
    
SQL_QuoteString(Empty_HandleescMessagecharsmax(escMessage), message)
    
get_user_authid(idsteamidcharsmax(steamid))
    
    new 
query[512]
    
formatex(querycharsmax(query), "INSERT INTO %s VALUES('%s','%s',NOW(),'%s')"tableescNamesteamidescMessage)
    
SQL_ThreadQuery(g_Tuple"ignoreHandle"query)
}
public 
ignoreHandle(FailState,Handle:queryerror[], errNum)
{
    
SQL_FreeHandle(query)
    return 
PLUGIN_HANDLED



4ever16 05-17-2019 05:26

Re: Send text to database
 
Thanks AmXDusT works perfectly!

JocAnis 05-17-2019 08:31

Re: Send text to database
 
@AmXDusT can you explain in a little details (cant find anyting specified on net) what
Code:

SQL_QuoteString
is doing? how it seems it is the function i need but im not sure if it is working well...also why 'Empty_Handle' ? Thanks in advance!

AmXDusT 05-18-2019 22:43

Re: Send text to database
 
Quote:

Originally Posted by JocAnis (Post 2651991)
@AmXDusT can you explain in a little details (cant find anyting specified on net) what
Code:

SQL_QuoteString
is doing? how it seems it is the function i need but im not sure if it is working well...also why 'Empty_Handle' ? Thanks in advance!

the empty_handle should be the SQL_Connect which I didn't do

quotestring should escape the string you're putting there to avoid having some backticks breaking your query. I'm not 100% sure tho, seen it in a few plugins and started using it as well for safety.

JocAnis 05-20-2019 07:18

Re: Send text to database
 
@AmXDust Thanks, i still dont get why Empty_handle but only with that one im not getting errors, how it seems its working well...probably QuoteString is doing the same as replace_all, cuz it requires buffer of strlen*2 of name

i was getting duplicated rows in a table..so i thought it was a character problem but with gotten logs im sure its the problem in sqlx as stated some years ago...so if anyone else struggling: add boolen on plugin_end() and check that bool for every threaded query...also what i found while searching for this fix:
Code:

SQL_SetCharset( g_SqlTuple, "utf8" )
only for 1.8.3 (couldnt test yet to see if it is working 100% but i guess it is)

4ever16 05-20-2019 12:22

Re: Send text to database
 
JocAnis, worked for me without problem. Used amxx 1.9

JocAnis 05-20-2019 12:38

Re: Send text to database
 
ok im talking about for totally another plugin but with the same function (quotestring)...


All times are GMT -4. The time now is 05:46.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.