/** * create or connect to the specified database. * * @param array $dsn the data source name * @param bool $persitent if the connection is persitent * * @return DB_OK on success or DB_error object on failure */ function connect($dsn, $persitent = false) { $this->connection = sqlite3_open($dsn['database']); if (!$this->connection) { return $this->raiseError(DB_ERROR_NODBSELECTED); } return DB_OK; }
/** open connection to database */ function open() { //prevent multiple db open if ($this->db) { return $this->db; } if (!$this->db_file) { return FALSE; } if (!(is_file($this->db_file) || $this->autocreate)) { return FALSE; } if ($this->db = sqlite3_open($this->db_file)) { return $this->db; } else { $this->set_error(__FUNCTION__); return FALSE; } }
function connect($db = null) { switch ($this->sql) { case "sqlite": $this->link = sqlite_open($this->host); break; case "sqlite3": $this->link = sqlite3_open($this->host); break; case "mysql": $this->link = mysql_connect($this->host, $this->user, $this->password, true); if (isset($db)) { $this->query("USE " . $db . ";"); } break; default: $this->error($this->sql . " is not supported yet", "connect"); return false; break; } }
function DBconnect(&$error) { $result = true; global $DB; $DB['DB'] = null; $DB['TRANSACTIONS'] = 0; //Stats $DB['SELECT_COUNT'] = 0; $DB['EXECUTE_COUNT'] = 0; //SDI('type: '.$DB['TYPE'].'; server: '.$DB['SERVER'].'; port: '.$DB['PORT'].'; db: '.$DB['DATABASE'].'; usr: '******'USER'].'; pass: '******'PASSWORD']); if (!isset($DB['TYPE'])) { $error = "Unknown database type."; $result = false; } else { switch ($DB['TYPE']) { case 'MYSQL': $mysql_server = $DB['SERVER'] . (!empty($DB['PORT']) ? ':' . $DB['PORT'] : ''); if (!($DB['DB'] = mysql_pconnect($mysql_server, $DB['USER'], $DB['PASSWORD']))) { $error = 'Error connecting to database [' . mysql_error() . ']'; $result = false; } else { if (!mysql_select_db($DB['DATABASE'])) { $error = 'Error database selection [' . mysql_error() . ']'; $result = false; } } break; case 'POSTGRESQL': $pg_connection_string = (!empty($DB['SERVER']) ? 'host=\'' . $DB['SERVER'] . '\' ' : '') . 'dbname=\'' . $DB['DATABASE'] . '\' ' . (!empty($DB['USER']) ? 'user=\'' . $DB['USER'] . '\' ' : '') . (!empty($DB['PASSWORD']) ? 'password=\'' . $DB['PASSWORD'] . '\' ' : '') . (!empty($DB['PORT']) ? 'port=' . $DB['PORT'] : ''); $DB['DB'] = pg_connect($pg_connection_string); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } break; case 'ORACLE': $DB['DB'] = ociplogon($DB['USER'], $DB['PASSWORD'], $DB['DATABASE']); // $DB['DB']= ociplogon($DB['USER'], $DB['PASSWORD'], '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST='.$DB['SERVER'].')(PORT=1521))(CONNECT_DATA=(SERVICE_NAME='.$DB['DATABASE'].')))'); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } break; case 'SQLITE3': $DB['TRANSACTIONS'] = 0; if (!function_exists('init_db_access')) { function init_db_access() { global $DB, $ZBX_SEM_ID; $ZBX_SEM_ID = false; if (function_exists('ftok') && function_exists('sem_get') && file_exists($DB['DATABASE'])) { $ZBX_SEM_ID = sem_get(ftok($DB['DATABASE'], 'z'), 1); } } } if (!function_exists('lock_db_access')) { function lock_db_access() { global $ZBX_SEM_ID; if ($ZBX_SEM_ID && function_exists('sem_acquire')) { sem_acquire($ZBX_SEM_ID); } } } if (!function_exists('unlock_db_access')) { function unlock_db_access() { global $ZBX_SEM_ID; if ($ZBX_SEM_ID && function_exists('sem_release')) { sem_release($ZBX_SEM_ID); } } } if (!function_exists('free_db_access')) { function free_db_access() { global $ZBX_SEM_ID; if ($ZBX_SEM_ID && function_exists('sem_remove')) { sem_remove($ZBX_SEM_ID); } $ZBX_SEM_ID = false; } } if (file_exists($DB['DATABASE'])) { $DB['DB'] = sqlite3_open($DB['DATABASE']); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } } else { $error = 'Missed database'; $result = false; } init_db_access(); break; default: $error = 'Unsupported database'; $result = false; } } if (false == $result) { $DB['DB'] = null; } return $result; }
function sql_database_connect() { // Connect to 'the database' - whatever it may be. Return a handle to use. // Determine DB type if ($this->dbtype == 'mysql') { $dbhandle = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass); if (!$dbhandle) { $this->errstr = 'SEVERE: AGI could not connect to MySQL Database. ' . mysql_error(); debug($this->errstr, 1); return null; } $this->debug("Connected to MySQL database OK.", 4); $selected = mysql_select_db($this->dbname, $dbhandle); if (!$selected) { $this->errstr = 'SEVERE: AGI could not select MySQL Database "' . $this->dbname . '" - ' . mysql_error(); $this->debug($this->errstr, 1); return null; } $this->debug("Selected database OK.", 4); $this->errstr = null; $this->db = "mysql"; return $dbhandle; } elseif ($this->dbtype == 'sqlite3') { // Database is SQLite. It's preferrable to use the inbuilt PHP5 sqlite3 class, so // check if that exist first. Requires PHP 5.3.0 if (class_exists('SQLite3')) { $dbhandle = new SQLite3($this->dbfile, SQLITE3_OPEN_READWRITE); if (!$dbhandle) { $this->errstr = 'SEVERE: AGI could not connect to (native) SQLite Database "' . $this->dbfile . '" - ' . $dbhandle->lastErrorMsg; $this->debug($this->errstr, 1); return null; } $this->debug("Connected to SQLite database OK (native sqlite).", 4); $this->errstr = null; $this->db = "sqlite"; return $dbhandle; // Bugger. OK, We'll have to use php-sqlite3 then. If the module is already loaded, or is // compiled in, we'll already have sqlite3_ commands. } elseif (!function_exists('sqlite3_open')) { $this->debug('Loading sqlite3.so', 4); // It's not loaded. Load it. // dl() is gone in php5, but since this will crash it anyhow, will just leave it as is dl('sqlite3.so'); // That would have crashed PHP if it couldn't load it, so we know it's loaded if // it got to here. $this->debug('Loaded', 4); } // We now have sqlite3_ functions. Use them! $dbhandle = sqlite3_open($this->dbfile); if (!$dbhandle) { $this->errstr = 'SEVERE: AGI could not connect to (module) SQLite3 Database "' . $this->dbfile . '" - ' . sqlite3_error($dbhandle); $this->debug($this->errstr, 1); return null; } $this->debug("Connected to SQLite3 database OK (module sqlite3).", 4); $this->errstr = null; $this->db = "sqlite3"; return $dbhandle; } else { $this->errstr = 'SEVERE: Unknown database type: "' . $this->dbtype . '"'; $this->debug($this->errstr, 1); return null; } }
/* * you may need this line, depending on how you compile * this extension. */ print "<html>\n"; if (!extension_loaded('sqlite3')) { print "Loading sqlite3.so....<br>"; dl('sqlite3.so'); } /* * create a SQLite3 handle. * * Note: in-memory database are created by the magic keyword ":memory:" * */ $db = sqlite3_open("/tmp/test.db"); if (!$db) { 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)");
/** * Try to connect to the configured Database (during installation) * * @access public * @param array input configuration array, holding the connection info * @param array referenced array which holds the errors that might be encountered * @return boolean return true on success, false on error */ function serendipity_db_probe($hash, &$errs) { global $serendipity; $dbName = isset($hash['sqlitedbName']) ? $hash['sqlitedbName'] : $hash['dbName']; if (!function_exists('sqlite3_open')) { $errs[] = 'SQLite extension not installed. Run "pear install sqlite" on your webserver or contact your systems administrator regarding this problem.'; return false; } if (defined('S9Y_DATA_PATH')) { // Shared installations! $dbfile = S9Y_DATA_PATH . $dbName . '.db'; } else { $dbfile = $serendipity['serendipityPath'] . $dbName . '.db'; } $serendipity['dbConn'] = sqlite3_open($dbfile); if ($serendipity['dbConn']) { return true; } $errs[] = "Unable to open \"{$dbfile}\" - check permissions (directory needs to be writeable for webserver)!"; return false; }
<?php /* * you may need this line, depending on how you compile * this extension. */ dl('sqlite3.so'); /* * 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)"); echo "last rowid inserted : " . sqlite3_last_insert_rowid($db) . "\n"; sqlite3_exec($db, "INSERT INTO test (name,age) VALUES ('bob',27)"); echo "last rowid inserted : " . sqlite3_last_insert_rowid($db) . "\n"; sqlite3_exec($db, "INSERT INTO test (name,age) VALUES ('martin',12)"); echo "last rowid inserted : " . sqlite3_last_insert_rowid($db) . "\n"; sqlite3_close($db);
function sqlite3_command($query, $db) { $han = sqlite3_open($db); $aa = sqlite3_query($han, $query); if (!$aa) { echo "SQLITE3 QUERY {$query} returned error: " . sqlite3_error($han); } return $aa; }
function DBconnect(&$error) { $result = true; global $DB; $DB['DB'] = null; $DB['TRANSACTIONS'] = 0; //Stats $DB['SELECT_COUNT'] = 0; $DB['EXECUTE_COUNT'] = 0; //SDI('type: '.$DB['TYPE'].'; server: '.$DB['SERVER'].'; port: '.$DB['PORT'].'; db: '.$DB['DATABASE'].'; usr: '******'USER'].'; pass: '******'PASSWORD']); if (!isset($DB['TYPE'])) { $error = "Unknown database type."; $result = false; } else { $DB['TYPE'] = zbx_strtoupper($DB['TYPE']); switch ($DB['TYPE']) { case 'MYSQL': $mysql_server = $DB['SERVER'] . (!empty($DB['PORT']) ? ':' . $DB['PORT'] : ''); if (!($DB['DB'] = mysql_connect($mysql_server, $DB['USER'], $DB['PASSWORD']))) { $error = 'Error connecting to database [' . mysql_error() . ']'; $result = false; } else { if (!mysql_select_db($DB['DATABASE'])) { $error = 'Error database in selection [' . mysql_error() . ']'; $result = false; } else { DBexecute('SET NAMES utf8'); DBexecute('SET CHARACTER SET utf8'); } } break; case 'POSTGRESQL': $pg_connection_string = (!empty($DB['SERVER']) ? 'host=\'' . $DB['SERVER'] . '\' ' : '') . 'dbname=\'' . $DB['DATABASE'] . '\' ' . (!empty($DB['USER']) ? 'user=\'' . $DB['USER'] . '\' ' : '') . (!empty($DB['PASSWORD']) ? 'password=\'' . $DB['PASSWORD'] . '\' ' : '') . (!empty($DB['PORT']) ? 'port=' . $DB['PORT'] : ''); $DB['DB'] = pg_connect($pg_connection_string); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } break; case 'ORACLE': $connect = ''; if (!empty($DB['SERVER'])) { $connect = '//' . $DB['SERVER']; if ($DB['PORT'] != '0') { $connect .= ':' . $DB['PORT']; } if ($DB['DATABASE']) { $connect .= '/' . $DB['DATABASE']; } } $DB['DB'] = ociplogon($DB['USER'], $DB['PASSWORD'], $connect); // $DB['DB']= ociplogon($DB['USER'], $DB['PASSWORD'], '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST='.$DB['SERVER'].')(PORT=1521))(CONNECT_DATA=(SERVICE_NAME='.$DB['DATABASE'].')))'); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } break; case 'IBM_DB2': $connect = ''; $connect .= 'DATABASE=' . $DB['DATABASE'] . ';'; $connect .= 'HOSTNAME=' . $DB['SERVER'] . ';'; $connect .= 'PORT=' . $DB['PORT'] . ';'; $connect .= 'PROTOCOL=TCPIP;'; $connect .= 'UID=' . $DB['USER'] . ';'; $connect .= 'PWD=' . $DB['PASSWORD'] . ';'; $DB['DB'] = db2_connect($connect, $DB['USER'], $DB['PASSWORD']); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } else { $options = array('db2_attr_case' => DB2_CASE_LOWER); db2_set_option($DB['DB'], $options, 1); if (isset($DB['SCHEMA']) && $DB['SCHEMA'] != '') { DBexecute("SET CURRENT SCHEMA='" . $DB['SCHEMA'] . "'"); } } break; case 'SQLITE3': $DB['TRANSACTIONS'] = 0; if (!function_exists('init_db_access')) { function init_db_access() { global $DB, $ZBX_SEM_ID; $ZBX_SEM_ID = false; if (function_exists('ftok') && function_exists('sem_get') && file_exists($DB['DATABASE'])) { $ZBX_SEM_ID = sem_get(ftok($DB['DATABASE'], 'z'), 1); } } } if (!function_exists('lock_db_access')) { function lock_db_access() { global $ZBX_SEM_ID; if ($ZBX_SEM_ID && function_exists('sem_acquire')) { sem_acquire($ZBX_SEM_ID); } } } if (!function_exists('unlock_db_access')) { function unlock_db_access() { global $ZBX_SEM_ID; if ($ZBX_SEM_ID && function_exists('sem_release')) { sem_release($ZBX_SEM_ID); } } } if (!function_exists('free_db_access')) { function free_db_access() { global $ZBX_SEM_ID; if ($ZBX_SEM_ID && function_exists('sem_remove')) { sem_remove($ZBX_SEM_ID); } $ZBX_SEM_ID = false; } } if (file_exists($DB['DATABASE'])) { $DB['DB'] = sqlite3_open($DB['DATABASE']); if (!$DB['DB']) { $error = 'Error connecting to database'; $result = false; } } else { $error = 'Missing database'; $result = false; } init_db_access(); break; default: $error = 'Unsupported database'; $result = false; } } if (false == $result) { $DB['DB'] = null; } return $result; }
public function open($filename, $flags, $encryption_key) { $this->conn = sqlite3_open($filename); return $this->conn != NULL; }