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

Solved how to use sqlite3

Discussion in 'Plugin Help' started by DeviceBlack, Oct 19, 2021.

  1. DeviceBlack

    DeviceBlack Creeper

    Messages:
    4
    GitHub:
    ColdRevenger
    I would like to know how to use sqlite in my plugin, do I need some specific file?
    Please, if possible cite some plugins that use sqlite like SimpleAuth, SimpleHome or MyPlot, Although I haven't had any success researching them
     
  2. Primus

    Primus Zombie Pigman

    Messages:
    749
    What do you mean that you have not found any success in researching them? It's unusual for someone to find an example, fail to understand, and continue to seek more of the same type examples.

    The SQLite3 extension is by default enabled in standard php installations. The same is true for the php binaries used in pocketmine.
    Good news as you won't find yourself tackling the configuration of your development environment. In other words - no extra effort is necessary, in terms of setup.

    To use features provided by sqlite3 you must construct the SQLite3 object.
    NOTE: I will be using named arguments, which is a feature from php8, in order to write the following code.
    PHP:
    /** @method void createDatabase() **/
    try {
        
    $this->database = new SQLite3(filename"path/to/file.db");
    } catch (
    Exception $e) {
        
    /** @var /pocketmine/utils/Logger $logger **/
        
    $logger->error('Error initializing database: ' $e->getMessage());
    }
    Now you have an object that is capable to understand and act upon given SQL queries to read and write data in a persistent manner.

    Further usage would usually go something like this:
    PHP:
    $this->createDatabase();
    $this->setupDatabase();
    PHP:
    /** @method void setupDatabase **/
    $this->database->query(
        <<<SQL3
            CREATE TABLE IF NOT EXISTS players (
                name VARCHAR(64) PRIMARY KEY,
                balance INTEGER DEFAULT 0
            )
        SQL3;
    );
    And that sets us up and running. It more than often follows this same sequence of steps: create, setup, use, close.

    PHP:
    // Inside the method PluginBase :: onDisable
    $this->database->close();
    I overlooked some details and didn't elaborate on some things. Because I believe that you will gain more valuable insight from trial and error, and for an ending note, here is a link to one of my personal examples of usages: https://github.com/Kris-Driv/magebit-dev-task/blob/master/backend/src/data/SQLite3DataProvider.php
     
    Last edited: Oct 21, 2021
  3. DeviceBlack

    DeviceBlack Creeper

    Messages:
    4
    GitHub:
    ColdRevenger
    I appreciate it!
    Honestly, I didn't think I would have such an efficient answer, I usually came across "Google can" and that made me wonder why this forum existed until then...

    It was a test file and it worked the first time, thanks

    PHP:
    use pocketmine\Server;
    use 
    pocketmine\plugin\PluginBase;

    class 
    Main extends PluginBase {
        private 
    $database;
        public 
    $prepare;
      
        public function 
    onEnable() {
            @
    mkdir($this->getDataFolder());
            try {
                if(
    file_exists($this->getDataFolder() . "players.db")) {
                    
    $this->database = new \SQLite3($this->getDataFolder() . "players.db"SQLITE3_OPEN_READWRITE);
                } else {
                    
    $this->database = new \SQLite3($this->getDataFolder() . "players.db"SQLITE3_OPEN_READWRITE SQLITE3_OPEN_CREATE);
                }
                
    $this->prepare $this->database->prepare("CREATE TABLE IF NOT EXISTS Player (Id INTEGER AUTO_INCREMENT PRIMARY KEY);");
                
    $this->prepare->execute();
                
    $this->getLogger()->info("[SQLite] System working YEEAAAAH");
            } catch (\
    Throwable $e) {
                
    $this->getLogger()->info("[SQLite] Oh no! system failed");
                
    $this->getServer()->getPluginManager()->disablePlugin($this);
            }
        }
    }
     
    Last edited: Oct 19, 2021
    Primus likes this.
  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.