Home >PHP Framework >Workerman >How to use SQLite for data storage in Workerman

How to use SQLite for data storage in Workerman

WBOY
WBOYOriginal
2023-11-08 11:57:431452browse

How to use SQLite for data storage in Workerman

How to use SQLite for data storage in Workerman

Introduction:
Workerman is a high-performance multi-process network programming framework developed in PHP language, providing It has rich network programming interfaces and convenient expansion mechanisms. SQLite is a lightweight embedded database suitable for use in small projects. This article will introduce how to use SQLite to store data in Workerman and provide specific code examples.

1. Set up the SQLite database
First, we need to create a SQLite database file and set up the data table structure. You can use SQLite's command line tools or visual tools (such as Navicat, etc.) to create it. The following is an example data table structure:

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. Install SQLite extension
Before using SQLite, we need to install the SQLite extension of PHP. You can install it with the following command:

sudo apt-get install phpX.X-sqlite3

Please replace X.X with your PHP version number.

3. Use SQLite in Workerman

  1. Introduce the SQLite class library and other related class libraries:
require_once __DIR__ . '/vendor/autoload.php';

use WorkermanWorker;
use WorkermanConnectionTcpConnection;
use SQLite3;
  1. Create a Workerman service:
$worker = new Worker('tcp://0.0.0.0:8000');
  1. Listen to connection events and process client requests:
$worker->onConnect = function ($connection) {
    // 连接建立成功的回调函数
};

$worker->onMessage = function ($connection, $data) {
    // 接收到客户端消息的回调函数
};

$worker->onClose = function ($connection) {
    // 连接关闭的回调函数
};

Worker::runAll();
  1. Create or open a database connection in the callback function when the connection is successfully established:
$worker->onConnect = function ($connection) {
    // 连接建立成功的回调函数
    $db = new SQLite3('/path/to/your/database.sqlite');
};

Please replace /path/to/your/database.sqlite with the path to your SQLite database file.

  1. Perform database operations in the callback function that receives client messages:
$worker->onMessage = function ($connection, $data) use ($db) {
    // 解析客户端消息...
    // 执行数据库操作...
    $username = $data['username'];
    $password = $data['password'];
    
    // 插入数据
    $query = "INSERT INTO `user` (`username`, `password`) VALUES ('{$username}', '{$password}')";
    $db->exec($query);
    
    // 查询数据
    $query = "SELECT * FROM `user`";
    $result = $db->query($query);
    while ($row = $result->fetchArray()) {
        // 处理查询结果...
    }
};
  1. Close the database connection in the callback function that closes the connection:
$worker->onClose = function ($connection) use ($db) {
    // 连接关闭的回调函数
    $db->close();
};

4. Complete code example

require_once __DIR__ . '/vendor/autoload.php';

use WorkermanWorker;
use WorkermanConnectionTcpConnection;
use SQLite3;

$worker = new Worker('tcp://0.0.0.0:8000');

$worker->onConnect = function ($connection) {
    $db = new SQLite3('/path/to/your/database.sqlite');
};

$worker->onMessage = function ($connection, $data) use ($db) {
    $username = $data['username'];
    $password = $data['password'];
  
    // 插入数据
    $query = "INSERT INTO `user` (`username`, `password`) VALUES ('{$username}', '{$password}')";
    $db->exec($query);
  
    // 查询数据
    $query = "SELECT * FROM `user`";
    $result = $db->query($query);
    while ($row = $result->fetchArray()) {
        // 处理查询结果...
    }
};

$worker->onClose = function ($connection) use ($db) {
    $db->close();
};

Worker::runAll();

Note: The above example code is only a functional demonstration, and the specific business logic and exception handling need to be modified and improved according to the actual situation.

Summary:
This article introduces how to use SQLite for data storage in Workerman and gives specific code examples. I hope this article can be helpful to readers. If you have any questions or errors, please correct them in time.

The above is the detailed content of How to use SQLite for data storage in Workerman. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn