在C、JAVA、PHP中操作postgreSql資料庫 (轉)
在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 "" . $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);
?>
?php>?php>
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-989239/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- postgresql 資料庫基本操作SQL資料庫
- PHP操作MySQL資料庫PHPMySql資料庫
- C# 操作 access 資料庫C#資料庫
- php簡單操作mysql資料庫的類PHPMySql資料庫
- PHP中CakePHP新增資料庫PHP資料庫
- 阿里雲IoT流轉到postgresql資料庫方案阿里SQL資料庫
- Oracle資料庫日期格式轉換操作Oracle資料庫
- 在 k8S 中搭建 SonarQube 7.4.9 版本(使用 PostgreSQL 資料庫)K8SSQL資料庫
- 在Java中地域分佈資料庫是如何連線和進行CRUD 操作的?Java資料庫
- C# 左移右移在資料型別轉換中的使用C#資料型別
- Python使用psycopg2三方庫操作PostgreSQL的資料PythonSQL
- 在.NetCore(C#)中使用ODP.NET Core+Dapper操作Oracle資料庫NetCoreC#APPOracle資料庫
- postgresql資料庫利用方式SQL資料庫
- PostgreSQL-資料庫命令SQL資料庫
- postgresql 資料庫基本管理SQL資料庫
- 資料庫_SQL-PostgreSQL資料庫SQL
- PHP中的資料庫連線方法PHP資料庫
- C#快速搭建模型資料庫SQLite操作C#模型資料庫SQLite
- 資料庫操作資料庫
- 資料庫操作·資料庫
- java mysql 資料庫備份和還原操作JavaMySql資料庫
- Rust 連線 PostgreSQL 資料庫RustSQL資料庫
- PostgreSQL:資料庫的選擇SQL資料庫
- PostgreSQL 資料庫學習 - 0. 資料庫安裝SQL資料庫
- 在MongoDB資料庫中查詢資料(上)MongoDB資料庫
- 資料庫在資料分析中如何應用資料庫
- 【Falsk 使用資料庫】---- 資料庫基本操作資料庫
- 在玩客雲上透過docker部署zabbix(PostgreSQL資料庫)DockerSQL資料庫
- MySQL 資料庫操作MySql資料庫
- mongodb資料庫操作MongoDB資料庫
- 資料庫基本操作資料庫
- MongoDB 資料庫操作MongoDB資料庫
- laravel 資料庫操作Laravel資料庫
- 在Java中利用動態代理實現資料庫連線與事務的自動管理【轉】Java資料庫
- Java技術在多資料庫系統中的應用研究Java資料庫
- Postgresql10資料庫之更改資料庫的名稱SQL資料庫
- 怎樣在QueryBuilder中使用PostgreSQL中的?操作符UISQL
- python 操作 PostgreSQL 資料庫,執行緒並行修改 5w 條資料,效能優化PythonSQL資料庫執行緒並行優化
- PostgreSQL 資料庫中 DISTINCT 關鍵字的 4 種用法SQL資料庫