Не удается подключиться к SQL
| |
scorpion2904
|
Дата: Четверг, 22.11.2012, 18:47:08 | Сообщение # 1 |
|
Сообщений: 68
Репутация: 5 [ +/- ]
|
|
При загрузке плагина пишет "Установлено SQLite соединение", а при заходе игрока "Не удалось установить SQL соединение"
Вот databases.cfg
Code "Databases" { "test" { "driver" "sqlite" "host" "localhost" "database" "test" "user" "root" "pass" "" } }
Код:
Code #include <sourcemod>
new Handle:g_SQL = INVALID_HANDLE, bool:MYSQL;
public OnPluginStart() { if (!SQL_CheckConfig("test")) { SetFailState("Секция \"test\" не найдена в databases.cfg"); return; }
decl String:error[256]; g_SQL = SQL_Connect("test", true, error, 256); if (g_SQL == INVALID_HANDLE) { LogError(error); SetFailState("Не удалось установить SQL соединение"); return; }
// тип соединения (mysql или sqlite) new String:driver[15]; SQL_ReadDriver(g_SQL, driver, 15); MYSQL = StrEqual(driver, "mysql", false); LogMessage("Установлено %s соединение", MYSQL ? "MYSQL" : "SQLite"); } public OnClientPutInServer(client) { decl String:steamid[28]; if (!GetClientAuthString(client, steamid, 28)) { KickClient(client, "Не удалось определить ваш SteamID (1)"); return; } decl String:zapros[125]; Format(zapros, 125, "SELECT `status` FROM `my_tab` WHERE `my_steamid` = '%s' LIMIT 1", steamid); SQL_TQuery(g_SQL, wS_OnClientPutInServer_CallBack, zapros, GetClientUserId(client)); }
public wS_OnClientPutInServer_CallBack(Handle:owner, Handle:result, const String:error[], any:userid) { // игрок успел выйти, стоп (wtf) new client = GetClientOfUserId(userid); if (client < 1) return;
if (result == INVALID_HANDLE) { PrintToServer("Не удалось установить SQL соединение"); return; }
if (!SQL_FetchRow(result)) { PrintToServer("Вашего SteamID нет в таблице 'my_tab'"); return; }
if (SQL_FetchInt(result, 0) == 1) { PrintToServer("Вы отключены (status = 1)"); return; }
// Разрешаем играть и обновляем status на 1 decl String:steamid[28]; if (!GetClientAuthString(client, steamid, 28)) { PrintToServer("Не удалось определить ваш SteamID (2)"); return; }
decl String:zapros[125]; Format(zapros, 125, "UPDATE `my_tab` SET `status` = 1 WHERE `my_steamid` = '%s'", steamid); SQL_TQuery(g_SQL, SQL_CallBack, zapros); }
public SQL_CallBack(Handle:owner, Handle:result, const String:error[], any:data) { if (result == INVALID_HANDLE) LogError(error); }
|
|
| |
Beelzebub
|
Дата: Пятница, 23.11.2012, 17:22:40 | Сообщение # 2 |
|
Сообщений: 202
Репутация: 13 [ +/- ]
|
|
Эм подключение к sql ведь по другому идет.
Code "test" { "driver" "sqlite" "database" "test_sql" }
попробуй так. То что ты втыкал в database это подключение к mysql.
|
|
| |
scorpion2904
|
Дата: Пятница, 23.11.2012, 18:03:24 | Сообщение # 3 |
|
Сообщений: 68
Репутация: 5 [ +/- ]
|
|
Ок ща попробуюДобавлено (23.11.2012, 18:03:24) --------------------------------------------- Ниче не изменилось
|
|
| |
Beelzebub
|
Дата: Пятница, 23.11.2012, 18:40:23 | Сообщение # 4 |
|
Сообщений: 202
Репутация: 13 [ +/- ]
|
|
Эм, тогда у тебя ручки кривые :)
|
|
| |
scorpion2904
|
Дата: Пятница, 23.11.2012, 19:30:46 | Сообщение # 5 |
|
Сообщений: 68
Репутация: 5 [ +/- ]
|
|
В самом коде не может быть ошибки?Добавлено (23.11.2012, 19:30:46) --------------------------------------------- Судя по урокам Ws в databases.cfg нет ошибок
|
|
| |
Beelzebub
|
Дата: Суббота, 24.11.2012, 13:03:00 | Сообщение # 6 |
|
Сообщений: 202
Репутация: 13 [ +/- ]
|
|
Code #pragma semicolon 1 #include <sourcemod>
new Handle:db = INVALID_HANDLE;
public OnPluginStart() { decl String:error[256]; if(SQL_CheckConfig("reg")) { db = SQL_Connect("reg", true, error, sizeof(error)); } else { SetFailState("Didn't find database."); } if(db==INVALID_HANDLE) { SetFailState("Could not connect to database: %s", error); } }
public OnClientPostAdminCheck(client) { decl String:query[256], String:steamid[32]; GetClientAuthString(client, steamid, sizeof(steamid)); Format(query, sizeof(query), "SELECT id_member FROM smf_members WHERE steamid = '%s'"); SQL_TQuery(db, SQLQueryID, query, GetClientUserId(client)); }
public SQLQueryID(Handle:owner, Handle:hndl, const String:error[], any:data) { if(!StrEqual("", error)) { LogError("Query failed: %s", error); } new client = GetClientOfUserId(data); if(!client) { return; } decl String:query[256]; Format(query, sizeof(query), "SELECT end_time FROM smf_log_subscribed WHERE id_member = %i AND status = 1", SQL_FetchInt(hndl, 0)); SQL_TQuery(db, SQLQuerySubscription, query, GetClientUserId(client)); }
public SQLQuerySubscription(Handle:owner, Handle:hndl, const String:error[], any:data) { if(!StrEqual("", error)) { LogError("Query failed: %s", error); } new client = GetClientOfUserId(data); if(!client) { return; } new remain = SQL_FetchInt(hndl, 0) - GetTime(); if(remain < 0) { return; } SetUserFlagBits(client, GetUserFlagBits(client) | ADMFLAG_RESERVATION); }
Code "reg" { "driver" "sqlite" "database" "reg_sql" }
Это SQL. Попробуй.Добавлено (24.11.2012, 13:03:00) --------------------------------------------- Вот нашел в инете какую то штуку :)
Code #pragma semicolon 1
#include <sourcemod> #include <sdktools>
#define PLUGIN_VERSION "1.0" #define SQLSIZE_MAX 8192 new Handle:db; /** Database connection */
public OnPluginStart() { RegAdminCmd("sql_query", Command_SQL, ReadFlagString("z"), "dispatch sql query"); RegAdminCmd("sql_connect_sqlite", Command_Connect, ReadFlagString("z"), "connect to local sqlite database"); RegAdminCmd("sql_connect_section", Command_ConnectSect, ReadFlagString("z"), "connect to db listed in configs/databases.cfg"); RegAdminCmd("sql_connect_custom", Command_ConnectCust, ReadFlagString("z"), "connect with custom settings"); RegAdminCmd("sql_disconnect", Command_Disconnect, ReadFlagString("z"), "disconnect"); CreateConVar("SQLQueries_version", PLUGIN_VERSION, "SQLQueries version string", FCVAR_PLUGIN|FCVAR_DONTRECORD); } public Action:Command_ConnectCust(client,args) { if(args != 6) { ReplyToCommand(client, "usage: sql_connect_custom <dbtype(sqlite|mysql)> <\"hostname/ip\"> <\"database\"> <\"username\"> <\"password\"> <\"port\">"); return Plugin_Handled; } new String:Settings[6][255]; for(new i=0;i<6;i++) { GetCmdArg((i+1), Settings[i], 255); } if(!StrEqual(Settings[0],"sqlite",true) && !StrEqual(Settings[0],"mysql",true)) { ReplyToCommand(client, "<dbtype> must be \"sqlite\" or \"mysql\" , all lowercase"); return Plugin_Handled; } new String:error[255],Handle:h_kv = CreateKeyValues(""); KvSetString(h_kv, "driver",Settings[0]); KvSetString(h_kv, "host",Settings[1]); KvSetString(h_kv, "database",Settings[2]); KvSetString(h_kv, "user",Settings[3]); KvSetString(h_kv, "pass",Settings[4]); KvSetString(h_kv, "port",Settings[5]); if(db != INVALID_HANDLE) { CloseHandle(db); db = INVALID_HANDLE; } db = SQL_ConnectCustom(h_kv, error, sizeof(error), true); CloseHandle(h_kv); if (db == INVALID_HANDLE) { ReplyToCommand(client, "Failed to connect to %s: %s", Settings[1],error); } else { decl String:query[SQLSIZE_MAX]; Format(query, sizeof(query), "SET NAMES 'utf8'"); if (!SQL_FastQuery(db, query)) ReplyToCommand(client, "Can't select character set (%s)", query); ReplyToCommand(client, "DatabaseInit *%s* (CONNECTED) with %s", Settings[0], Settings[1]); if(StrEqual(Settings[0],"sqlite",false)) { Format(query, sizeof(query), "SELECT SQLITE_VERSION()"); SQL_TQuery(db, query_callback, query,(client != 0)?GetClientUserId(client):0); Format(query, sizeof(query), "SELECT name as Tables FROM sqlite_master WHERE type='table' ORDER BY name"); SQL_TQuery(db, query_callback, query,(client != 0)?GetClientUserId(client):0); } else { Format(query, sizeof(query), "select version()"); SQL_TQuery(db, query_callback, query,(client != 0)?GetClientUserId(client):0); Format(query, sizeof(query), "show tables"); SQL_TQuery(db, query_callback, query,(client != 0)?GetClientUserId(client):0); } } return Plugin_Handled; }
public Action:Command_ConnectSect(client,args) { if(args == 0) { ReplyToCommand(client, "usage: sql_connect_section <\"databases.cfg section\">"); return Plugin_Handled; } decl String:Sect[255]; GetCmdArg(1,Sect,sizeof(Sect)); if (SQL_CheckConfig(Sect)) { if(db != INVALID_HANDLE) { CloseHandle(db); db = INVALID_HANDLE; } new String:error[255]; db = SQL_Connect(Sect,true,error, sizeof(error)); if (db == INVALID_HANDLE) { ReplyToCommand(client, "Failed to connect to '%s': %s", Sect, error); } else { decl String:query[SQLSIZE_MAX]; Format(query, sizeof(query), "SET NAMES 'utf8'"); if (!SQL_FastQuery(db, query)) ReplyToCommand(client, "Can't select character set (%s)", query); new String:ProdName[255]; SQL_ReadDriver(db, ProdName, sizeof(ProdName)); ReplyToCommand(client, "DatabaseInit *%s* (CONNECTED) with db config %s", ProdName, Sect); if(StrEqual(ProdName,"sqlite",false)) { Format(query, sizeof(query), "SELECT SQLITE_VERSION()"); SQL_TQuery(db, query_callback, query,(client != 0)?GetClientUserId(client):0); Format(query, sizeof(query), "SELECT name as Tables FROM sqlite_master WHERE type='table' ORDER BY name"); SQL_TQuery(db, query_callback, query,(client != 0)?GetClientUserId(client):0); } else { Format(query, sizeof(query), "select version()"); SQL_TQuery(db, query_callback, query,(client != 0)?GetClientUserId(client):0); Format(query, sizeof(query), "show tables"); SQL_TQuery(db, query_callback, query,(client != 0)?GetClientUserId(client):0); } } } else ReplyToCommand(client, "db section %s not found", Sect); return Plugin_Handled; } public Action:Command_Disconnect(client,args) { if(db != INVALID_HANDLE) { CloseHandle(db); db = INVALID_HANDLE; } } public Action:Command_SQL(client, args) { if(db == INVALID_HANDLE) ReplyToCommand(client, "No DB connection"); else { if(args != 1) { ReplyToCommand(client, "Encase the sql query with quotation marks"); return Plugin_Handled; } decl String:buffer[SQLSIZE_MAX]; buffer[0] = 0; GetCmdArg(1, buffer, sizeof(buffer)); SQL_TQuery(db, query_callback, buffer, (client != 0)?GetClientUserId(client):0); } return Plugin_Handled; } public Action:Command_Connect(client,args) { if(args == 0) { ReplyToCommand(client, "usage: sql_connect_sqlite <\"dbname\"> <CreateIfNonExistant - 1|0 - default:0>"); return Plugin_Handled; } new String:dbname[64], String:Create[64], iCreate = 0; GetCmdArg(1, dbname, sizeof(dbname)); if(args == 2) { GetCmdArg(2, Create, sizeof(Create)); iCreate = StringToInt(Create); } if(iCreate == 0) { new String:dbPath[PLATFORM_MAX_PATH]; BuildPath(Path_SM,dbPath,sizeof(dbPath),"data/sqlite/%s.sq3",dbname); if(!FileExists(dbPath,false)) { ReplyToCommand(client, "Database does not exist. If you want to create it, type: sqlite_connect \"%s\" 1",dbname); return Plugin_Handled; } }
if(db != INVALID_HANDLE) { CloseHandle(db); db = INVALID_HANDLE; }
new String:error[255]; new Handle:h_kv = CreateKeyValues(""); KvSetString(h_kv, "driver", "sqlite"); KvSetString(h_kv, "database", dbname); db = SQL_ConnectCustom(h_kv, error, sizeof(error), true); CloseHandle(h_kv); if (db == INVALID_HANDLE) ReplyToCommand(client, "Failed to create sqlite db connection: %s", error); else { decl String:query[SQLSIZE_MAX]; Format(query, sizeof(query), "SET NAMES 'utf8'"); if (!SQL_FastQuery(db, query)) ReplyToCommand(client, "Can't select character set (%s)", query); ReplyToCommand(client, "DatabaseInit SQLLITE (CONNECTED)"); Format(query, sizeof(query), "SELECT SQLITE_VERSION()"); SQL_TQuery(db, query_callback, query,(client != 0)?GetClientUserId(client):0); Format(query, sizeof(query), "SELECT name as Tables FROM sqlite_master WHERE type='table' ORDER BY name"); SQL_TQuery(db, query_callback, query,(client != 0)?GetClientUserId(client):0); } return Plugin_Handled; }
public query_callback(Handle:owner, Handle:hndl, const String:error[], any:data) { new client = GetClientOfUserId(data); if (hndl == INVALID_HANDLE) ReplyCust(client, "Query failed! %s", error); else { if(!SQL_HasResultSet(hndl)) ReplyCust(client,"Affected rows: %i",SQL_GetAffectedRows(db)); //why doesn't hndl work for GetAffectedRows? else { //if (!SQL_GetRowCount(hndl)) // ReplyCust(client,"Query didn't produce any matches (0 rows returned)"); new fcount = (SQL_GetFieldCount(hndl)-1); decl String:Buffer[255], String:Output[1024]; Output[0] = 0; for(new i=0;i<=fcount;i++) { SQL_FieldNumToName(hndl, i, Buffer, sizeof(Buffer)); StrCat(Output, sizeof(Output), Buffer); if(i < fcount) StrCat(Output, sizeof(Output), "|"); } ReplyCust(client, Output); new Headerlen = strlen(Output); for(new i=0;i<Headerlen;i++) { Output[i] = 45; } ReplyCust(client, Output); while(SQL_FetchRow(hndl)) { Output[0] = 0; for(new i=0;i<=fcount;i++) { new DBResult:Result; SQL_FetchString(hndl, i, Buffer, sizeof(Buffer), Result); switch(Result) { case DBVal_Null: StrCat(Output, sizeof(Output), "*NULL*"); case DBVal_Data: StrCat(Output, sizeof(Output), Buffer); case DBVal_Error: StrCat(Output, sizeof(Output), "*INVALID IDX*"); case DBVal_TypeMismatch: StrCat(Output, sizeof(Output), "*TYPE MISMATCH*"); } if(i < fcount) StrCat(Output, sizeof(Output), "|"); } ReplyCust(client, Output); } } } }
ReplyCust(client, String:Reply[], any:...) { decl String:bReply[SQLSIZE_MAX]; if(client == 0) SetGlobalTransTarget(LANG_SERVER); else SetGlobalTransTarget(client); VFormat(bReply, sizeof(bReply), Reply, 3); if(client == 0) PrintToServer(bReply); else { PrintToConsole(client, bReply); PrintToChat(client, bReply); } }
|
|
| |
Sunday
|
Дата: Суббота, 24.11.2012, 13:35:02 | Сообщение # 7 |
|
Сообщений: 186
Репутация: 24 [ +/- ]
|
|
Привет, у тебя в коде маленькая ошибка:
Code if (!SQL_FetchRow(result)) { PintToServer("Вашего SteamID нет в таблице 'my_tab'"); return; }
Поправь, PintToServer на PrintToServer. :
L 11/24/2012 - 16:31:24: -------- Mapchange to dm_runoff -------- L 11/24/2012 - 16:31:24: [msql.smx] Установлено SQLite соединение
еще можно добавить: public OnClientPutInServer(client) { // Если это бот, стоп if (IsFakeClient(client)) return;
Сообщение отредактировал Sunday - Суббота, 24.11.2012, 15:09:53 |
|
| |
|