CodeIgniter Sqlite不工作 [英] CodeIgniter Sqlite not working
问题描述
每当我在我的模型中查询我的数据库(sqlite)(im使用codeigniter,下面的完整代码):
Whenever I query my database (sqlite) like this in my model (im using codeigniter, full code below):
$this->db->select('post');
$query = $this->db->get('posts');
return $query->result_array();
我得到以下错误:
Fatal error: Call to a member function rowCount() on a non-object in /codeigniter/system/database/drivers/pdo/pdo_result.php on line 42
当将查询更改为不存在的时候,我得到一个正确错误,例如:
When changing the query to something nonexistent I get a "proper" error, something like:
A Database Error Occurred
Error Number: HY000
no such column: posst
SELECT posst FROM posts
Filename: /codeigniter/models/post.php
Line Number: 8
数据库实际上是工作,但有一些我缺少的。
我尝试重新创建数据库。它字面上有1表与1列,但我只是不能得到任何数据。我也试着用不同的管理程序创建它,但没有效果。我确保它是一个Sqlite 3 db,它是由webserver根据phpinfo支持。
任何人都有一个线索在哪里我犯了一个错误?
Which leads me to believe the database is actually working, but there is something I am missing. I have tried recreating the database. It literally has 1 table with 1 column, but I just cannot get any data out. I also tried creating it with different "admin" programs but to no avail. I made sure it is an Sqlite 3 db, which is supported by the webserver according to phpinfo. Does anybody have a clue where I am making a mistake?
--------全代码:
我的帖子模型模型/ post.php
-------- full code: my post model in models/post.php
<?php
class Post extends CI_Model{
function get_posts(){
$this->db->select('posst');
$query = $this->db->get('posts');
return $query->result_array();
}
}
$ b b
我的控制器在控制器/ posts.php:
My controller in controller/posts.php :
<?php
class Posts extends CI_Controller{
function index(){
$this->load->model('post');
$data['posts']=$this->post->get_posts();
echo"<pre>";
print_r($data['posts']);
echo"</pre>";
}
}
$ b b
我在database.php中的数据库配置:
My database config in database.php :
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'sqlite:/home/******/******/www/wtp3/codeigniter/db/wtp35.sqlite';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'pdo';
$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;
推荐答案
此修复的积分与S.Stüvel,J 。Bransen和S. Timmer。这是一个特定服务器的修复,所以YMMV。
Credits for this fix are with S. Stüvel, J. Bransen and S. Timmer. This is a fix for a specific server, so YMMV. It did the trick for me though.
在pdo_driver.php中,第81行改变:
In pdo_driver.php, starting line 81 change:
empty($this->database) OR $this->hostname .= ';dbname='.$this->database;
$this->trans_enabled = FALSE;
$this->_random_keyword = ' RND('.time().')'; // database specific random keyword
}
到
if(strpos($this->database, 'sqlite') !== FALSE) {
$this->hostname = $this->database;
$this->_random_keyword = ' RANDOM()';
}
else {
$this->hostname .= ";dbname=".$this->database;
$this->_random_keyword = ' RND('.time().')'; // database specific random keyword
}
$this->trans_enabled = FALSE;
}
在第189行将整个函数_execute($ sql)更改为
On line 189 change the entire function _execute($sql) to
function _execute($sql)
{
$sql = $this->_prep_query($sql);
$result_id = $this->conn_id->query($sql);
if (is_object($result_id))
{
$this->affect_rows = $result_id->rowCount();
}
else
{
$this->affect_rows = 0;
}
return $result_id;
}
然后在pdo_result.php中更改:
在第29行
Then in pdo_result.php change": On line 29 change
public $num_rows;
到
var $pdo_results = '';
var $pdo_index = 0;
在第36行上替换整个函数
on line 36 replace entire function
public function num_rows()
{
if (is_int($this->num_rows))
{
return $this->num_rows;
}
elseif (($this->num_rows = $this->result_id->rowCount()) > 0)
{
return $this->num_rows;
}
$this->num_rows = count($this->result_id->fetchAll());
$this->result_id->execute();
return $this->num_rows;
}
与:
function num_rows()
{
if ( ! $this->pdo_results ) {
$this->pdo_results = $this->result_id->fetchAll(PDO::FETCH_ASSOC);
}
return sizeof($this->pdo_results);
然后在第60行更改
function num_fields()
{
return $this->result_id->columnCount();
}
到:
function num_fields()
{
if ( is_array($this->pdo_results) ) {
return sizeof($this->pdo_results[$this->pdo_index]);
} else {
return $this->result_id->columnCount();
}
}
然后在第94行更改:
function field_data()
{
$data = array();
try
{
for($i = 0; $i < $this->num_fields(); $i++)
{
$data[] = $this->result_id->getColumnMeta($i);
}
return $data;
}
catch (Exception $e)
{
if ($this->db->db_debug)
{
return $this->db->display_error('db_unsuported_feature');
}
return FALSE;
}
}
到:
function field_data()
{
if ($this->db->db_debug)
{
return $this->db->display_error('db_unsuported_feature');
}
return FALSE;
}
然后第146行更改:
return FALSE;
到
$this->pdo_index = $n;
然后在第159行更改
function _fetch_assoc()
{
return $this->result_id->fetch(PDO::FETCH_ASSOC);
}
到
function _fetch_assoc()
{
if ( is_array($this->pdo_results) ) {
$i = $this->pdo_index;
$this->pdo_index++;
if ( isset($this->pdo_results[$i]))
return $this->pdo_results[$i];
return null;
}
return $this->result_id->fetch(PDO::FETCH_ASSOC);
}
最后在第174行更改:
And finally on line 174 change:
function _fetch_object()
{
return $this->result_id->fetchObject();
到
function _fetch_object()
{
if ( is_array($this->pdo_results) ) {
$i = $this->pdo_index;
$this->pdo_index++;
if ( isset($this->pdo_results[$i])) {
$back = new stdClass();
foreach ( $this->pdo_results[$i] as $key => $val ) {
$back->$key = $val;
}
return $back;
}
return null;
}
return $this->result_id->fetch(PDO::FETCH_OBJ);
}
这对我有用。再次,不是我的工作,信贷出去了S.Stüvel,J.布兰森和S.蒂默。
相当长的答案,但我希望这有助于。
This worked for me. Again, not my work, credit goes out to S. Stüvel, J. Bransen and S. Timmer. Rather long answer, but i hope this helps.
这篇关于CodeIgniter Sqlite不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!