Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with SQLite in codeigniter

I'm trying to get started with sqlite in the latest version of CodeIgniter.

My database.php looks like this:

$active_group = 'default';
$active_record = TRUE;

$db ['default'] ['hostname'] ='';
$db ['default'] ['username'] ='';
$db ['default'] ['password'] ='';
$db ['default'] ['database'] = APPPATH. 'db / producers.sqlite';
$db ['default'] ['dbdriver'] = 'sqlite';
$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;

I have created my table is produced and put data into it.

I'm trying to collect data with this code:

$query = $ this-> db-> get ('Producers');


foreach ($ query-> result () as $ row)
{
echo $ row-> name;
}

This gives me the following error: Fatal error: [] operator not supported for strings in / Applications / MAMP / htdocs / webites / api / public_html / system / database / DB_driver.php on line 1183

Or this error sometimes:

A Database error occurred

Error Number: 1

SQL logic error or missing database

SELECT * FROM (Producers)

Filename: / Applications / MAMP / htdocs / webites / api / public_html / controllers / welcome.php

Line Number: 23

How do I resolve it? I can not add data to either, there are similar errors

like image 604
IVar Avatar asked Nov 30 '11 12:11

IVar


4 Answers

I've had the same issue when using CI 2.1.0 and found the following fix for the fatal error:

In system/database/DB_driver.php change:

Line 1165

$message = $error;

to

$message[] = $error;

Line 1169

$message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;

to

$message[] = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;

Source

like image 101
Jalfie Avatar answered Nov 18 '22 10:11

Jalfie


Never tried to use SQLite with CI, but I found a possible answer to your problem. Try adding 'sqlite:' in front of your database name, like:

$db ['default'] ['database'] = 'sqlite:'.APPPATH.'db / producers.sqlite';

Source.

like image 45
thomaux Avatar answered Nov 18 '22 10:11

thomaux


To solve the error "Fatal error: [] operator not supported for strings"

I modify file in *DB_driver.php:1171*

adding this line:

$message = (array)$message;
like image 1
porquero Avatar answered Nov 18 '22 11:11

porquero


There is a bug in 2.1.0 preventing it from working with SQLite databases. You need to have updated version of CodeIgniter 2.1.1.

like image 1
Ansari Avatar answered Nov 18 '22 11:11

Ansari