1. Hi I made a mysql db, I use the command Select but imhaving trouble returning the value to my other plugins. On a plugin I use the callfunction to the mysql plugin. which is supposed to return something.

    MySqlPlugin:
    Code:
            double ReadValue(BasePlayer player)
            {
                string userid = player.userID.ToString();
                const string SelectData = "SELECT * FROM profiles;";
                double value = 0; // have to set it to something
                _mySqlConnection = _mySql.OpenDb("127.0.0.1", 3306, "db", "root", "pass", this);
                var sql = Ext.MySql.Sql.Builder.Append(SelectData);            _mySql.Query(sql, _mySqlConnection, list =>
                {
            
                foreach (var entry in list)
                    {
                        if (entry["userid"].ToString() == userid)
                        {
                    
                        value = double.Parse(entry["profilevalue"].ToString()); // supposed to set to the db value
                    
                    
                        Puts("Checked: " + value.ToString()); // >>>> This works in console, value is correct
                        }
                    
                    }
                });
            
            return value; //returning 0...
            }        
    When I try the script, I see the msg in console with the correct value (which is not 0). But when I look at the sendreply (called from other script), it says 0. Return type is not returning the correct value, it sends the 0 that is set at the first line.

    What's wrong? I can't make a return value where I placed the Puts either. Can somebody help me get the return working properly plz?
     
    Last edited by a moderator: Sep 25, 2015
  2. You can't return a value from the Query as it is a callback. You need to have your callback store the/properly use a callback.
     
  3. I am not sure to understand your last sentence. Or do you have another suggestion like using: SELECT profilevalue FROM profiles WHERE userid = '" + userid + "'"), _mySqlConnection); ? I tried that but couldnt make it work either. What's the easiest way to do it?
     
  4. Code:
    private const string InsertData = "INSERT INTO example (`data`, `data_other`) VALUES (@0, @1);";
    private const string SelectData = "SELECT `id`, `data`, `data_other` FROM example;";        [HookMethod("TestMethod")]
            void TestMethod(){
                Puts("TestMethod");            // create table
                var sql = Ext.MySql.Sql.Builder.Append(@"DROP TABLE IF EXISTS example;
                                   CREATE TABLE IF NOT EXISTS example (
                                     id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                                     data VARCHAR(100),
                                     data_other INT
                                   );");          // insert data
                var r = new System.Random();
                for (var i = 0; i < 10; i++)
                {
                    var rInt = r.Next();
                    sql.Append(InsertData, "test" + rInt, rInt);
                }
                _mySql.Insert(sql, _mySqlConnection);            // select data
                sql = Ext.MySql.Sql.Builder.Append(SelectData);
                _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());
                });
            }
     
  5. I already know about that extension. Thats what helped me manage and create db. If you read my first msg ud see the same lines used from extension. My problem is to return a value to my plugins not just send a log msg in console with that value.
     
  6. You can see how to send values to other plugins in my other post, here:
    Question for techies | Oxide

    In that example i am not sending variables, but it works if you try to send variables to other plugins too...

    Is that what you mean?
     
  7. (Sorry cant quote on phone it doesnt send msg)

    I know i used the call function fromother plugin. It just cant return the value when i try to get it out from the query. When i write return it gives me error because i cant make a return inside query. So i change string value as written on top og my msg on this post but value string is not changed outside the query script. So return at the bottom line is 0
     
  8. Return the value from the query works at my example above, i mean "foreach (var entry in list)":
    Returning a value with MySQL database? | Oxide

    It is writing values to console, you can send values to other plugin.

    Hm?
     
  9. I wrote in query: value = double.Parse(entry["profilevalue"].ToString()); // supposed to set to the db value

    So the value is supposed to be the db value now. But the return value; at the end is still the 0 from the making of double value = 0; on beginning lines.
    [DOUBLEPOST=1443272125][/DOUBLEPOST]Its like if it doesnt even change the string value to be returned at the end.
     
  10. ok man, from the code in your first post....

    you have fnction which returns "double" and then you return value (which is apparently a string).

    so you have to change your
    Code:
            double ReadValue(BasePlayer player)
    
    to
    Code:
            string ReadValue(BasePlayer player)
    
     
  11. I cant retrieve the db value inside the query and write it in console but trying to return value is not updating and is not the right value like console sends
     
  12. read solution above
     
  13. Yes its string atm i edited it to double begore sorry. Im switching double to stribg to try stiffs because it sends into a sendreply which is a string. Anyway its not the problem i get no error anywhere.
    [DOUBLEPOST=1443272467][/DOUBLEPOST]My plugin is double. Its just in thread forgot to change it because ii tested stuffs
    [DOUBLEPOST=1443272545][/DOUBLEPOST]Pretend its double. It still doesnt return the value i look for.
     
  14. i dont understand what you mean...

    your return value should be string and not double, you are converting strings to double which leads to incorrect values received, or i am missing something here?

    if yout take string from database and send it to plugin, it has to use it as string, not double...

    Edit:

    ohh you probably meanchange :
    Code:
                        value = double.Parse(entry["profilevalue"].ToString()); // supposed to set to the db value
    
    to
    Code:
                        value = double.Parse(entry["profilevalue"]); // double value
    
     
  15. Yes it was for testibg purposes. Forget the string readvalue. I use double readvalue. But i still cant change the double value = dbvalue. Dbvalue is picked well inside the query. It cant be returned from inside either.
    [DOUBLEPOST=1443272918][/DOUBLEPOST]Double value = dbvalue;
    Puts(dbvalue); // works

    ...

    ...

    Return dbvalue; // is not updated from query like i told it to
    [DOUBLEPOST=1443273077][/DOUBLEPOST]Well even with tostring or not, the double.parse will still convert it to double so the return should be working but it still return the default 0 value.
    [DOUBLEPOST=1443273467][/DOUBLEPOST]Also if it was mispelled it would catch an error of conversion no? Its just ignored and value is not updated
    [DOUBLEPOST=1443274630][/DOUBLEPOST]Edit: ok i changed the line and removed tostring of the parse. Now it looks: value = double.Parse(entry["profilevalue"]);

    I get now the error: argument #1 cannot convert oject expresdion to type string..

    Where do thwy see a string?
     
  16. You simply can't return that value because the Query is executed after your method call. It is run asynchronously. So you need to call a method from your plugin and pass that data.(like its done in the example with Puts it is also a function of your plugin)
     
  17. So basically you suggest to still use query but instead of returning the dbvalue to the plugincall initiator (plugin.call), i should call another void to send it to that plugin? Sorry if im hard to understand. Its like a chain value transfer from a void to another.
    [DOUBLEPOST=1443279241][/DOUBLEPOST]
     
  18. ^^ this exactly
    he suggest you to callback function from inside of _mySql.Query(sql, _mySqlConnection, list => { ...here... } , because its done async
     
  19. U're right. Thats kinda chain datavalue transfer. Call then call again. If thats the easiest way to make it ill try that and see how it goes