php pg_exec,在C、JAVA、PHP中操作postgreSql数据库 (转)

在C、JAVA、PHP中操作postgreSql数据库 (转)[@more@]

在C、、中操作postgreaid.com.cn/"> to:eight@linuxaid.com.cn">eight

〖〗〖转发〗

postgresql的好用不亚于,下文介绍了几种工具与postgresql的交互。

1.C操作postgreSql

:

/*程序在6.0上通过

*该程序使用pgsql的内部实现的一般功能

*这里create,insert,,update,drop几个最常用的SQL语句

*具体还有些更强大的功能,如阻塞等,需要进一步研究

*详细资料可以查看参考手册,那边有所有的函数*/

/*头*/

#include #include

main() {

char *p,

*pgport,

*pgoptions,

*pgtty;

char *Name;

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   {

for (j = 0; 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   {

for (j = 0; 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 (),从网上下一个吧,解包后发现,其实就是一个jar文件,将这个jar文件的路径加入到classpath中.注意,我在这上面困了很久.我装的1.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页面的顶端为

";

echo "

";

$rst = pg_exec("select * from test1",$conn)

if (!$rst)

{

echo "Sql错误!.";

exit;

}

$fields_num = pg_num_fields($rst);

$i=0;

while($i   $fields[$i] = pg_field_name($rst,$i);

echo "" . $fields[$i] . "";

$i++;

}

echo "";

while ($record=pg_fetch_array($rst)) {

echo "

";

$i=0;

while($i   $value = $record[$fields[$i]];

if($value=="")

echo "";

else

echo "" . $value . "";

$i++;

}

echo "";

}

pg_free_result($rst);

echo "";

pg_close($conn);

?>

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-985687/,如需转载,请注明出处,否则将追究法律责任。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值