CodeIgniter 3.0.6中的SQLite3

问题描述 投票:5回答:2

我找不到有关如何在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

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

php codeigniter sqlite3
2个回答
13
投票

您正在使用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
);

0
投票
'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
© www.soinside.com 2019 - 2024. All rights reserved.