postgresql的好用不亚于mysql,下文介绍了几种编程工具与postgresql的交互。
1.C操作postgreSql 程序: /*程序在redhat6.0上调试通过 *该程序使用pgsql的内部函数实现数据库的一般功能 *这里create,insert,select,update,drop几个最常用的SQL语句 *具体还有些更强大的功能,如阻塞等,需要进一步研究 *详细资料可以查看参考手册,那边有所有的函数调用*/ /*头文件*/ #include <stdio.h> #include main() { char *pghost, *pgport, *pgoptions, *pgtty; char *dbName; int nFields; int i, j; PGconn *conn; PGresult *res; /* * 程序开头需要设定连接到数据库服务器的一些参数,如果设置值为NULL, * 则使用环境变量中设置的缺省值。 */ pghost = NULL; /* 服务器的主机名 */ pgport = NULL; /* 服务器端口 */ pgoptions = NULL;/* 附加的功能参数 */ pgtty = NULL; /* 服务器的调试tty */ dbName = "mytest"; /* 要操作的数据库名 */ /* 连接数据库服务器*/ conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName); /* 检查连接是否成功 */ if (PQstatus(conn) == CONNECTION_BAD) { fprintf(stderr, "Connection to database '%s' failed.", dbName); fprintf(stderr, "%s", PQerrorMessage(conn)); exit_nicely(conn); } /* 开始处理数据块 */ res = PQexec(conn, "BEGIN"); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "BEGIN command failed"); PQclear(res); exit_nicely(conn); } /*调用PQclear在PQresult的游标不再使用后清除游标,防止内存溢出 */ PQclear(res); /* 建立一个叫test1的表 */ PQexec(conn,"create table test1 (name char(20),age int4)"); /* 插入数据 */ PQexec(conn,"insert into test1 values ('cjm',10)"); PQexec(conn,"insert into test1 values ('eight',20)"); PQexec(conn,"insert into test1 values ('linuxaid',30)"); /* 开始查询 */ printf("all the date is:"); res = PQexec(conn, "select * from test1"); for (i = 0; i < PQntuples(res); i++) { for (j = 0; j < PQnfields(res); j++) printf("%-15s", PQgetvalue(res, i, j)); printf(""); } PQclear(res); /* 使用SQL的update函数 */ PQexec(conn,"update test1 set age=25 where name='eight'"); /* 打印出更新后的数据 */ printf(" the new date is:"); res = PQexec(conn, "select * from test1"); for (i = 0; i < PQntuples(res); i++) { for (j = 0; j < PQnfields(res); j++) printf("%-15s", PQgetvalue(res, i, j)); printf(""); } PQclear(res); /* 删除表 */ PQexec(conn,"drop table test1"); /* 关闭和数据库的连接 */ PQfinish(conn); } 在C下操作postGreSql比较简单,Sample很多,下面介绍一下用Java操作postGreSql.
2.JAVA操作postGreSql 需要postgreSQl的java driver(jdbc),从网上下一个吧,解包后发现,其实就是一个jar文件,将这个jar文件的路径加入到classpath中.注意,我在这上面困了很久.我装的jdk1.2使用时不需要classpath,因此开始的时候,我将该jar文件放到jdk的目录下,在makefile文件中指定路径,死活通不过,总是该死的class not found错误,创建classpath环境变量后才得以通过(不要问我为什么我也不知,:-( ); 在用JDBC时要不断的使用try{...}catch(..){..},否则总出错, 下面给一个简单的sample,大家happy一下.程序中用到的数据库非常简单,按以下方法创建就可以了(以postgres用户创建): 1.启动数据库:postmaster -S -i 2.创建其他用户:createuser eight 3.创建数据库:createdb mytest 4.创建table: 首先psql mytest mytest=>create table test1( name char(10), age int(4)); 注意,命令要以;结尾。 mydb=>q 退出psql. import java.lang.*; import java.util.*; import java.sql.*; public class db { public static void main(String argv[]) { System.out.println("start..."); //init database connection Connection pdb; try { Class.forName("postgresql.Driver"); } catch(java.lang.ClassNotFoundException e) { System.out.println("err:class.forname."); } try { pdb=DriverManager.getConnection("jdbc:postgresql:mytest","eight",""); Statement stmt=pdb.createStatement(); ResultSet rs=stmt.executeQuery("select * from test1"); if(rs!=null) { System.out.println("get data from database:"); while(rs.next()) { System.out.print(rs.getString(1)); System.out.print(rs.getint(2)); System.out.print(""); } } rs.close(); stmt.close(); pdb.close(); } catch(Exception e) { e.printStackTrace(); } } }
3.PHP操作postGreSql 使用PHPLIB处理各种数据库的方式都一样,只需要扩充PHPLIB,加入所需要PHPLIB文件既可。PHPLIB通过一个名为DB_Sql的类来操作SQL数据库。在你的代码中包含适合你的数据库的版本。在下面的例子中,我使用postGreSql版本。为了在代码中得到DB_Sql,在PHPLIB要求的目录下安装PHPLIB文件。然后,找到cgi-bin目录,在cgi-bin目录下创建phplib目录。接着,拷贝所有的PHPLIB中的.inc文件到phplib目录下。最后,将phplib目录放在php.ini文件中include_path = 的那行上。include_path是PHP引用在include()或require()中文件名的地方。在每一个PHP页面的顶端为 <?php require(common.php3); ?> common.php3在包含目录中,包含对每一页都通用的所有的数据和函数。在common.php3中,为 <?php require(db_pgsql.inc); require(ct_sql.inc); require(session.inc); require(auth.inc); require(perm.inc); require(user.inc); require(page.inc); ?>
下面是一个操作PostgreSql的例子 <? $conn = pg_connect("","","","","mytest"); if (!$conn) { echo "无法连接数据库."; exit; } echo "<table border="1" cellspacing="0" cellpadding="0">"; echo "<tr>"; $rst = pg_exec("select * from test1",$conn) if (!$rst) { echo "执行Sql错误!."; exit; }
$fields_num = pg_num_fields($rst); $i=0; while($i<$fields_num){ $fields[$i] = pg_field_name($rst,$i); echo "<th>" . $fields[$i] . "</th>"; $i++; } echo "</tr>"; while ($record=pg_fetch_array($rst)) { echo "<tr>"; $i=0; while($i<$fields_num){ $value = $record[$fields[$i]]; if($value=="") echo "<td> </td>"; else echo "<td>" . $value . "</td>"; $i++; } echo "</tr>"; } pg_free_result($rst); echo "</table>"; pg_close($conn); ?>
|