A summary of several database APIs

Here are some notes for how to use all databases in Java and Perl and how to use a few common ones in PHP.
I've added cross-references to online descriptions of the functions,  because this is just a summary,  and does not pretend to explain all the features of each API.
 
Java JDBC Perl DBI MySQL/PHP MySQL/C Postgres/C Postgres/PHP
Open a connection Class.forName(driver_name);
Connectioncon = DriverManger.getConnection(String url, Properties info);
use DBI;
$dbh = DBI->connect($dsn, $user, $password, { %attr });
$link = mysql_connect( [server [, username [,  password [,  new_link [,  client_flags]]]]]);
mysql_select_db(database_name [,link])
 
MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag) ;
int mysql_select_db(MYSQL *mysql, const char *db) 
PGconn *PQconnectdb(const char *conninfo);
size_t ;
$connection = pg_connect(connection_string);
OR
$connection = pg_pconnect(connection_string);
Execute a non-SELECT query Statement s = con.createStatement();
s.executeUpdate(String sql);
$nrows = $dbh->do($statement, \%attr, @bind_values $result = mysql_query(query, [link]);
$nrows = mysql_affected_rows([link]);
unsigned long mysql_real_escape_string(MYSQL *mysql, char *to, const char *from, unsigned long length) ;
int mysql_query(MYSQL *mysql, const char *query) 
PQescapeString (char *to, const char *from, size_t length);
PGresult *PQexec(PGconn *conn, const char *query);
$result = pg_query(connection, query);
$nrows =  pg_affected_rows(result);
 
Execute a SELECT query Statement s = con.createStatement();
ResultSet r = s.executeQuery(String sql);
OR
ps.setXxx(int, Xxx x); // repeat...
ResultSet r = ps.executeQuery();
$ary_ref = $dbh->selectall_arrayref($statement, \%attr, @bind_values);
OR
$rv = $sth->execute(@bind_values
$result = mysql_query(query, [link]);
 
mysql_query $result = pg_query(connection, query);
Prepare a SELECT query PreparedStatement ps = con.prepareStatement(String sql); $sth = $dbh->prepare($statement) not supported (none before v4.1) not supported
Read a row of results rs.next(); $ary_ref = $sth->fetchrow_arrayref
OR
@ary = $sth->fetchrow_array;
OR
$hash_ref = $sth->fetchrow_hashref;
 OR
$tbl_ary_ref = $sth->fetchall_arrayref;
$row = mysql_fetch_array(result); MYSQL_ROW mysql_fetch_row(MYSQL_RES *result)  $row = pg_fetch_array)
Value of one column in the row of results rs.getXxx(int);
OR
rs.getXxx(String);
$row[column]
OR
$hash{column_name}
$row[column]
OR
$row[column_name]
MYSQL_ROW is equivalent to:
typedef char *MYSQL_ROW[]
$row[column]
OR
$row[column_name]
Cleanup not normally needed not normally needed not normally needed void mysql_free_result(MYSQL_RES *result) ;
void mysql_close(MYSQL *mysql) 
not normally needed

Here's another random idea: I want to see a threaded mailreader that lets me dynamically edit the threads. I've used Opera, Kmail, Mozilla, Pine and Mutt, and none of them is 100% accurate at identifying threads. Part of this is the fault of the people who compose messages to mailing-lists and newsgroups: some people misdirect a reply, or use software that doesn't include a References: header, thus breaking the flow of the thread; other people reply to a message with a new, completely unrelated question just because they're too lazy to type the list's address into their mailer. I wish that I, as someone reading a set of messages, could fix broken or misapplied thread associations when I see them.

See also a comparison of collection classes.



Author:  David Lee Lambert
Index: [.]