Example #1
0
 public function exec(&$statement)
 {
     if (@sqlite_exec($this->link, $statement)) {
         return sqlite_changes($this->link);
     }
     return false;
 }
Example #2
0
 /**
  * Constructs a new SqliteResultSet
  *
  * \param $sql
  *   The sql query to execute.
  *
  * \param $backend
  *   A reference to the used backend.
  */
 function SqliteResultSet($sql, &$backend)
 {
     assert('is_string($sql)');
     $this->sql = $sql;
     $this->backend =& $backend;
     $this->rs = sqlite_query($this->backend->handle, $sql) or trigger_error(sprintf('Query failed (%s)', sqlite_error_string(sqlite_last_error($this->backend->handle))), E_USER_ERROR);
     $this->rows_affected = sqlite_changes($this->backend->handle);
 }
 protected function _doExec($query)
 {
     if ($qI = sqlite_query($query, $this->_connection)) {
         return sqlite_changes($this->_connection);
     } else {
         throw new jException('jelix~db.error.query.bad', sqlite_error_string($this->_connection) . '(' . $query . ')');
     }
 }
Example #4
0
function sql_affected_rows()
{
    global $con;
    if (DB_TYPE == 'mysql') {
        $sql = mysql_affected_rows();
    } elseif (DB_TYPE == 'sqlite') {
        $sql = sqlite_changes($con);
    }
    return $sql;
}
 public function getLength()
 {
     if ($this->r === true) {
         return sqlite_changes($this->r);
     } else {
         if ($this->r === false) {
             return 0;
         }
     }
     return sqlite_num_rows($this->r);
 }
 public function exec($sql, $connResource)
 {
     if (empty($sql)) {
         return 0;
     }
     sqlite_exec($connResource, $sql);
     // echo '<pre>';
     // print_r(debug_backtrace());
     // debug_print_backtrace();
     // echo '</pre>';
     // delete from table 结果为0,原因未知。
     // 使用 delete from table where 1 能返回正确结果
     return sqlite_changes($connResource);
 }
Example #7
0
 protected function _query($sql)
 {
     $result = sqlite_query($this->_connection, $sql);
     $error = sqlite_last_error($this->_connection);
     $this->_errorHandler($error, $error != 0 ? sqlite_error_string($error) : '');
     if ($result && $this->queryHasResultSet($sql)) {
         $this->_numRows = sqlite_num_rows($result);
         $resultObject = $this->createRecordsetObject();
         $resultObject->setResult($result);
     } else {
         $this->_numRows = sqlite_changes($this->_connection);
         $resultObject = $this->createResultObject();
     }
     return $resultObject;
 }
Example #8
0
 /**
  * Run a query
  *
  * @param string $sql
  * @param resource $connection DB Connection
  * @throws Exception MySQL error
  */
 public function __construct($sql, $connection)
 {
     parent::__construct($sql);
     $this->connection = $connection;
     $errorMessage = '';
     // Overwritten by sqlite_query
     $resource = sqlite_query($this->connection, $sql, SQLITE_ASSOC, $errorMessage);
     if (false === $resource) {
         throw new Exception('SQLite Error: ' . $errorMessage);
     } else {
         $this->resource = $resource;
         $this->setNumberOfRows(sqlite_num_rows($resource));
         $this->columns = $this->getColumnTypes();
         $this->rowsAffected = sqlite_changes($this->connection);
         $this->lastId = sqlite_last_insert_rowid($this->connection);
     }
 }
function execute($db, $sql)
{
    if (IS_SQLITE3) {
        if ($db->query($sql)) {
            return $db->changes();
        } else {
            return false;
        }
    } else {
        if (sqlite_query($db, $sql, 0666, $error)) {
            //echo "<font color='red'>1.".$error."</font>";
            return sqlite_changes($db);
        } else {
            //echo "<font color='red'>2.".$error."</font>";
            return false;
        }
    }
}
Example #10
0
 /**
  * SQLite::query()
  * 
  * @param mixed $sql
  * @return
  */
 public function query($sql)
 {
     $data = array();
     $i = 0;
     $err_msg = "";
     $result = sqlite_query($this->conn, $sql, SQLITE_ASSOC, $err_msg);
     if ($result == false) {
         throw new Exception("{$sql}<br />执行错误:" . $err_msg);
     } else {
         while ($arr = sqlite_fetch_array($result, SQLITE_ASSOC)) {
             $data[$i++] = $arr;
         }
     }
     if (count($data) > 0) {
         return $data;
     } else {
         return sqlite_changes($this->conn);
     }
 }
Example #11
0
 function getAffectedRows()
 {
     return sqlite_changes($this->conn);
 }
Example #12
0
 /**
  * Gets the number of rows affected by a query.
  *
  * @return number of rows affected by the last query
  */
 function affectedRows()
 {
     return @sqlite_changes($this->connection);
 }
Example #13
0
 public function __construct($sql, $connection_handle, $result_set_handle)
 {
     parent::__construct($sql, $connection_handle, $result_set_handle);
     $this->n_rows = sqlite_num_rows($result_set_handle);
     $this->n_rows_affected = sqlite_changes($connection_handle);
 }
 /**
  * Sets the number of rows affected by the query
  * 
  * @param  fResult $result    The result object for the query
  * @param  mixed   $resource  Only applicable for `ibm_db2`, `pdo`, `oci8` and `sqlsrv` extentions or `mysqli` prepared statements - this is either the `PDOStatement` object, `mysqli_stmt` object or the `oci8` or `sqlsrv` resource
  * @return void
  */
 private function setAffectedRows($result, $resource = NULL)
 {
     if ($this->extension == 'ibm_db2') {
         $insert_update_delete = preg_match('#^\\s*(INSERT|UPDATE|DELETE)\\b#i', $result->getSQL());
         $result->setAffectedRows(!$insert_update_delete ? 0 : db2_num_rows($resource));
     } elseif ($this->extension == 'mssql') {
         $affected_rows_result = mssql_query('SELECT @@ROWCOUNT AS rows', $this->connection);
         $result->setAffectedRows((int) mssql_result($affected_rows_result, 0, 'rows'));
     } elseif ($this->extension == 'mysql') {
         $result->setAffectedRows(mysql_affected_rows($this->connection));
     } elseif ($this->extension == 'mysqli') {
         if (is_object($resource)) {
             $result->setAffectedRows($resource->affected_rows);
         } else {
             $result->setAffectedRows(mysqli_affected_rows($this->connection));
         }
     } elseif ($this->extension == 'oci8') {
         $result->setAffectedRows(oci_num_rows($resource));
     } elseif ($this->extension == 'pgsql') {
         $result->setAffectedRows(pg_affected_rows($result->getResult()));
     } elseif ($this->extension == 'sqlite') {
         $result->setAffectedRows(sqlite_changes($this->connection));
     } elseif ($this->extension == 'sqlsrv') {
         $result->setAffectedRows(sqlsrv_rows_affected($resource));
     } elseif ($this->extension == 'pdo') {
         // This fixes the fact that rowCount is not reset for non INSERT/UPDATE/DELETE statements
         try {
             if (!$resource || !$resource->fetch()) {
                 throw new PDOException();
             }
             $result->setAffectedRows(0);
         } catch (PDOException $e) {
             // The SQLite PDO driver seems to return 1 when no rows are returned from a SELECT statement
             if ($this->type == 'sqlite' && $this->extension == 'pdo' && preg_match('#^\\s*SELECT#i', $result->getSQL())) {
                 $result->setAffectedRows(0);
             } elseif (!$resource) {
                 $result->setAffectedRows(0);
             } else {
                 $result->setAffectedRows($resource->rowCount());
             }
         }
     }
 }
Example #15
0
 public function rowCount()
 {
     return sqlite_changes($this->_link);
 }
Example #16
0
 /**
  * Non funziona con query del tipo SELECT
  */
 function sql_affected_rows()
 {
     return sqlite_changes($this->sql_link);
 }
Example #17
0
 /**
  * Función que elimina un registro de la base de datos
  * @param string $tabla
  * @param array $valores
  * @return int
  */
 public function delete($tabla = '', $valores = array())
 {
     $where = '';
     $valores = $this->_prepare($valores);
     if (is_array($valores)) {
         foreach ($valores as $key => $value) {
             if ($where != '') {
                 $where .= ' AND ';
             }
             if ($this->isReserved($value)) {
                 $where .= "{$key} {$value}";
             } else {
                 $where .= "{$key} = '{$value}'";
             }
         }
     } else {
         if (trim($valores) != '') {
             $where = $valores;
         }
     }
     if ($where != '') {
         $where = 'WHERE ' . $where;
     }
     $this->_query("DELETE FROM {$tabla} {$where}");
     if (sqlite_last_error()) {
         return false;
     } else {
         return sqlite_changes();
     }
 }
Example #18
0
 function _affectedrows()
 {
     return sqlite_changes($this->_connectionID);
 }
Example #19
0
 function query($query)
 {
     // For reg expressions
     $query = str_replace("/[\n\r]/", '', trim($query));
     // initialise return
     $return_val = 0;
     // Flush cached values..
     $this->flush();
     // Log how the function was called
     $this->func_call = "\$db->query(\"{$query}\")";
     // Keep track of the last query for debug..
     $this->last_query = $query;
     // Perform the query via std mysql_query function..
     $this->result = @sqlite_query($this->dbh, $query);
     $this->count(true, true);
     // If there is an error then take note of it..
     if (@sqlite_last_error($this->dbh)) {
         $err_str = sqlite_error_string(sqlite_last_error($this->dbh));
         $this->register_error($err_str);
         $this->show_errors ? trigger_error($err_str, E_USER_WARNING) : null;
         return false;
     }
     // Query was an insert, delete, update, replace
     if (preg_match("/^(insert|delete|update|replace)\\s+/i", $query)) {
         $this->rows_affected = @sqlite_changes($this->dbh);
         // Take note of the insert_id
         if (preg_match("/^(insert|replace)\\s+/i", $query)) {
             $this->insert_id = @sqlite_last_insert_rowid($this->dbh);
         }
         // Return number fo rows affected
         $return_val = $this->rows_affected;
     } else {
         // Take note of column info
         $i = 0;
         while ($i < @sqlite_num_fields($this->result)) {
             $this->col_info[$i]->name = sqlite_field_name($this->result, $i);
             $this->col_info[$i]->type = null;
             $this->col_info[$i]->max_length = null;
             $i++;
         }
         // Store Query Results
         $num_rows = 0;
         while ($row = @sqlite_fetch_array($this->result, SQLITE_ASSOC)) {
             // Store relults as an objects within main array
             $obj = (object) $row;
             //convert to object
             $this->last_result[$num_rows] = $obj;
             $num_rows++;
         }
         // Log number of rows the query returned
         $this->num_rows = $num_rows;
         // Return number of rows selected
         $return_val = $this->num_rows;
     }
     // If debug ALL queries
     $this->trace || $this->debug_all ? $this->debug() : null;
     return $return_val;
 }
Example #20
0
 function affectedRows()
 {
     return sqlite_changes($this->link);
 }
Example #21
0
 function affected_rows()
 {
     return $this->link_id ? @sqlite_changes($this->link_id) : false;
 }
Example #22
0
 /**
  * Executes given SQL statement.
  * @param string $queryString
  * @return resource|bool
  * @throws DriverException
  */
 public function runQuery($queryString)
 {
     $this->affectedRows = false;
     if ($this->dbCharset !== null) {
         $queryString = iconv($this->charset, $this->dbCharset . '//IGNORE', $queryString);
     }
     if ($this->unbuffered) {
         $result = @sqlite_unbuffered_query($this->resource, $queryString, null, $error);
     } else {
         $result = @sqlite_query($this->resource, $queryString, null, $error);
     }
     if ($error && $result === false) {
         throw new DriverException($error, sqlite_last_error($this->resource), $queryString);
     }
     $this->affectedRows = @sqlite_changes($this->resource);
     return $result;
 }
Example #23
0
 /**
  * Returns the number of rows affected
  *
  * @param resource $result
  * @param resource $connection
  * @return mixed MDB2 Error Object or the number of rows affected
  * @access private
  */
 function _affectedRows($connection, $result = null)
 {
     if (is_null($connection)) {
         $connection = $this->getConnection();
         if (PEAR::isError($connection)) {
             return $connection;
         }
     }
     return @sqlite_changes($connection);
 }
Example #24
0
 /**
  * 查询
  *
  * $use_connection_type 默认不传为自动判断,可传true/false,若传字符串(只支持a-z0-9的字符串),则可以切换到另外一个连接,比如传other,则可以连接到$this->_connection_other_id所对应的ID的连接
  *
  * @param string $sql 查询语句
  * @param string $as_object 是否返回对象
  * @return Database_Driver_SQLite_Result
  */
 public function query($sql, $as_object = null, $use_master = null)
 {
     $sql = trim($sql);
     if (preg_match('#^([a-z]+)(?: |\\n|\\r)#i', $sql, $m)) {
         $type = strtoupper($m[1]);
     }
     # 连接数据库
     $connection = $this->connection();
     # 记录调试
     if (IS_DEBUG) {
         Core::debug()->info($sql, 'SQLite');
         static $is_sql_debug = null;
         if (null === $is_sql_debug) {
             $is_sql_debug = (bool) Core::debug()->profiler('sql')->is_open();
         }
         if ($is_sql_debug) {
             $db = $this->_get_hostname_by_connection_hash($this->connection_id());
             $benchmark = Core::debug()->profiler('sql')->start('Database', 'sqlite://' . $db);
         }
     }
     static $is_no_cache = null;
     if (null === $is_no_cache) {
         $is_no_cache = (bool) Core::debug()->profiler('nocached')->is_open();
     }
     //显示无缓存数据
     if ($is_no_cache && strtoupper(substr($sql, 0, 6)) == 'SELECT') {
         $sql = 'SELECT SQL_NO_CACHE' . substr($sql, 6);
     }
     // Execute the query
     if (($result = sqlite_query($sql, $connection)) === false) {
         if (isset($benchmark)) {
             // This benchmark is worthless
             $benchmark->delete();
         }
         if (IS_DEBUG) {
             $err = 'Error:' . sqlite_error_string($connection) . '. SQL:' . $sql;
         } else {
             $err = sqlite_error_string($connection);
         }
         throw new Exception($err, sqlite_last_error($connection));
     }
     if (isset($benchmark)) {
         # 在线查看SQL情况
         if ($is_sql_debug) {
             $data = array();
             $data[0]['db'] = $db;
             $data[0]['select_type'] = '';
             $data[0]['table'] = '';
             $data[0]['key'] = '';
             $data[0]['key_len'] = '';
             $data[0]['Extra'] = '';
             $data[0]['query'] = '';
             $data[0]['type'] = '';
             $data[0]['id'] = '';
             $data[0]['row'] = count($result);
             $data[0]['ref'] = '';
             $data[0]['all rows'] = '';
             $data[0]['possible_keys'] = '';
             if (strtoupper(substr($sql, 0, 6)) == 'SELECT') {
                 $re = sqlite_query('EXPLAIN ' . $sql, $connection);
                 $i = 0;
                 while (true == ($row = sqlite_fetch_array($re, SQLITE_NUM))) {
                     $data[$i]['select_type'] = (string) $row[1];
                     $data[$i]['table'] = (string) $row[2];
                     $data[$i]['key'] = (string) $row[5];
                     $data[$i]['key_len'] = (string) $row[6];
                     $data[$i]['Extra'] = (string) $row[9];
                     if ($i == 0) {
                         $data[$i]['query'] = '';
                     }
                     $data[$i]['type'] = (string) $row[3];
                     $data[$i]['id'] = (string) $row[0];
                     $data[$i]['ref'] = (string) $row[7];
                     $data[$i]['all rows'] = (string) $row[8];
                     $data[$i]['possible_keys'] = (string) $row[4];
                     $i++;
                 }
             }
             $data[0]['query'] = $sql;
         } else {
             $data = null;
         }
         Core::debug()->profiler('sql')->stop($data);
     }
     // Set the last query
     $this->last_query = $sql;
     if ($type === 'INSERT' || $type === 'REPLACE') {
         // Return a list of insert id and rows created
         return array(sqlite_last_insert_rowid($connection), sqlite_changes($connection));
     } elseif ($type === 'UPDATE' || $type === 'DELETE') {
         // Return the number of rows affected
         return sqlite_changes($connection);
     } else {
         // Return an iterator of results
         return new Database_Driver_SQLite_Result($result, $sql, $as_object, $this->config);
     }
 }
Example #25
0
/**
 * Supprime des enregistrements d'une table
 *
 * @param string $table         Nom de la table SQL
 * @param string|array $where   Conditions à vérifier
 * @param string $serveur       Nom du connecteur
 * @param bool $requeter        Exécuter la requête, sinon la retourner
 * @return bool|string
 *     - int : nombre de suppressions réalisées,
 *     - Texte de la requête si demandé,
 *     - False en cas d'erreur.
**/
function spip_sqlite_delete($table, $where = '', $serveur = '', $requeter = true)
{
    $res = spip_sqlite_query(_sqlite_calculer_expression('DELETE FROM', $table, ',') . _sqlite_calculer_expression('WHERE', $where), $serveur, $requeter);
    // renvoyer la requete inerte si demandee
    if (!$requeter) {
        return $res;
    }
    if ($res) {
        $link = _sqlite_link($serveur);
        if (_sqlite_is_version(3, $link)) {
            return $res->rowCount();
        } else {
            return sqlite_changes($link);
        }
    } else {
        return false;
    }
}
 /**
  * Executes a sql statement.
  *
  * @param string $key Cache key
  * @param int $expire Expiration time in seconds
  * @return object Query results object
  * @throws Exception When database is not defined
  */
 public function execute($key = null, $expire = 0)
 {
     if (!$this->db) {
         throw new Exception('Database is not defined.');
     }
     if ($key !== null) {
         $result = $this->fetch($key);
         if ($this->is_cached) {
             return $result;
         }
     }
     $result = null;
     $this->is_cached = false;
     $this->num_rows = 0;
     $this->affected_rows = 0;
     $this->insert_id = -1;
     $this->last_query = $this->sql;
     if ($this->stats_enabled) {
         if (empty($this->stats)) {
             $this->stats = array('queries' => array());
         }
         $this->query_time = microtime(true);
     }
     if (!empty($this->sql)) {
         $error = null;
         switch ($this->db_type) {
             case 'pdo':
                 try {
                     $result = $this->db->prepare($this->sql);
                     if (!$result) {
                         $error = $this->db->errorInfo();
                     } else {
                         $result->execute();
                         $this->num_rows = $result->rowCount();
                         $this->affected_rows = $result->rowCount();
                         $this->insert_id = $this->db->lastInsertId();
                     }
                 } catch (PDOException $ex) {
                     $error = $ex->getMessage();
                 }
                 break;
             case 'mysqli':
                 $result = $this->db->query($this->sql);
                 if (!$result) {
                     $error = $this->db->error;
                 } else {
                     if (is_object($result)) {
                         $this->num_rows = $result->num_rows;
                     } else {
                         $this->affected_rows = $this->db->affected_rows;
                     }
                     $this->insert_id = $this->db->insert_id;
                 }
                 break;
             case 'mysql':
                 $result = mysql_query($this->sql, $this->db);
                 if (!$result) {
                     $error = mysql_error();
                 } else {
                     if (!is_bool($result)) {
                         $this->num_rows = mysql_num_rows($result);
                     } else {
                         $this->affected_rows = mysql_affected_rows($this->db);
                     }
                     $this->insert_id = mysql_insert_id($this->db);
                 }
                 break;
             case 'pgsql':
                 $result = pg_query($this->db, $this->sql);
                 if (!$result) {
                     $error = pg_last_error($this->db);
                 } else {
                     $this->num_rows = pg_num_rows($result);
                     $this->affected_rows = pg_affected_rows($result);
                     $this->insert_id = pg_last_oid($result);
                 }
                 break;
             case 'sqlite':
                 $result = sqlite_query($this->db, $this->sql, SQLITE_ASSOC, $error);
                 if ($result !== false) {
                     $this->num_rows = sqlite_num_rows($result);
                     $this->affected_rows = sqlite_changes($this->db);
                     $this->insert_id = sqlite_last_insert_rowid($this->db);
                 }
                 break;
             case 'sqlite3':
                 $result = $this->db->query($this->sql);
                 if ($result === false) {
                     $error = $this->db->lastErrorMsg();
                 } else {
                     $this->num_rows = 0;
                     $this->affected_rows = $result ? $this->db->changes() : 0;
                     $this->insert_id = $this->db->lastInsertRowId();
                 }
                 break;
         }
         if ($error !== null) {
             if ($this->show_sql) {
                 $error .= "\nSQL: " . $this->sql;
             }
             throw new Exception('Database error: ' . $error);
         }
     }
     if ($this->stats_enabled) {
         $time = microtime(true) - $this->query_time;
         $this->stats['queries'][] = array('query' => $this->sql, 'time' => $time, 'rows' => (int) $this->num_rows, 'changes' => (int) $this->affected_rows);
     }
     return $result;
 }
Example #27
0
 /**
  * Affected Rows
  *
  * @return	int
  */
 public function affected_rows()
 {
     return sqlite_changes($this->conn_id);
 }
Example #28
0
 /**
  * Gets the number of rows affected by the data manipulation
  * query.
  *
  * @return int Number of rows affected by the last query.
  */
 function getUpdateCount()
 {
     return (int) @sqlite_changes($this->dblink);
 }
Example #29
0
/**
 * Returns the number of matched rows in a SQL query
 *
 * @access public
 * @return int  Number of matched rows
 */
function serendipity_db_matched_rows()
{
    global $serendipity;
    // It is unknown whether sqllite returns rows MATCHED or rows UPDATED
    return sqlite_changes($serendipity['dbConn']);
}
 /**
  * Deletes a particular message from the specified catalogue.
  *
  * @param string  $source     the source message to delete.
  * @param string  $catalogue  the catalogue to delete from.
  * @return boolean true if deleted, false otherwise. 
  */
 function delete($message, $catalogue = 'messages')
 {
     $details = $this->getCatalogueDetails($catalogue);
     if ($details) {
         list($cat_id, $variant, $count) = $details;
     } else {
         return false;
     }
     $db = sqlite_open($this->source);
     $text = sqlite_escape_string($message);
     sqlite_query("DELETE FROM trans_unit WHERE cat_id = {$cat_id} AND source = '{$message}'", $db);
     if (sqlite_changes($db)) {
         $this->updateCatalogueTime($cat_id, $variant, $db);
         $deleted = true;
     } else {
         $deleted = false;
     }
     sqlite_close($db);
     return $deleted;
 }