Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Using SQL in Async

Comments in 'Plugin Development' started by dxm_hippie, Apr 15, 2016.

  1. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    Having a issue using a sql database in async, i haven't looked to much into async task..

    PHP:
         /**
            *
            * @param string $player
            * @param SQLite3 $database
            * @param bool $self
            */
        
    public function __construct($player$database$self false){
                
    $this->player $player;
                
    $this->stats $database;
                
    $this->self $self;
        }
      
        public function 
    onRun()
        {
                
    var_dump($this->stats);
                
    $prepare $this->stats->prepare("SELECT * FROM stats WHERE Name = :Name");// error here
    Code:
    //var_dump
    object(SQLite3)#4540 (0) {
    }
    // error
    Warning: SQLite3::prepare(): The SQLite3 object has not been correctly initialised
    Do i need to redefine the sql instead of just trying to pass it from the main class?
    Last edited: Apr 15, 2016
  2. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    Try this:
    PHP:
    $prepare $this->stats->prepare("SELECT * FROM stats WHERE Name = :name");
    $prepare->bindValue(":name"$nameSQLITE3_TEXT);
  3. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    The error is at the marked line, $this->stats->prepare is not running correctly bc "The SQLite3 object has not been correctly initialised"

    I do have
    $prepare->bindValue(":Name", $this->player, SQLITE3_TEXT);
  4. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    Can you show us what $this->stats is?
  5. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    PHP:
    if(!file_exists($this->getDataFolder() . 'Stats/'.'players.db'))
            {
                @
    mkdir($this->getDataFolder(). 'Stats');
                
    $this->stats = new \SQLite3($this->getDataFolder() . 'Stats/'.'players.db'SQLITE3_OPEN_READWRITE SQLITE3_OPEN_CREATE);
                
    $resource $this->getResource("Stats.sql");
                
    $this->stats->exec(stream_get_contents($resource));
            }else{
                
    $this->stats = new \SQLite3($this->getDataFolder() . 'Stats/'.'players.db'SQLITE3_OPEN_READWRITE);
            }
    PHP:
    $this->getServer()->getScheduler()->scheduleAsyncTask(new GetStatsTask($player->getName(), $this->stats$self));
    $this->stats works fine on the main thread or main class..whatever you wanna call it lol
  6. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    Try:
    PHP:
            //if(!file_exists($this->getDataFolder() . 'Stats/'.'players.db'))
            //{ This is useless because mkdir will make a directory if it doesnt exist but wont do anything if it does.
            
    @mkdir($this->getDataFolder(). 'Stats');
            
    $this->stats = new \SQLite3($this->getDataFolder() . 'Stats/players.db'SQLITE3_OPEN_READWRITE SQLITE3_OPEN_CREATE);
            
    $this->stats->exec("CREATE TABLE IF NOT EXISTS `stats` (name VARCHAR, whatever BLOB)"); //You didnt create a table...
            //$resource = $this->getResource("Stats.sql");
            //$this->stats->exec(stream_get_contents($resource)); Wutt?
            //}
            /*else{
            $this->stats = new \SQLite3($this->getDataFolder() . 'Stats/'.'players.db', SQLITE3_OPEN_READWRITE);
            }
            */
  7. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    I dont think the definition of $this->stats is the problem..and i did create the table with

    PHP:
     $resource $this->getResource("Stats.sql");
    $this->stats->exec(stream_get_contents($resource));
    Like i said it runs fine on the main class i have used it there to insert and fetch data just when i try pass it to a async task i get that error.
  8. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    Why are you even using AsyncTask? There's no point & it wont work.
  9. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    Because it will be a very large database..
  10. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    Also i want to update it on a schedule so i was going to run a task to run the stats updat async task every so often ..and i wanted some practice with AsyncTask XD
  11. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    That doesnt really matter, ive got a large database and it doesnt lag...
  12. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    I dont have a powerful server, and lots going on..
    Timings with only 1 players data stored..i think thats bad? lol
    ** Command: stats Time: 308602095 Count: 4 Avg: 77150524 Violations: 0
    Last edited: Apr 15, 2016
  13. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    ... You know what an Async task is right? In @PEMapModder's HereAuth, he uses async tasks to query MySQL statements. I don't think you are supposed to be sharing those objects between threads or whatever it is called.

    I believe you have to open the SQLite database every time you want to query it. Correct me if I am wrong :p
    dxm_hippie likes this.
  14. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    I did try to reopen it by sharing the path and redefining the $this->stats = new\Sql($path bla) and yes async task takes the load off the main thread...ill have to take a look at hearauth.
  15. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    It's HereAuth, not HearAuth...
  16. dxm_hippie
    Offline

    dxm_hippie Active Member Plugin Developer

    Joined:
    Feb 1, 2015
    Posts:
    413
    Plugins:
    1
    Minecraft User:
    XxDXM_hippiexX
    You know what I mean, simple typo lol

Share This Page

Advertisement