PHP code example of yangweijie / think-orm-sqlite-remote

1. Go to this page and download the library: Download yangweijie/think-orm-sqlite-remote library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yangweijie / think-orm-sqlite-remote example snippets


// config/database.php
return [
    'connections' => [
        'sqlite_remote' => [
            'type'           => \yangweijie\orm\sqlite\remote\Connection::class,
            'tunnel_url'     => 'http://your-server.com/ntunnel-sqlite',
            'database'       => '/path/to/database.sqlite',
            'prefix'         => '',
            'timeout'        => 30,
            'auth_username'  => 'admin',     // 可选
            'auth_password'  => 'secret',    // 可选
            'fields_cache'   => true,        // 字段缓存
        ],
    ],
];

// config/database.php
return [
    'connections' => [
        'sqlite_socket' => [
            'type'           => \yangweijie\orm\sqlite\remote\Connection::class,
            'socket_host'    => '127.0.0.1',
            'socket_port'    => 9527,
            'database'       => '/path/to/database.sqlite',
            'prefix'         => '',
            'timeout'        => 30,
            'auth_username'  => 'admin',     // 需与守护进程配置一致
            'auth_password'  => 'secret',
            'fields_cache'   => true,
        ],
    ],
];

use think\facade\Db;

// 查询
$users = Db::connect('sqlite_socket')->table('users')->select();

// 插入
Db::connect('sqlite_socket')->table('users')->insert([
    'name' => 'John',
    'email' => '[email protected]',
]);

// 更新
Db::connect('sqlite_socket')->table('users')->where('id', 1)->update(['name' => 'Jane']);

// 删除
Db::connect('sqlite_socket')->table('users')->where('id', 1)->delete();

use think\facade\Db;

// 自动事务
Db::connect('sqlite_socket')->transaction(function() {
    $id = Db::table('users')->insertGetId(['name' => 'test']);
    Db::table('profiles')->insert(['user_id' => $id, 'bio' => '...']);
});

// 手动控制
Db::connect('sqlite_socket')->startTrans();
try {
    $id = Db::table('users')->insertGetId(['name' => 'test']);
    Db::table('profiles')->insert(['user_id' => $id]);
    Db::connect('sqlite_socket')->commit();
} catch (\Exception $e) {
    Db::connect('sqlite_socket')->rollback();
    throw $e;
}

namespace app\model;

use think\Model;

class User extends Model
{
    protected $connection = 'sqlite_socket';
    protected $table = 'users';
    protected $autoWriteTimestamp = true;
}

// 使用
$users = User::where('status', 1)->select();
$user = User::find(1);

// 在服务提供者或路由文件中
\yangweijie\orm\sqlite\remote\service\ServiceProvider::registerRoute(
    app()->route,
    'ntunnel-sqlite',
    [
        'auth_username' => 'admin',
        'auth_password' => 'secret',
        'database_root' => '/data/sqlite',
        'allowed_databases' => ['/data/sqlite/app.db'],
    ]
);
bash
# 基本启动(默认启用 WAL 模式)
php think sqlite-tunnel:start

# 指定端口
php think sqlite-tunnel:start --port=9527

# 带认证
php think sqlite-tunnel:start --port=9527 --auth-username=admin --auth-password=secret

# 限制数据库目录
php think sqlite-tunnel:start --port=9527 --database-root=/data/sqlite

# 完整参数
php think sqlite-tunnel:start \
    --host=127.0.0.1 \
    --port=9527 \
    --auth-username=admin \
    --auth-password=secret \
    --database-root=/data/sqlite \
    --idle-timeout=300 \
    --max-connections=100
bash
# 生成所有表的字段缓存
php think optimize:schema-remote --connection=sqlite_socket

# 生成指定表的字段缓存
php think optimize:schema-remote --connection=sqlite_socket --table=users

# 生成所有表(通配符)
php think optimize:schema-remote --connection=sqlite_socket --table=*
bash
# 1. 注册服务提供者(config/app.php 或 app/provider.php)
\yangweijie\orm\sqlite\remote\service\ServiceProvider::class,

# 2. 启动 Socket 守护进程(后台运行)
nohup php think sqlite-tunnel:start --port=9527 --auth-username=admin --auth-password=secret &

# 3. 使用 Supervisor 管理守护进程
[program:sqlite-tunnel]
command=php think sqlite-tunnel:start --port=9527 --wal-mode=WAL --busy-timeout=5000
directory=/path/to/your/project
autostart=true
autorestart=true
user=www-data
stdout_logfile=/var/log/sqlite-tunnel.log