CodeIgniter 3.0.6中的SQLite3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeIgniter 3.0.6中的SQLite3相关的知识,希望对你有一定的参考价值。

我找不到有关如何在CodeIgniter中使用sqlite3的任何文档,但它确实说它受支持。

这是我当前的数据库配置:

$db['default']['hostname'] = '';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = 'db/base.db';
$db['default']['dbdriver'] = 'sqlite3';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

但是我在页面加载时遇到了非常不合理的错误

Unable to connect to your database server using the provided settings.

Filename: core/CodeIgniter.php

Line Number: 500

所以我的问题是,为什么我的配置不起作用,我怎么能让它工作?

答案

您正在使用CI2语法,我不知道您在哪里获得它,因为在默认包中您可以找到与下面相同的代码,其中您只需要定义的是database(db的路径)和dbdriver

$db['default'] = array(
    'dsn'      => '',
    'hostname' => '',
    'username' => '',
    'password' => '',
    'database' => './application/database/data.db',
    'dbdriver' => 'sqlite3',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt'  => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

或者您可以使用此(使用PDO数据库驱动程序,需要dsn字符串,否则CI将尝试构建它)

$db['default'] = array(
    'dsn'      => 'sqlite:application/database/data.db',// path/to/database
    'hostname' => '',
    'username' => '',
    'password' => '',
    'database' => '',
    'dbdriver' => 'pdo',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt'  => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);
另一答案
'database' => './application/database/data.db',

如果不工作替换

'database' => APPPATH.'database/data.db',

'dsn'      => 'sqlite:application/database/data.db',// path/to/database

如果不工作替换

'dsn'      => 'sqlite:'.APPPATH.'/database/data.db',// path/to/database

以上是关于CodeIgniter 3.0.6中的SQLite3的主要内容,如果未能解决你的问题,请参考以下文章

删除 index.php codeigniter 3.0.6

如何删除codeigniter路径中的index.php?

Codeigniter 会话不适用于 PHP 7

Codeigniter 错误:调用未定义的函数 mysql_pconnect()

Codeigniter insert_batch 错误

move_uploaded_file 返回 false 但 $_FILES 错误为 0