1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

SQLITE3 numbers going negative even though i made it minus

Discussion in 'Development' started by Levi, Jul 2, 2018.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    in my queries
    Code:
    -- #    {reduce_money
    -- #        :username string
    -- #        :money int
    UPDATE economy
    SET money = money - :money
    WHERE player = :username;
    -- #    }
    the method
    PHP:
    public static function reduceMoney(Player $playerint $amount)
        {
            
    $plugin MainClass::getInstance();
            
    $plugin->provider->executeChange("reduce_money", [
                
    "username" => $player->getName(), "money" => $amount
            
    ]);
        }
    when i reduce money(my current money is 80,000,000btw)
    PHP:
    EconomyAPI::reduceMoney($player60000000);
    it gives me -60000000 in the database
     
    Last edited: Jul 2, 2018
  2. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    You didn't get the value of 'money' first, so the result will be 0 - :money.
     
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Why do you need to get the value first?
     
    Bamuel likes this.
  4. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Because he wants to deduct the value he sends to that function from the current value in his db - doesn't he have to select the value first?
     
    Last edited: Jul 2, 2018
  5. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Move this to facepalm. You reduce the money, you take them, you get them away. Maybe addMoney or EconomyAPI::reduceMoney($player, -60000000); or setMoney($player, $currentMoney - 60000000)
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    It sounds pretty obvious,
    PHP:
    $money 0;
    function 
    reduceMoney(int $reduce){
        
    $money $money $reduce;
    }

    reduceMoney(6000000);
    //0 - 6000000? -6000000
    You could use SQLite3's MAX() function to cap the value you're setting.
    Code:
    UPDATE table SET value=MAX(0, value - value - 1);
    //value = 0
    
     
  7. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Looks to me like we are all assuming different things are happening in the code that the OP didn't post. Yet again, please post links to your plugin on github, or complete code.
     
    jasonwynn10 likes this.
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    He's writing "money = money - :money" in his DB. The first and second "money"s are column name references, and ":money" is a variable. Why does he need to select the value first if it can be represented in the query already?
    If his original amount of money is indeed 80,000,000 and he subtracted 60,000,000, he is indeed doing the right thing. But I think it's more likely that he made some mistakes in testing, e.g. his original amount of money was 0 not 80,000,000.
     
  9. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    what?
     
  10. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Indeed... according to https://sqlite.org/lang_update.html:

    So you've messed up elsewhere, and we can't know where from the information you posted.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.