MyBB Community Forums

Full Version: Mybb Sqlite Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
While I was testing mybb by adding post / delete thread and etc, mybb keeps throwing out "Integrity constraint violation: 19 column dateline is not unique". What causes this issue?
SQlite support is severely lacking I'm afraid. The only fully supported databases are MySQL/MySQLi really.
It's a bug in MyBB's inc/db_sqlite.php::replace_query() function.

MyBB only works with MySQL, the other DB drivers are all buggy.

Workaround: in inc/functions.php change

$db->replace_query("stats", $todays_stats, "dateline");
to
$db->replace_query("stats", $todays_stats);

Or if that does not work either, try in inc/db_sqlite.php

                        if(is_array($default_field) && !empty($default_field))
                        {
                                $search_bit = array();
                                foreach($default_field as $field)
                                {
                                        $search_bit[] = "{$field} = '".$replacements[$field]."'";
                                }

                                $search_bit = implode(" AND ", $search_bit);
                                $query = $this->write_query("SELECT COUNT(".$default_field[0].") as count FROM {$this->table_prefix}{$table} WHERE {$search_bit} LIMIT 1");
                                if($this->fetch_field($query, "count") == 1)
                                {
                                        $update = true;
                                }
                        }
                        else
                        {
                                $query = $this->write_query("SELECT {$default_field} FROM {$this->table_prefix}{$table}");

                                while($column = $this->fetch_array($query))
                                {
                                        if($column[$default_field] == $replacements[$default_field])
                                        {
                                                $update = true;
                                                break;
                                        }
                                }
                        }

It doesn't set $search_bit in the else tree, that's why it fails. You should remove the else tree entirely and just cast $default_field to array instead.

                        if($default_field)
                        {
                                $search_bit = array();
                                foreach((array)$default_field as $field)
                                {
                                        $search_bit[] = "{$field} = '".$replacements[$field]."'";
                                }

                                $search_bit = implode(" AND ", $search_bit);
                                $query = $this->write_query("SELECT COUNT(".$default_field[0].") as count FROM {$this->table_prefix}{$table} WHERE {$search_bit} LIMIT 1");
                                if($this->fetch_field($query, "count") == 1)
                                {
                                        $update = true;
                                }
                        }

and then cross your fingers and hope for the best

Seriously, you don't want to use this. For MyBB, go with MySQL.
(2013-03-23, 03:46 PM)Euan T. Wrote: [ -> ]SQlite support is severely lacking I'm afraid. The only fully supported databases are MySQL/MySQLi really.

Sorry forgot to update the thread. This issue has been resolved. I just changed field "mybb_stats" > dateline [bigint(30) INTEGER PRIMARY] to dateline bigint(30), then the error goes away. Smile

(2013-03-23, 03:49 PM)frostschutz Wrote: [ -> ]It's a bug in MyBB's inc/db_sqlite.php::replace_query() function.

MyBB only works with MySQL, the other DB drivers are all buggy.

Workaround: in inc/functions.php change

$db->replace_query("stats", $todays_stats, "dateline");
to
$db->replace_query("stats", $todays_stats);

Or if that does not work either, try in inc/db_sqlite.php

                        if(is_array($default_field) && !empty($default_field))
                        {
                                $search_bit = array();
                                foreach($default_field as $field)
                                {
                                        $search_bit[] = "{$field} = '".$replacements[$field]."'";
                                }

                                $search_bit = implode(" AND ", $search_bit);
                                $query = $this->write_query("SELECT COUNT(".$default_field[0].") as count FROM {$this->table_prefix}{$table} WHERE {$search_bit} LIMIT 1");
                                if($this->fetch_field($query, "count") == 1)
                                {
                                        $update = true;
                                }
                        }
                        else
                        {
                                $query = $this->write_query("SELECT {$default_field} FROM {$this->table_prefix}{$table}");

                                while($column = $this->fetch_array($query))
                                {
                                        if($column[$default_field] == $replacements[$default_field])
                                        {
                                                $update = true;
                                                break;
                                        }
                                }
                        }

It doesn't set $search_bit in the else tree, that's why it fails. You should remove the else tree entirely and just cast $default_field to array instead.

                        if($default_field)
                        {
                                $search_bit = array();
                                foreach((array)$default_field as $field)
                                {
                                        $search_bit[] = "{$field} = '".$replacements[$field]."'";
                                }

                                $search_bit = implode(" AND ", $search_bit);
                                $query = $this->write_query("SELECT COUNT(".$default_field[0].") as count FROM {$this->table_prefix}{$table} WHERE {$search_bit} LIMIT 1");
                                if($this->fetch_field($query, "count") == 1)
                                {
                                        $update = true;
                                }
                        }

and then cross your fingers and hope for the best

Seriously, you don't want to use this. For MyBB, go with MySQL.

I already fixed the error. It was an error in database Mybb installer set during installation.
(2013-03-23, 04:12 PM)Daniel Hyuuga Wrote: [ -> ]Sorry forgot to update the thread. This issue has been resolved. I just changed field "mybb_stats" > dateline [bigint(30) INTEGER PRIMARY] to dateline bigint(30), then the error goes away. Smile

Yo dawg I see you have an error so let's make an error so you can hide your error with an error.

By the way, there are several other calls to $db->replace_query() which will cause more of the same issues.

./admin/modules/home/preferences.php:40:		$db->replace_query("adminoptions", $sqlarray, "uid");
./inc/class_session.php:525:		$db->replace_query("sessions", $onlinedata, "sid", false);
./inc/functions.php:1909:	$db->replace_query("stats", $todays_stats, "dateline"); # that's yours
./install/upgrade.php:748:	$db->replace_query("upgrade_data", $replace_array, "title");

...they will all not do what they're supposed to. You have to fix db_sqlite::replace_query() for that, fix above might work or might not work, I didn't test it.

Anyway: use MySQL.
(2013-03-23, 04:53 PM)frostschutz Wrote: [ -> ]
(2013-03-23, 04:12 PM)Daniel Hyuuga Wrote: [ -> ]Sorry forgot to update the thread. This issue has been resolved. I just changed field "mybb_stats" > dateline [bigint(30) INTEGER PRIMARY] to dateline bigint(30), then the error goes away. Smile

Yo dawg I see you have an error so let's make an error so you can hide your error with an error.

By the way, there are several other calls to $db->replace_query() which will cause more of the same issues.

./admin/modules/home/preferences.php:40:		$db->replace_query("adminoptions", $sqlarray, "uid");
./inc/class_session.php:525:		$db->replace_query("sessions", $onlinedata, "sid", false);
./inc/functions.php:1909:	$db->replace_query("stats", $todays_stats, "dateline"); # that's yours
./install/upgrade.php:748:	$db->replace_query("upgrade_data", $replace_array, "title");

...they will all not do what they're supposed to. You have to fix db_sqlite::replace_query() for that, fix above might work or might not work, I didn't test it.

Anyway: use MySQL.

Nah its okay, I got it resolved. Plus the error in first post messes MyBB up too, and I have reason to use Sqlite over MySQL xD

Plus date cant be a primary key since date tend to have same value if users posts too. (logic)
Why do I even try to help ...
(2013-03-23, 05:56 PM)frostschutz Wrote: [ -> ]Why do I even try to help ...

lol im sorry if I didnt take your advice, but honestly I'm grateful your trying to help, and for mysql part, I really cant use it since I need both of my app and mybb to be able to communicate and read each others data.