WEB开发网
开发学院数据库Sybase Sybase SQL Studio 6.0.3程序员指南 阅读

Sybase SQL Studio 6.0.3程序员指南

 2007-06-06 15:07:51 来源:WEB开发网   
核心提示:SQLC编程-将SQL嵌入C语言 大多数数据库管理系统引擎现在都支持C和C++代码中嵌入SQL查询,Sybase使它更简单,Sybase SQL Studio 6.0.3程序员指南(2),不幸的是,提供的例子不必兜圈子,有希望的是,Sybase将不久支持这些,也不必为各种的操作系统包含一个条件章节的大全,我开始便写设计

SQLC编程-将SQL嵌入C语言 大多数数据库管理系统引擎现在都支持C和C++代码中嵌入SQL查询。Sybase使它更简单。不幸的是,提供的例子不必兜圈子,也不必为各种的操作系统包含一个条件章节的大全。我开始便写设计运行在Linux上一个更直接例子,你将在它下面找到:

#include
#include
/* Set up the SQL Communication Area*/
EXEC SQL INCLUDE SQLCA;
#include "sqldef.h"

/* Set up a struct set up to collect query results */
typedef struct employee_t {
unsigned long emp_id;
char name[ 41 ];
char sex[ 2 ];
char birthdate[ 15 ];
} employee_t;

/* Print out the various fields of the employee struct */
static void print_employee(employee_t emp)
{
printf("(%li, %s, %s, %s)\n", emp.emp_id, emp.name, emp.sex, emp.birthdate);
}

/* Declare the Cursor. This must precede all cursor operations in
the source code
*/
EXEC SQL DECLARE C1 CURSOR FOR
SELECT emp_id, emp_fname || ' ' || emp_lname, sex, birth_date
FROM "dba".employee;

/* Fetch a single row and advance the cursor */
static int fetch_row(
EXEC SQL BEGIN DECLARE SECTION;
unsigned long *emp_id,
char *name,
char *sex,
char *birthdate
EXEC SQL END DECLARE SECTION;
)
{
EXEC SQL FETCH RELATIVE 1 C1
INTO :emp_id, :name, :sex, :birthdate;

/* SQLCODE is 0 while there are no errors and rows remaining */
return !SQLCODE;
}

/* A basic SQL error handler */
EXEC SQL WHENEVER SQLERROR {
char buffer[ 200 ];
printf("SQL error: %s\n", sqlerror_message(&sqlca, buffer, sizeof(buffer)));
return(0);
};

/* The heart of the matter */
int main( int argc, char *argv[] )
{
employee_t current_employee;

/* Initialize the Sybase client run-time */
if(!db_init(&sqlca))

{
printf("Unable to initialize database interface");
return(-1);
}

/* Locate the running server engine */
if(!db_find_engine(&sqlca, NULL))
{
printf("Database Engine not running");
db_fini( &sqlca );
return(-1);
}

/* Connect to the engine using user/password "DBA"/"SQL" */
EXEC SQL CONNECT "DBA" IDENTIFIED BY "SQL";

/* Open (initialize) the cursor */
EXEC SQL OPEN C1;

/* Iterate over all the rows, retrieve the data and print them out */
while (fetch_row(¤t_employee.emp_id,
current_employee.name,
current_employee.sex,
current_employee.birthdate
))
{
print_employee(current_employee);
}

/* We're done with the cursor */
EXEC SQL CLOSE C1;
return 0;
}

如果你对SQLC还不熟悉,你要注意的第一事情是散布在代码中以EXEC SQL开始的非C语言语句,这些是特殊的指令,SQLC预处理器知道是静态语句(static statements),这个预处理器将这些语句转换为正确的C代码,他们引用DBMS库函数以便在数据库引擎上执行命令。因为他们是硬编码(hard-coding)到结果目表代码,因此他们是静态语句。

SQLC功能强大的部分是特殊声明的C变量,它能直接被嵌入到SQL语句以接受结果,或把变量传递给运行时刻(runtime)询问。这样的变量被看作主机变量(host variables),在上面例子中在fetch_rows函数重使用主机变量将查询结果从查询传递给标准C变量。

EXEC SQL FETCH RELATIVE 1 C1
INTO :emp_id, :name, :sex, :birthdate;

emp_id、name、sex和birthdate是基本C指针,包含查询返回值。这种情况下,查询准备从以前用下列语句设置的光标中获取下一行值。

EXEC SQL DECLARE C1 CURSOR FOR
SELECT emp_id, emp_fname || ' ' || emp_lname, sex, birth_date
FROM "dba".employee;

表达式emp_fname || ' ' || emp_lname连接两个字符类型的列emp_fname和emp_lname,变成一个字符串值,从查询返回值。

为了使这个例子工作,拷贝例子源码到一个文件listing1.sqc中,并且运行SQLC预处理器:

$ sqlpp listing1.sqc

为了使这个例子工作,拷贝例子源码到一个文件listing1.sqc中,并且运行SQLC预处理器:

$ sqlpp listing1.sqc

它输出一个C文件listing1.sqc,你必须编译它,特别小心地指定相关的库和包括文件目录:

$ cc -Wall -I$SYBASE/include/ -L$SYBASE/lib/ -ldblib6 -ldbtools6 -o listing1 listing1.c

产生一个可执行文件listing1。在运行它之前,保证有一台引擎运行范例数据库:

$ dbeng6 asademo.db

它的输出应该是这样的:

$ ./listing1
(102, Fran Whitney, F, 1958-06-05)
(105, Matthew Cobb, M, 1960-12-04)
(129, Philip Chin, M, 1966-10-30)
(148, Julie Jordan, F, 1951-12-13)
(160, Robert Breault, M, 1947-05-13)
(184, Melissa Espinoza, F, 1939-12-14)
(191, Jeannette Bertrand, F, 1964-12-21)
(195, Marc Dill, M, 1963-07-19)
(207, Jane Francis, F, 1954-09-12)
(243, Natasha Shishov, F, 1949-04-22)
(247, Kurt Driscoll, M, 1955-03-05)
(249, Rodrigo Guevara, M, 1956-11-23)
(266, Ram Gowda, M, 1947-10-18)
(278, Terry Melkisetian, F, 1966-05-17)
(299, Rollin Overbey, M, 1964-03-15)
(316, Lynn Pastor, F, 1962-07-14)
(318, John Crow, M, 1962-04-24)
(390, Jo Ann Davidson, F, 1957-02-17)
(409, Bruce Weaver, M, 1946-04-05)
(445, Kim Lull, M, 1955-01-19)
(453, Andrew Rabkin, M, 1957-08-10)
(467, James Klobucher, M, 1952-11-09)
(479, Linda Siperstein, F, 1967-09-21)
(501, David Scott, M, 1947-03-01)
(529, Dorothy Sullivan, F, 1950-04-19)
(582, Peter Samuels

, M, 1968-02-28)
(586, James Coleman, M, 1966-03-04)
(591, Irene Barletta, F, 1957-01-30)
(604, Albert Wang, M, 1958-12-25)
(641, Thomas Powell, M, 1951-10-31)
(667, Mary Garcia, F, 1963-01-23)
(690, Kathleen Poitras, F, 1965-09-29)
(703, Jose Martinez, M, 1953-07-22)
(750, Jane Braun, F, 1939-08-09)
(757, Denis Higgins, F, 1968-05-12)
(839, Dean Marshall, M, 1966-05-21)
(856, Samuel Singer, M, 1959-04-07)
(862, John Sheffield, M, 1955-09-25)
(868, Felicia Kuo, F, 1968-07-24)
(879, Kristen Coe, F, 1965-11-11)
(888, Doug Charlton, M, 1966-01-23)
(902, Moira Kelly, F, 1950-08-16)
(913, Ken Martel, M, 1943-04-23)
(921, Charles Crowley, M, 1960-09-11)
(930, Ann Taylor, F, 1962-06-06)
(949, Pamela Savarino, F, 1955-07-28)
(958, Thomas Sisson, M, 1969-10-02)
(992, Joyce Butterfield, F, 1960-04-15)
(1013, Joseph Barker, M, 1969-02-14)
(1021, Paul Sterling, M, 1950-02-27)
(1039, Shih Lin Chao, M, 1969-12-12)
(1062, Barbara Blaikie, F, 1953-11-14)
(1090, Susan Smith, F, 1950-11-30)
(1101, Mark Preston, M, 1966-09-14)
(1142, Alison Clark, F, 1957-05-04)
(1157, Hing Soo, M, 1970-03-07)
(1162, Kevin Goggin, M, 1952-04-18)
(1191, Matthew Bucceri, M, 1944-11-19)
(1250, Emilio Diaz, M, 1936-01-02)
(1293, Mary Anne Shea, F, 1955-03-13)
(1336, Janet Bigelow, F, 1950-07-21)
(1390, Jennifer Litton, F, 1948-04-05)
(1446, Caroline Yeung, F, 1971-06-21)
(1483, John Letiecq, M, 1939-04-27)
(1507, Ruth Wetherby, F, 1959-07-19)
(1570, Anthony Rebeiro, M, 1963-04-12)
(1576, Scott Evans, M, 1960-11-15)
(1596, Catherine Pickett, F, 1959-11-18)
(1607, Mark Morris, M, 1941-01-08)
(1615, Sheila Romero, F, 1972-09-12)
(1643, Elizabeth Lambert, F, 1968-09-12)
(1658, Michael Lynch, M, 1973-01-18)
(1684, Janet Hildebrand, F, 1955-10-31)
(1740, Robert Nielsen, M, 1965-06-19)
(1751, Alex Ahmed, M, 1963-12-12)
$ Java内幕

我在另一篇关于Oracle 8.0.5介绍中讨论过JDBC,在Sybase中使用JDBC十分类似(当然这是JDBC的主要目标之一)。然而,主要的数据库管理系统供应商在一个更专利的基础上一直在竞争的一个领域Java存储过程(JAVA stored procedure)。例如 PL/SQL 过程我在Oracle的文章有所论述,它允许用户创造能存储在数据库中的特定例程,与数据协作,这在客户端查询中允许更加成熟和定制特性。

Sybase支持传统的存储过程、程序存储模型和Transact-SQL方法,但是它也允许存储过程甚至使用JAVA重写的扩展类型。注意,Sybase确实至今还不允许对应于JAVA的SQLJ(SQLC对应C/C+)。Sybase的Java存储过程使用JDBC,并且是不比客户端的Java开发困难。

Sybase的Java存储过程仍不支持一些有用的JDBC数据类型,包括大二进制对象(BLOB)、大字符对象(CLOB )和数组。有希望的是,Sybase将不久支持这些,特别是CLOB对集中XML文件存储是非常有用的。

Tags:Sybase SQL Studio

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接