歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> 在C、JAVA、PHP中操作postgreSql

在C、JAVA、PHP中操作postgreSql

日期:2017/2/27 14:19:36   编辑:更多Linux
  postgresql的好用不亞於mysql,下文介紹了幾種編程工具與postgresql的交互。 1.C操作postgreSql 程序: /*程序在redhat6.0上調試通過 *該程序使用pgsql的內部函數實現數據庫的一般功能 *這裡create,insert,select,update,drop幾個最常用的SQL語句 *具體還有些更強大的功能,如阻塞等,需要進一步研究 *詳細資料可以查看參考手冊,那邊有所有的函數調用*/ /*頭文件*/ #include #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頁面的頂端為

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



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



Copyright © Linux教程網 All Rights Reserved