Solved SQL Query 'List'

Discussion in 'Rust Development' started by Braanflakes, Mar 21, 2017.

  1. I was taking a look at MySQL Extension | Oxide and after reading through and testing my own version of this, I'm curious as to what 'list' returns in the following statement:

    Code:
    _mySql.Query(sql, _mySqlConnection, list =>
                {
                    if (list == null) return;
                    var sb = new StringBuilder();
                    sb.AppendLine();
                    sb.AppendLine(" id\tdata\t\tdata_other");
                    foreach (var entry in list)
                    {
                        sb.AppendFormat(" {0}\t{1}\t{2}", entry["id"], entry["data"], entry["data_other"]);
                        sb.AppendLine();
                    }
                    Puts(sb.ToString());
                });
    I'm trying to run a query on my own database to check if a user exists and if they do not then I want to add them. I don't think I fully understand what 'list' means though. If someone could shed some light on that for me, I'd appreciate it.
     
    Last edited by a moderator: Mar 21, 2017
  2. list return empty list not null, u should check count of list
    for example:


    Code:
    mySql.Query(sql, mySqlConnection, list => {
        if (list.Count > 0)
        {
            for(int i = 0; i  < list.Count; i++)
            {
                list[i]["key"]
            }
        }
    }
     
  3. Ah, I was definitely overthinking it. This did the trick, I appreciate your help!

    Below is my working code if it helps anyone:

    Code:
    var sql = Core.Database.Sql.Builder.Append(SelectExists, UserId);            _mySql.Query(sql, _mySqlConnection, list =>
                    {
                        // if the database already contains the user, do nothing
                        if (list.Count > 0)
                        {
                            Puts("User " + Name + " (" + UserId + ") already exists in the database.");
                            return;
                        }                    // if the database doesn't contain the user, add them with a currency value of 0
                        sql = Core.Database.Sql.Builder.Append(InsertPlayerData, UserId, 0);
                        _mySql.Insert(sql, _mySqlConnection);
                        Puts("User did not exist. Successfully added " + Name + " (" + UserId + ") to the database.");
     
  4. list may be null, specifically when the query fails.
    I am not completly sure, but maybe it may also be non-null, even if the query fails (Specifically when the reader fails reading what the query returned - if the query reader is lazy, then this issue may occur. This results in you only getting part of the output you queried.).

    There's still a small error in that code.
    Firstly, if the query fails, list may be null (as explained above).
    Secondly, the user may not actually get successfully added to the database, even if the "Puts" at the end prints that it did because the insert statement may fail.
    Thirdly the message is printed *before* the query even finishes. The insert statement is concurrent, so Puts may execute before the query.
     
  5. So how would you recommend handling that situation, just use try/catch blocks? Or is there a more efficient method.
     
  6. For non-queries (e.g. inserts) you can check whether the affected row count is 0 in the callback for the insert (however, you cannot distinguish between an affected row count of 0 and an error, but in this case it should always be >0 if the insert is successful, right?).
    For queries (e.g. selects) you can check whether list is null in the callback.
    In the situation that list may also be non-null when there was an error there is no way to check whether the query was successfull.