function DBexecute($query, $skip_error_messages = 0) { global $DB; //COpt::savesqlrequest($query); $result = false; if (isset($DB['DB']) && !empty($DB['DB'])) { $DB['EXECUTE_COUNT']++; // WRONG FOR ORACLE!! //SDI('SQL Exec: '.$query); switch ($DB['TYPE']) { case 'MYSQL': $result = mysql_query($query, $DB['DB']); if (!$result) { error('Error in query [' . $query . '] [' . mysql_error() . ']'); } break; case 'POSTGRESQL': if (!($result = pg_query($DB['DB'], $query))) { error('Error in query [' . $query . '] [' . pg_last_error() . ']'); } break; case 'ORACLE': $stid = OCIParse($DB['DB'], $query); if (!$stid) { $e = @ocierror(); error('SQL error [' . $e['message'] . '] in [' . $e['sqltext'] . ']'); } $result = @OCIExecute($stid, $DB['TRANSACTIONS'] ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS); if (!$result) { $e = ocierror($stid); error('SQL error [' . $e['message'] . '] in [' . $e['sqltext'] . ']'); } else { $result = $stid; } break; case 'SQLITE3': if (!$DB['TRANSACTIONS']) { lock_db_access(); } $result = sqlite3_exec($DB['DB'], $query); if (!$result) { error('Error in query [' . $query . '] [' . sqlite3_error($DB['DB']) . ']'); } if (!$DB['TRANSACTIONS']) { unlock_db_access(); } break; } if ($DB['TRANSACTIONS'] && !$result) { $DB['TRANSACTION_STATE'] &= $result; // SDI($query); // SDI($DB['TRANSACTION_STATE']); } } return $result; }
/** * execute a SQL query. * * @param string $query the SQL query * * @return mixed + object DB_error object on failure * + object Result resource for SELECT requests * + bool TRUE for other sucessful requests */ function simpleQuery($query) { $isManip = preg_match('/^\\s*"?(SELECT\\s+load_extension\\(|BEGIN[;\\s])/i', $query) || DB::isManip($query); $retryMicroseconds = 300000; $retryMore = 1.5; $retryNb = 10; for ($i = 0; $i <= $retryNb; $i++) { if ($isManip) { $this->result = sqlite3_exec($this->connection, $query); } else { $this->result = sqlite3_query($this->connection, $query); } if ($this->result) { return $this->result; } $errorNative = $this->errorNative(); if ($errorNative == 'database is locked' && $i < $retryNb) { //error_log( 'SFN - database is locked, waiting ' . ( $retryMicroseconds / 1000 ) . ' ms and retrying...' ); usleep($retryMicroseconds); $retryMicroseconds = floor($retryMicroseconds * $retryMore); } else { break; } } return $this->RaiseError($errorNative); }
/** * perform a query on the database * @param string $Q_str * @return result id or bool depend on the query type| FALSE */ function query($Q_str) { if (!$this->db) { if (!(db::$autoconnect && $this->open())) { return FALSE; } } $this->verbose($Q_str, __FUNCTION__, 2); if ($this->last_qres) { #- close unclosed previous qres sqlite3_query_close($this->last_qres); $this->last_qres = null; } if (preg_match('!^\\s*select!i', $Q_str)) { $this->last_qres = sqlite3_query($this->db, $Q_str); $res = $this->last_qres; } else { $res = sqlite3_exec($this->db, $Q_str); } if (!$res) { $this->set_error(__FUNCTION__); } return $res; }
die("Could not create in-memory database.."); } /* * create a simple test and insert some values.. */ print "Creating table:<br>\n"; $ret = sqlite3_exec($db, "CREATE TABLE test (id INTEGER, name TEXT, age INTEGER);"); if (!$ret) { print "\ttable already exists<br>\n"; } else { print "\ttable created<br>\n"; } print "Inserting values:<br>\n"; sqlite3_exec($db, "INSERT INTO test (id,name,age) VALUES (1,'michael',32)"); sqlite3_exec($db, "INSERT INTO test (id,name,age) VALUES (2,'bob',27)"); sqlite3_exec($db, "INSERT INTO test (id,name,age) VALUES (3,'martin',12)"); /* * Create a query */ print "SQL query:<br>\n"; $query = sqlite3_query($db, "SELECT * FROM test ORDER BY age DESC"); if (!$query) { die(sqlite3_error($db)); } /* * sqlite3_fetch_array() returns an associative array * for each row in the result set. Key indexes are * the columns names. * */ while ($row = sqlite3_fetch_array($query)) {
* this extension. */ dl('sqlite3.so'); /* column type<=>name map */ $col_types = array(SQLITE3_INTEGER => "integer", SQLITE3_FLOAT => "float", SQLITE3_TEXT => "text", SQLITE3_BLOB => "blob", SQLITE3_NULL => "null"); /* * create a SQLite3 handle. * * Note: in-memory database are created by the magic keyword ":memory:" * */ $db = sqlite3_open(":memory:"); if (!$db) { die("Could not create in-memory database.."); } if (!sqlite3_exec($db, "create table test (a int, b text, c double, d blob)")) { die(sqlite3_error($db)); } $res = sqlite3_query($db, "insert into test (a,b,c, d) VALUES (?, ?, ?, ?)"); if (!$res) { die(sqlite3_error($db)); } if (!sqlite3_bind_int($res, 1, 10)) { die(sqlite3_error($db)); } if (!sqlite3_bind_text($res, 2, "bob")) { die(sqlite3_error($db)); } if (!sqlite3_bind_double($res, 3, 3.1415)) { die(sqlite3_error($db)); }
/** * execute a SQL query. * * @param string $query the SQL query * * @return mixed + object DB_error object on failure * + object Result resource for SELECT requests * + bool TRUE for other sucessful requests */ function simpleQuery($query) { $isSelect = preg_match("/^\\s*SELECT/i", $query); if (!$isSelect) { $this->result = sqlite3_exec($this->connection, $query); } else { $this->result = sqlite3_query($this->connection, $query); } if (!$this->result) { return $this->RaiseError($this->errorNative()); } return $this->result; }
/** * execute a SQL query. * * @param string $query the SQL query * * @return mixed + object DB_error object on failure * + object Result resource for SELECT requests * + bool TRUE for other sucessful requests */ function simpleQuery($query) { $isSelect = preg_match("/^\\s*SELECT/i", $query); #$handle = fopen("/tmp/sql.txt",'a'); #fwrite($handle, "<strong>sqlite3 simpleQuery: $query</strong><br>\n"); #fclose($handle); if (!$isSelect) { $this->result = sqlite3_exec($this->connection, $query); } else { $this->result = sqlite3_query($this->connection, $query); } if (!$this->result) { return $this->RaiseError($this->errorNative()); } return $this->result; }
function DBexecute($query, $skip_error_messages = 0) { global $DB; $result = false; $time_start = microtime(true); if (isset($DB['DB']) && !empty($DB['DB'])) { $DB['EXECUTE_COUNT']++; //SDI('SQL xec: '.$query); switch ($DB['TYPE']) { case 'MYSQL': $result = mysql_query($query, $DB['DB']); if (!$result) { error('Error in query [' . $query . '] [' . mysql_error() . ']'); } break; case 'POSTGRESQL': $result = (bool) pg_query($DB['DB'], $query); if (!$result) { error('Error in query [' . $query . '] [' . pg_last_error() . ']'); } break; case 'ORACLE': $result = OCIParse($DB['DB'], $query); if (!$result) { $e = @ocierror(); error('SQL error [' . $e['message'] . '] in [' . $e['sqltext'] . ']'); } else { if (!@OCIExecute($result, $DB['TRANSACTIONS'] ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS)) { $e = ocierror($result); error('SQL error [' . $e['message'] . '] in [' . $e['sqltext'] . ']'); } else { /* It should be here. The function must return boolean */ $result = true; } } break; case 'IBM_DB2': $options = array(); if (!($result = db2_prepare($DB['DB'], $query))) { $e = @db2_stmt_errormsg($result); error('SQL error [' . $query . '] in [' . $e . ']'); } else { if (true !== @db2_execute($result)) { $e = @db2_stmt_errormsg($result); error('SQL error [' . $query . '] in [' . $e . ']'); } else { /* It should be here. The function must return boolean */ $result = true; } } break; case 'SQLITE3': if (!$DB['TRANSACTIONS']) { lock_db_access(); } $result = sqlite3_exec($DB['DB'], $query); if (!$result) { error('Error in query [' . $query . '] [' . sqlite3_error($DB['DB']) . ']'); } if (!$DB['TRANSACTIONS']) { unlock_db_access(); } break; } if ($DB['TRANSACTIONS'] && !$result) { $DB['TRANSACTION_STATE'] &= $result; } } COpt::savesqlrequest(microtime(true) - $time_start, $query); return (bool) $result; }
echo "my_callback called !\n"; echo "1st argument: " . implode($data, ',') . "\n"; echo "2nd argument: " . implode($columns, ',') . "\n"; return 0; } /* * create a SQLite3 handle. * * Note: in-memory database are created by the magic keyword ":memory:" * */ $db = sqlite3_open(":memory:"); if (!$db) { die("Could not create in-memory database.."); } /* * create a simple test and insert some values.. */ $ret = sqlite3_exec($db, "CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER);"); if (!$ret) { die(sqlite3_error($db)); } sqlite3_exec($db, "INSERT INTO test (name,age) VALUES ('michael',32)"); sqlite3_exec($db, "INSERT INTO test (name,age) VALUES ('bob',27)"); sqlite3_exec($db, "INSERT INTO test (name,age) VALUES ('martin',12)"); /* * Note the 3rd argument to this function ... * */ $res = sqlite3_exec($db, "SELECT * from test ORDER BY name", "my_callback"); sqlite3_close($db);
public function exec($query) { return sqlite3_exec($this->conn, $query); }
/** * execute a SQL query. * * @param string $query the SQL query * * @return mixed + object DB_error object on failure * + object Result resource for SELECT requests * + bool TRUE for other sucessful requests */ function simpleQuery($query) { $isManip = DB::isManip($query); if ($isManip) { $this->result = sqlite3_exec($this->connection, $query); } else { $this->result = sqlite3_query($this->connection, $query); } if (!$this->result) { return $this->RaiseError($this->errorNative()); } return $this->result; }
function sql($command, $type = "BLANK", $override = false) { $this->debug("Running SQL Command {$command}", 4); // Ensure we're connected to the database. if ($this->dbhandle == null) { if (!($this->dbhandle = $this->sql_database_connect())) { $this->debug('SEVERE: Unable to connect to database.', 1); return false; } } // Check for non-portable stuff. if ($override != true) { $result = $this->sql_check($command); // sql_check returns a sanitized SQL command, or false if error. if ($result == false) { return false; } } else { $result = $command; } // Check the TYPE switch ($type) { case "BLANK": $this->debug("WARNING: Please provide the type of the query for SQL command '{$result}'. Defaulting to BOTH", 2); $type = "BOTH"; break; case "ASSOC": case "NUM": case "BOTH": case "NONE": break; default: $this->errstr = "SEVERE: Uknown Query type '{$type}' for query '{$result}'"; $this->debug($this->errstr, 1); return false; } // Actually do the SQL $sqlresult = null; switch ($this->db) { case "mysql": $res = mysql_query($result, $this->dbhandle); if (!$res) { $this->errstr = "MySQL Error: " . mysql_error() . " with query {$result}"; $this->debug($this->errstr, 1); return false; } // Loop through the returned result set, loading it into the array to return // to the caller. $this->numrows = mysql_num_rows($res); // Return the correct type. if ($type == "NONE") { return true; } for ($i = 0; $i <= $this->numrows; $i++) { if ($type == "NUM") { $sqlresult[$i] = mysql_fetch_array($res, MYSQL_NUM); } elseif ($type = "ASSOC") { $sqlresult[$i] = mysql_fetch_array($res, MYSQL_ASSOC); } else { $sqlresult[$i] = mysql_fetch_array($res, MYSQL_BOTH); } } return $sqlresult; case "sqlite": $res = $this->dbhandle->query($result); if (!$res) { $this->errstr = "SQLite3 Error: '" . $this->dbhandle->lastErrorMsg . "' with query '{$result}'"; $this->debug($this->errstr, 1); return false; } // Loop through the returned result set, loading it into the array to return // to the caller. // Return the correct type. if ($type == "NONE") { return true; } $i = 0; if ($type == "NUM") { while ($sqlresult[$i++] = $res->fetchArray(SQLITE3_NUM)) { } } elseif ($type = "ASSOC") { while ($sqlresult[$i++] = $res->fetchArray(SQLITE3_ASSOC)) { } } else { while ($sqlresult[$i++] = $res->fetchArray(SQLITE3_BOTH)) { } } $res->finalize(); $this->numrows = $i; return $sqlresult; case "sqlite3": // Init the sqlite3 hack variables. global $sql3holderAssoc; global $sql3holderNum; global $sql3holderRowNbr; $sql3holderAssoc = null; $sql3holderNum = null; $sql3holderRowNbr = 0; // If no result is required, just run the query and return the status. if ($type == "NONE") { $res = sqlite3_exec($this->dbhandle, $result); if (!$res) { $this->errstr = sqlite3_error($this->dbhandle); return false; } else { $this->errstr = null; return $res; } } // This next line uses the sqlite3_hack function, below, to load // up the $sql3holder variables. $res = sqlite3_exec($this->dbhandle, $result, "sqlite3_hack"); $this->numrows = $sql3holderRowNbr; $this->debug("SQL returned {$sql3holderRowNbr} Rows", 4); if ($sql3holderRowNbr == 0) { return true; } if ($type == "NUM") { return $sql3holderNum; } elseif ($type == "ASSOC") { return $sql3holderAssoc; } else { return $sql3holderNum + $sql3holderAssoc; } default: $this->debug("SEVERE: Database type '" . $this->db . "' NOT SUPPORTED (sql)", 0); return false; } }