2018年4月30日 星期一


惡靈勢力2原碼連到資料庫有兩種方法。

第一種是通過命名配置,命名配置是在
addons\sourcemod\configs\databases.cfg中列出的預設配置。

SourceMod指定如果正在使用SQL,預設有一個名為“default”的配置。

"default"
    {
        "host"                "localhost"
        "database"            "sourcemod"
        "user"                "root"
        "pass"                ""
        //"timeout"            "0"
        //"port"            "0"
    }

  • host 連線主機的位置,如果Mysql資料庫跟你遊戲Server同一台電腦使用localhost即可
    或是打上資料庫的那台電腦IP
  • database 要連線的資料庫名稱,這名稱要自己在Mysql創一個新資料庫
  • user 使用者名稱,預設是root
    root這使用者安裝MySQL時一定會有
  • pass 使用者密碼,預設的root帳號的密碼是空的,不需要輸入
    日後有修改密碼要記的修改
  • timeout 連線逾時的時間,有註解了不會執行
  • port 資料庫的連接埠,也有註解不用輸入,MySQL的port預設是3306
    除非安裝時有修改才要設定,並把//註解拿掉

另外也能不使用default,自己再增加一個自己取的名字

基於命名配置的連接,在原碼可以使用SQL_Connect或SQL_DefConnect實例化

SQL_Connect範例,可放在插件開始的地方OnPluginStart

#define DB_CONF_NAME "atol4d2SQL"//databases.cfg裡面命名的名稱
//建立與數據庫的連接
bool:ConnectDB()
{
    if (db != INVALID_HANDLE)
        return true;
    
    if (SQL_CheckConfig(DB_CONF_NAME))
    {
        new String:Error[256];
        db = SQL_Connect(DB_CONF_NAME, true, Error, sizeof(Error));

        if (db == INVALID_HANDLE)
        {
            LogError("無法連接到數據庫: %s", Error);
            return false;
        }
        else if (!SQL_FastQuery(db, "SET NAMES 'utf8'"))
        {
            if (SQL_GetError(db, Error, sizeof(Error)))
                LogError("無法將編碼更新為 UTF8: %s", Error);
            else
                LogError("無法將編碼更新為UTF8:未知");
        }
    }
    else
    {
        LogError("Databases.cfg 缺少 '%s' 輸入!", DB_CONF_NAME);
        return false;
    }

    return true;
}

SQL_DefConnect範例,可放在玩家進入遊戲或插件開始OnPluginStart相關判斷的地方

char error[255];
Database db = SQL_DefConnect(error, sizeof(error));
 
if (db == null)
{
    PrintToServer("Could not connect: %s", error);
} 
else 
{
    delete db;
}



另一種選擇是使用SQL_ConnectCustom並通過傳遞包含它們的keyvalue句柄來手動指定所有連接參數

// The function to call, when you want to connect to the database
stock Database_Connect() {
    // Make sure to close the database handle, in case you try to connect more than once
    if (g_hDatabase != INVALID_HANDLE) {
        CloseHandle(g_hDatabase);
        g_hDatabase = INVALID_HANDLE;
    }

    // Only connect if the handle is invalid
    if (g_hDatabase == INVALID_HANDLE) {
        // Use KeyValues for login credentials
        new Handle:hKeyValues = CreateKeyValues("");
        KvSetString(hKeyValues, "host", "YOUR DATABASE ADDRESS HERE");
        KvSetString(hKeyValues, "database", "YOUR DATABASE NAME HERE");
        KvSetString(hKeyValues, "user", "YOUR DATABASE USER HERE");
        KvSetString(hKeyValues, "pass", "YOUR DATABASE PASSWORD HERE");

        // Connect and write errors to sError
        decl String:sError[512];
        g_hDatabase = SQL_ConnectCustom(hKeyValues, sError, sizeof(sError), true);
        
        // Close the KeyValues handle to free memory
        CloseHandle(hKeyValues);

        if (g_hDatabase == INVALID_HANDLE) {
            // An error has occured if the database handle is still invalid
            LogError("[Database] Failed to connect to the database! Error: %s", sError);
        } else {
            // Successfully connected to the database
            // Now you can use g_hDatabase to query your database
            PrintToServer("[Database] Successfully connected to the database");
        }
    }
}

這種的方式我覺得比較複雜,看各位要選擇哪一種方式了
SQL_Connect或SQL_DefConnect算是比較雷同的寫法,也有人使用SQL_ConnectCustom
這三種選一種方法即可,不用三個語法都加到自己原碼裡

我這邊之後會研究SQL_DefConnect的寫法,感覺簡單一點

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © 阿土進擊班 - Blogger Templates - Powered by Blogger - Designed by Johanes Djogan -