Im not shure I have done the right thing. My plugin reads and writes to two SQL tables. As I have coded it the plugin_init is calling two tasks with a slight delay to init and connect to db. Is this really necessary? Do I only need to connect once to the database and then I can query both tables when connected?
It works now, but occationally there is a connection error in one or both result handles for the db connection. This happened once when the server had a lot of players and it was laggy.
From plugin_init:
Code:
// Connect to dabatase and init tables
set_task(5.0, "sql_init_ranks")
set_task(7.0, "sql_init_maps")
Init table 1:
Code:
// Init the ranks database
public sql_init_ranks() {
new host[64], username[32], password[32], dbname[32], error[32]
get_cvar_string("amx_sql_host",host,64)
get_cvar_string("amx_sql_user",username,32)
get_cvar_string("amx_sql_pass",password,32)
get_cvar_string("amx_sql_db",dbname,32)
dbcRanks = dbi_connect(host,username,password,dbname,error,32)
if (dbcRanks == SQL_FAILED) {
log_amx("[AMXX: SKILLRANK] SQL Connection Failed to table skillrank")
return PLUGIN_HANDLED
} else {
dbi_query(dbcRanks,"CREATE TABLE IF NOT EXISTS `skillrank` ( `steamId` VARCHAR(32) NOT NULL,`nickNames` VARCHAR(32) NOT NULL, `nFinnished` INT NOT NULL, `rankTotal` INT NOT NULL, `primaryRank` INT NOT NULL, PRIMARY KEY(`steamId`))")
}
dbi_free_result(resultRanks)
return PLUGIN_CONTINUE
}
Init table 2:
Code:
// Init the maps database
public sql_init_maps() {
new host[64], username[32], password[32], dbname[32], error[32]
get_cvar_string("amx_sql_host",host,64)
get_cvar_string("amx_sql_user",username,32)
get_cvar_string("amx_sql_pass",password,32)
get_cvar_string("amx_sql_db",dbname,32)
dbcMaps = dbi_connect(host,username,password,dbname,error,32)
if (dbcMaps == SQL_FAILED) {
log_amx("[AMXX: SKILLRANK] SQL Connection Failed to table skillmaps")
return PLUGIN_HANDLED
} else {
dbi_query(dbcMaps,"CREATE TABLE IF NOT EXISTS `skillmaps` ( `id` int(11) NOT NULL auto_increment, `steamId` VARCHAR(32) NOT NULL,`nickNames` VARCHAR(32) NOT NULL, `mapName` VARCHAR(32) NOT NULL, `playerClass` INT NOT NULL, `curDate` VARCHAR(10) NOT NULL, `curTime` VARCHAR(8) NOT NULL, `difficulty` INT NOT NULL, `runTime` INT NOT NULL, PRIMARY KEY(`id`))")
}
dbi_free_result(resultMaps)
return PLUGIN_CONTINUE
}