thinkphp 控制器中动态切换数据库 sqlite

tp文档:http://doc.thinkphp.cn/manual/switch_database.html

参照:

M("User","think_","mysql://root:123456@localhost:3306/test")->query("查询SQL");

比较直接的方式,我这里是要切换到sqlite数据库

代码如下:

$pageSize = 20;
 $page = 1;
 $li = M("article","","sqlite://root:123456@localhost:3306/".ROOT_PATH.'/sqlite数据库名称')
        ->query("select * from article where big_id={$big_id} and small_id={$small_id} order by id desc limit ".($page-1)*$pageSize.",".$pageSize );
        //echo "select * from article where big_id={$big_id} and small_id={$small_id} order by id desc limit ".($page-1)*$pageSize.",".$pageSize;
        $data['state'] = 1;
        $data['msg'] = "获取成功";
        $data['data'] = array(
            
            'pageSize' => $pageSize,
            'page' => $page,
            'datas' => $li
            
            );

        exit( json_encode( $data ) );

root:123456@localhost:3306/ 是默认加的 链接需要这样的格式 不加这段无法成功,按照这样的格式加,没有密码也没有关系 随意输入

ROOT_PATH 是sqlite数据库根目录

这样就可以访问数据库了。

{
state: 1,
msg: "获取成功",
data: {
pageSize: 30,
page: 0,
datas: [
{
id: "5382",
title: " 君顏爲誰笑ヽ 紅顏爲誰笑ヽ",
url: "",
content: null,
img: null,
big_id: "0",
small_id: "0"
},
{
id: "5381",
title: " 牽手望月亮 拉手看星星",
url: "",
content: null,
img: null,
big_id: "0",
small_id: "0"
}]
}
}

注:文档中还有其他切换方式,可以去试试。