在C、JAVA、PHP中操作postgreSql資料庫 (轉)

worldblog發表於2007-12-07
在C、JAVA、PHP中操作postgreSql資料庫 (轉)[@more@]

在C、、中操作postgre

aid.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頁面的頂端為 
 require(common.php3); 
?> 
common.php3在包含目錄中,包含對每一頁都通用的所有的資料和函式。在common.php3中,為 
 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 "

";
  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 "";
   $i++;
  }
  echo "";
  while ($record=pg_fetch_array($rst)) {
   echo "";
   $i=0;
   while($i   $value = $record[$fields[$i]];
   if($value=="")
   echo "";
   else
   echo "";
   $i++;
   }
   echo "";
  }
  pg_free_result($rst);
  echo "
" . $fields[$i] . "
 " . $value . "
";
  pg_close($conn);
?>


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-989239/,如需轉載,請註明出處,否則將追究法律責任。

相關文章