Title: PDO plugin: Fixing errors in the sqlite driver
Last modified: August 20, 2016

---

# PDO plugin: Fixing errors in the sqlite driver

 *  [kyamagu](https://wordpress.org/support/users/kyamagu/)
 * (@kyamagu)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/fixing-errors-in-the-sqlite-driver/)
 * I had to do a couple of things to make it work for 3.2.1.
 * First, you should follow the instruction written in [http://epilogica.info/articles/how-to/wordpress/installing-sqlite-PDO.htm](http://epilogica.info/articles/how-to/wordpress/installing-sqlite-PDO.htm).
   Make sure the permission of `wp-content` is writable by php. Next, you should
   do the following:
 * wp-content/pdo/driver_sqlite/pdo_sqlite_driver.php: lines 57-58: uncomment these
 *     ```
       $this->rewrite_date_sub();
       $this->rewriteNowUsage();
       ```
   
 * wp-content/pdo/PDOEngine.php: line 615: basically the original approach crashes
   when contents have ‘),’ sequence. Needed to replace this line:
 *     ```
       $explodedParts = explode ('),', $match[2]);
       ```
   
 * with:
 *     ```
       $explodedParts = $this->parseMultipleInserts($match[2]);
       ```
   
 * and add the following function after `function determineQueryType()`:
 *     ```
       private function parseMultipleInserts($values){
           $tokens = preg_split("/(''|'|\),)/",$values,-1,PREG_SPLIT_DELIM_CAPTURE);
           $explodedParts = array();
           $part = '';
           $literal = FALSE;
           foreach ($tokens as $tok) {
             $part = $part.$tok;
             switch ($tok) {
               case "),":
                 if (!$literal) {
                   $explodedParts[] = $part;
                   $part = '';
                 }
                 break;
               case "'":
                 if ($literal) { $literal = FALSE; }
                 else { $literal = TRUE; }
                 break;
             }
           }
           if (!empty($part)) {
             $explodedParts[] = $part;
           }
           return $explodedParts;
         }
       ```
   
 * wp-content/pdo/db.php: lines 116-118: replace these lines
 *     ```
       function escape($string) {
               return addslashes($string);
       }
       ```
   
 * with these:
 *     ```
       function escape($string) {
       	if ( is_array($string) ) {
       		foreach ($string as &$value) {
       			$value = addslashes($value);
       		}
       		return $string;
       	} else {
       		return addslashes($string);
       	}
       }
       ```
   
 * Hope these fixes would solve weird errors.
 * If you’re using the older version of wordpress, you might also need to change
   permission of `wp-admin/includes/upgrade.php` and `wp-admin/includes/schema.php`
   to be writable by php.

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [kyamagu](https://wordpress.org/support/users/kyamagu/)
 * (@kyamagu)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/fixing-errors-in-the-sqlite-driver/#post-2267427)
 * Actually I further needed to do the following:
 * In wp-content/pdo/PDOEngine.php, the fix shown above doesn’t work. Use the following
   function instead:
 *     ```
       private function parseMultipleInserts($values){
       	  $tokens = preg_split("/(''|'|\),)/",$values,-1,PREG_SPLIT_DELIM_CAPTURE);
       	  $explodedParts = array();
       	  $part = '';
       	  $literal = FALSE;
       	  foreach ($tokens as $tok) {
       	    switch ($tok) {
       	      case "),":
       	        if (!$literal) {
       	          $explodedParts[] = $part;
       	          $part = '';
       	        }
       	        else { $part = $part.$tok; }
       	        break;
       	      case "'":
       	        if ($literal) { $literal = FALSE; }
       	        else { $literal = TRUE; }
                 $part = $part.$tok;
       	        break;
       	      default:
                 $part = $part.$tok;
       	    }
       	  }
       	  if (!empty($part)) {
       	    $explodedParts[] = $part;
       	  }
       	  return $explodedParts;
       	}
       ```
   
 * In wp-content/pdo/driver_sqlite/schema.php: line 25: Maybe not needed:
 *     ```
       //$query = "create table modTimes (modFile text not null primary key, modTime text not null default '0000-00-00 00:00:00')";
       ```
   
 * In wp-content/pdo/driver_sqlite/pdo_sqlite_driver.php: after line 63: insert 
   this:
 *     ```
       $this->rewriteBoolean();
       ```
   
 * and add the following method in the same file:
 *     ```
       /**
       	*	method to rewrite the use of the true/false expression for use in sqlite
       	*
       	*	sqlite doesn't recognize true or false as expression and necessary to be
       	* converted to 0:false or 1:true
       	*/
       	private function rewriteBoolean(){
       		$query = $this->istrreplace('true', "1", $this->_query);
       		$query = $this->istrreplace('false', "0", $this->_query);
       		$this->_query = $query;
       	}
       ```
   
 * In wp-content/pdo/db.php: change _real_escape()
 *     ```
       function _real_escape($string) {
       		//return addslashes( $string );
       		return addslashes($this->escape( $string ));
       	}
       ```
   
 * In wp-content/pdo/db.php: change escape()
 *     ```
       function escape($string) {
       	  if ( is_array($string) ) {
       		  foreach ($string as &$value) {
       			  $value = sqlite_escape_string($value);
       		  }
       		  return $string;
       	  } else {
       		  return sqlite_escape_string($string);
       	  }
         }
       ```
   
 * In wp-content/db.php: lines 153-155: comment out
 *     ```
       //if (version_compare($v, "2.4") == -1){
       	//	changeFiles_2_4();
       	//}
       ```
   
 * In wp-content/pdo/wp_install.php: Maybe better to rewrite up-to-date version 
   of wp-install:
 *     ```
       function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '' ) {
       	global $wp_rewrite, $wpdb;
   
       	if ( !empty( $deprecated ) )
       		_deprecated_argument( __FUNCTION__, '2.6' );
   
       	//wp_check_mysql_version();
       	wp_cache_flush();
       	/**** changes start here ***/
       	switch (DB_TYPE):
       		case 'sqlite':
       			require PDODIR . '/driver_sqlite/schema.php';
       			installdb();
       			break;
       		case 'mysql':
       			make_db_current_silent();
       			break;
       	endswitch;
       	/**** changes end ***/
       	populate_options();
       	populate_roles();
   
       	update_option('blogname', $blog_title);
       	update_option('admin_email', $user_email);
       	update_option('blog_public', $public);
   
       	$guessurl = wp_guess_url();
   
       	update_option('siteurl', $guessurl);
   
       	// If not a public blog, don't ping.
       	if ( ! $public )
       		update_option('default_pingback_flag', 0);
   
       	// Create default user.  If the user already exists, the user tables are
       	// being shared among blogs.  Just set the role in that case.
       	$user_id = username_exists($user_name);
       	$user_password = trim($user_password);
       	$email_password = false;
       	if ( !$user_id && empty($user_password) ) {
       		$user_password = wp_generate_password( 12, false );
       		$message = __('<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.');
       		$user_id = wp_create_user($user_name, $user_password, $user_email);
       		update_user_option($user_id, 'default_password_nag', true, true);
       		$email_password = true;
       	} else if ( !$user_id ) {
       		// Password has been provided
       		$message = '<em>'.__('Your chosen password.').'</em>';
       		$user_id = wp_create_user($user_name, $user_password, $user_email);
       	} else {
       		$message =  __('User already exists. Password inherited.');
       	}
   
       	$user = new WP_User($user_id);
       	$user->set_role('administrator');
   
       	wp_install_defaults($user_id);
   
       	$wp_rewrite->flush_rules();
   
       	wp_new_blog_notification($blog_title, $guessurl, $user_id, ($email_password ? $user_password : __('The password you chose during the install.') ) );
   
       	wp_cache_flush();
   
       	return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $user_password, 'password_message' => $message);
       }
       ```
   
 *  [sewpafly](https://wordpress.org/support/users/sewpafly/)
 * (@sewpafly)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/fixing-errors-in-the-sqlite-driver/#post-2267606)
 * Where is `sqlite_escape_string` defined?
 * Update: Nevermind, I see that it should be an included php function… I guess 
   I’ll look it up.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘PDO plugin: Fixing errors in the sqlite driver’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/pdo-for-wordpress.svg)
 * [PDO (SQLite) For Wordpress](https://wordpress.org/plugins/pdo-for-wordpress/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/pdo-for-wordpress/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/pdo-for-wordpress/)
 * [Active Topics](https://wordpress.org/support/plugin/pdo-for-wordpress/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/pdo-for-wordpress/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/pdo-for-wordpress/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [sewpafly](https://wordpress.org/support/users/sewpafly/)
 * Last activity: [14 years, 6 months ago](https://wordpress.org/support/topic/fixing-errors-in-the-sqlite-driver/#post-2267606)
 * Status: not resolved