我有一个 c 程序,提示用户输入产品制造商,并应根据用户输入进行查询以打印该制造商生产的所有产品,无论它们是 PC、笔记本电脑还是打印机。但是,这里出了点问题,因为在返回一行后我得到了两个 sql 状态:
SQLSTATE-02000 和 SQLSTATE=24000
这些都不在我搜索过的 sql 错误代码中。
下面我包含了我的 .c 文件和我正在使用的 .sql 架构。
任何帮助是极大的赞赏。
#include <stdio.h>
#include <stdlib.h>
/*** header file for ORACLE definitions ***/
exec sql include sqlca;
int print_sql_state(char *code){
printf("\nSQLSTATE=%s\n", code);
}
int main()
{
/*** Variable Declaration Block ***/
exec sql begin declare section;
char login[129], password[129];
char SQLSTATE[6];
/*query based on*/
char maker[40];
/*results based on product*/
int model;
char type[40];
/*results based on type of product*/
float speed;
int ram;
int hd;
float screen;
int price;
exec sql end declare section;
/*** handler for error ***/
exec sql whenever sqlerror goto report_error;
exec sql whenever not found goto notfound;
/*** Prompt user for login name and password ***/
printf("Username=");
gets(login);
printf("Password=");
char *pass_string = malloc(128);
pass_string=getpass("");
strcpy(password, pass_string);
/*** Connect to DB ***/
exec sql connect :login
identified by :password;
printf("\nCONNECTION SUCCESSFUL\n");
/*** INSERT RECORD WITH VALUES PROVIDED BY USER ***/
while(1){
printf("\nInput manufacturer:");
scanf("%s", &maker);
if (maker==-1) /* end loop */
return 0;
/*** cursor declaration ***/
exec sql declare specs_1 cursor for
SELECT P.model, P.speed, P.ram, P.hd, P.price
FROM Product PR, PC P
WHERE PR.model = P.model AND PR.maker = :maker
UNION
SELECT L.model, L.speed, L.ram, L.hd, L.price
FROM Product PR, Laptop L
WHERE PR.model = L.model AND PR.maker = :maker;
/*seperate query since not Union Compatible*/
exec sql declare specs_2 cursor for
SELECT P.model, P.price
FROM Product PR, Printer P
WHERE PR.model = P.model AND PR.maker = :maker;
exec sql open specs_1;
/*** TRAVERSE RESULT SET WITH CURSOR ***/
while(1){
EXEC SQL
FETCH specs_1 into :model, :speed, :ram, :hd, :price;
printf("\nModel = %d, Speed = %f, Ram = %d, hd = %d, Price = %d",
model, speed, ram, hd, price);
}
exec sql open specs_2;
/*** TRAVERSE RESULT SET WITH CURSOR ***/
while(1){
EXEC SQL
FETCH specs_2 into :model, :price;
printf("\nModel = %d, Price = %d", model, price);
}
continue;
exec sql close specs_1;
exec sql close specs_2;
notfound:
printf("\nNO DATA");
print_sql_state(SQLSTATE);
exec sql close specs_1;
exec sql close specs_2;
}
report_error:
printf("\nCONNECTION ERROR\n");
print_sql_state(SQLSTATE);
return 1;
}
和 SQL
CREATE TABLE Product(
maker VARCHAR(50),
model INTEGER PRIMARY KEY,
type VARCHAR(10) /*PC, Laptop, or Printer*/
);
CREATE TABLE PC(
model INTEGER PRIMARY KEY, /*model number*/
speed REAL,
ram INTEGER,
hd INTEGER,
price INTEGER /*"in dollars"*/
;
CREATE TABLE Laptop(
model INTEGER PRIMARY KEY,
speed REAL,
ram INTEGER,
hd INTEGER,
price INTEGER,
screen INTEGER
);
CREATE TABLE Printer(
model INTEGER PRIMARY KEY, /*model number*/
price INTEGER /*"in dollars"*/
);
编辑::: 输入“戴尔”作为“制造商”时我的程序的输出
Username=me
Password=
CONNECTION SUCCESSFUL
Input manufacturer:Dell
Model = 1, Speed = 3.400000, Ram = 4000, hd = 125, Price = 999
NO DATA
SQLSTATE=02000
CONNECTION ERROR
SQLSTATE=24000
编辑:: 这些是我一直在使用的测试插件。
INSERT INTO Product Values('Alien', 5, 'PC');
INSERT INTO PC Values(5, 3.0, 8000, 500, 1299);
INSERT INTO Product Values('HP', 7, 'PC');
INSERT INTO PC Values(7, 3.0, 8000, 500, 2899);
INSERT INTO Product Values('Toshiba', 8, 'PC');
INSERT INTO PC Values(8, .3, 1000, 20, 299, 10);
INSERT INTO Product Values('Dell', 1, 'Laptop');
INSERT INTO Laptop Values(1, 3.4, 4000, 125, 999, 13);
INSERT INTO Product Values('Sony', 4, 'Laptop');
INSERT INTO Laptop Values(4, 1.2, 2000, 20, 299, 10);
INSERT INTO Product Values('HP', 2, 'Laptop');
INSERT INTO PC Values(2, 1.0, 8000, 500, 899, 19);
INSERT INTO Product Values('Toshiba', 9, 'Laptop');
INSERT INTO Laptop Values(9, .4, 900, 20, 299, 10);
INSERT INTO Product Values('Toshiba', 10, 'Laptop');
INSERT INTO Laptop Values(10, .4, 900, 20, 299, 13);
INSERT INTO Product Values('Toshiba', 11, 'Laptop');
INSERT INTO Laptop Values(11, .4, 900, 20, 299, 10);
INSERT INTO Product Values('Toshiba', 12, 'Laptop');
INSERT INTO Laptop Values(12, .4, 900, 20, 299, 19);
INSERT INTO Product Values('Dell', 3, 'Printer');
INSERT INTO Printer Values(3, 199);
INSERT INTO Product Values('Kodak', 6, 'Printer');
INSERT INTO Printer Values(6, 300);