Tôi đang sử dụng MySQL Workbench CE cho Windows phiên bản 5.2.40.
Tôi muốn thực hiện các truy vấn SQL sau đây với nhau. Tuy nhiên, tôi chỉ có thể thực thi các truy vấn SQL bằng cách thực hiện truy vấn CREATE TABLE
trước tiên, sau đó thực thi truy vấn INSERT INTO
và sau đó thực hiện truy vấn SELECT
.
CREATE TABLE testTable[
Name VARCHAR[20],
Address VARCHAR[50],
Gender VARCHAR[10]
]
INSERT INTO testTable
VALUES
['Derp', 'ForeverAlone Street', 'Male'],
['Derpina', 'Whiterun Breezehome', 'Female']
Select * FROM testTable
Vậy làm cách nào để thực hiện các truy vấn CREATE TABLE
, INSERT INTO
và SELECT
bằng một cú nhấp chuột?
Chào mừng bạn đến với TechBrothersit
TechBrothersit là điểm blog và kênh video [YouTube] để tìm hiểu và chia sẻ thông tin, kịch bản, ví dụ thời gian thực về SQL Server, Transact-SQL [TSQL], Quản lý cơ sở dữ liệu SQL Server [SQL DBA], Business Intelligence [BI], SQL Dịch vụ tích hợp máy chủ [SSIS], Dịch vụ báo cáo máy chủ SQL [SSRS], Khái niệm Kho dữ liệu [DWH], Microsoft Dynamics AX, Microsoft Dynamics LifeCycle Services và tất cả các công nghệ khác nhau của Microsoft.
3.6.3 & NBSP; Hỗ trợ thực thi nhiều câu lệnh
Theo mặc định, mysql_real_query[]
và mysql_query[]
Giải thích đối số chuỗi câu lệnh của họ là một câu lệnh được thực thi và bạn xử lý kết quả theo liệu câu lệnh tạo ra một tập kết quả [một tập hợp các hàng, như đối với SELECT
] hoặc số lượng hàng bị ảnh hưởng [ Đối với
/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
printf["mysql_real_connect[] failed\n"];
mysql_close[mysql];
exit[1];
}
/* execute multiple statements */
status = mysql_query[mysql,
"DROP TABLE IF EXISTS test_table;\
CREATE TABLE test_table[id INT];\
INSERT INTO test_table VALUES[10];\
UPDATE test_table SET id=20 WHERE id=10;\
SELECT * FROM test_table;\
DROP TABLE test_table"];
if [status]
{
printf["Could not execute statement[s]"];
mysql_close[mysql];
exit[0];
}
/* process each statement result */
do {
/* did current statement return data? */
result = mysql_store_result[mysql];
if [result]
{
/* yes; process rows and free the result set */
process_result_set[mysql, result];
mysql_free_result[result];
}
else /* no result set or error */
{
if [mysql_field_count[mysql] == 0]
{
printf["%lld rows affected\n",
mysql_affected_rows[mysql]];
}
else /* some error occurred */
{
printf["Could not retrieve result set\n"];
break;
}
}
/* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
if [[status = mysql_next_result[mysql]] > 0]
printf["Could not execute statement\n"];
} while [status == 0];
mysql_close[mysql];
1, /* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
printf["mysql_real_connect[] failed\n"];
mysql_close[mysql];
exit[1];
}
/* execute multiple statements */
status = mysql_query[mysql,
"DROP TABLE IF EXISTS test_table;\
CREATE TABLE test_table[id INT];\
INSERT INTO test_table VALUES[10];\
UPDATE test_table SET id=20 WHERE id=10;\
SELECT * FROM test_table;\
DROP TABLE test_table"];
if [status]
{
printf["Could not execute statement[s]"];
mysql_close[mysql];
exit[0];
}
/* process each statement result */
do {
/* did current statement return data? */
result = mysql_store_result[mysql];
if [result]
{
/* yes; process rows and free the result set */
process_result_set[mysql, result];
mysql_free_result[result];
}
else /* no result set or error */
{
if [mysql_field_count[mysql] == 0]
{
printf["%lld rows affected\n",
mysql_affected_rows[mysql]];
}
else /* some error occurred */
{
printf["Could not retrieve result set\n"];
break;
}
}
/* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
if [[status = mysql_next_result[mysql]] > 0]
printf["Could not execute statement\n"];
} while [status == 0];
mysql_close[mysql];
2, v.v.].MySQL cũng hỗ trợ thực thi một chuỗi chứa nhiều câu lệnh được phân tách bằng các ký tự dấu chấm phẩy [
/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
printf["mysql_real_connect[] failed\n"];
mysql_close[mysql];
exit[1];
}
/* execute multiple statements */
status = mysql_query[mysql,
"DROP TABLE IF EXISTS test_table;\
CREATE TABLE test_table[id INT];\
INSERT INTO test_table VALUES[10];\
UPDATE test_table SET id=20 WHERE id=10;\
SELECT * FROM test_table;\
DROP TABLE test_table"];
if [status]
{
printf["Could not execute statement[s]"];
mysql_close[mysql];
exit[0];
}
/* process each statement result */
do {
/* did current statement return data? */
result = mysql_store_result[mysql];
if [result]
{
/* yes; process rows and free the result set */
process_result_set[mysql, result];
mysql_free_result[result];
}
else /* no result set or error */
{
if [mysql_field_count[mysql] == 0]
{
printf["%lld rows affected\n",
mysql_affected_rows[mysql]];
}
else /* some error occurred */
{
printf["Could not retrieve result set\n"];
break;
}
}
/* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
if [[status = mysql_next_result[mysql]] > 0]
printf["Could not execute statement\n"];
} while [status == 0];
mysql_close[mysql];
3]. Khả năng này được bật bởi các tùy chọn đặc biệt được chỉ định khi bạn kết nối với máy chủ với /* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
printf["mysql_real_connect[] failed\n"];
mysql_close[mysql];
exit[1];
}
/* execute multiple statements */
status = mysql_query[mysql,
"DROP TABLE IF EXISTS test_table;\
CREATE TABLE test_table[id INT];\
INSERT INTO test_table VALUES[10];\
UPDATE test_table SET id=20 WHERE id=10;\
SELECT * FROM test_table;\
DROP TABLE test_table"];
if [status]
{
printf["Could not execute statement[s]"];
mysql_close[mysql];
exit[0];
}
/* process each statement result */
do {
/* did current statement return data? */
result = mysql_store_result[mysql];
if [result]
{
/* yes; process rows and free the result set */
process_result_set[mysql, result];
mysql_free_result[result];
}
else /* no result set or error */
{
if [mysql_field_count[mysql] == 0]
{
printf["%lld rows affected\n",
mysql_affected_rows[mysql]];
}
else /* some error occurred */
{
printf["Could not retrieve result set\n"];
break;
}
}
/* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
if [[status = mysql_next_result[mysql]] > 0]
printf["Could not execute statement\n"];
} while [status == 0];
mysql_close[mysql];
4 hoặc sau khi kết nối bằng cách gọi /* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
printf["mysql_real_connect[] failed\n"];
mysql_close[mysql];
exit[1];
}
/* execute multiple statements */
status = mysql_query[mysql,
"DROP TABLE IF EXISTS test_table;\
CREATE TABLE test_table[id INT];\
INSERT INTO test_table VALUES[10];\
UPDATE test_table SET id=20 WHERE id=10;\
SELECT * FROM test_table;\
DROP TABLE test_table"];
if [status]
{
printf["Could not execute statement[s]"];
mysql_close[mysql];
exit[0];
}
/* process each statement result */
do {
/* did current statement return data? */
result = mysql_store_result[mysql];
if [result]
{
/* yes; process rows and free the result set */
process_result_set[mysql, result];
mysql_free_result[result];
}
else /* no result set or error */
{
if [mysql_field_count[mysql] == 0]
{
printf["%lld rows affected\n",
mysql_affected_rows[mysql]];
}
else /* some error occurred */
{
printf["Could not retrieve result set\n"];
break;
}
}
/* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
if [[status = mysql_next_result[mysql]] > 0]
printf["Could not execute statement\n"];
} while [status == 0];
mysql_close[mysql];
5.Thực hiện một chuỗi nhiều statement có thể tạo ra nhiều bộ kết quả hoặc các chỉ báo số hàng. Xử lý các kết quả này liên quan đến một cách tiếp cận khác với trường hợp tuyên bố đơn: sau khi xử lý kết quả từ câu lệnh đầu tiên, cần kiểm tra xem có nhiều kết quả tồn tại hay không và lần lượt xử lý chúng nếu như vậy. Để hỗ trợ xử lý nhiều kết quả, C API bao gồm các hàm
/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
printf["mysql_real_connect[] failed\n"];
mysql_close[mysql];
exit[1];
}
/* execute multiple statements */
status = mysql_query[mysql,
"DROP TABLE IF EXISTS test_table;\
CREATE TABLE test_table[id INT];\
INSERT INTO test_table VALUES[10];\
UPDATE test_table SET id=20 WHERE id=10;\
SELECT * FROM test_table;\
DROP TABLE test_table"];
if [status]
{
printf["Could not execute statement[s]"];
mysql_close[mysql];
exit[0];
}
/* process each statement result */
do {
/* did current statement return data? */
result = mysql_store_result[mysql];
if [result]
{
/* yes; process rows and free the result set */
process_result_set[mysql, result];
mysql_free_result[result];
}
else /* no result set or error */
{
if [mysql_field_count[mysql] == 0]
{
printf["%lld rows affected\n",
mysql_affected_rows[mysql]];
}
else /* some error occurred */
{
printf["Could not retrieve result set\n"];
break;
}
}
/* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
if [[status = mysql_next_result[mysql]] > 0]
printf["Could not execute statement\n"];
} while [status == 0];
mysql_close[mysql];
6 và /* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
printf["mysql_real_connect[] failed\n"];
mysql_close[mysql];
exit[1];
}
/* execute multiple statements */
status = mysql_query[mysql,
"DROP TABLE IF EXISTS test_table;\
CREATE TABLE test_table[id INT];\
INSERT INTO test_table VALUES[10];\
UPDATE test_table SET id=20 WHERE id=10;\
SELECT * FROM test_table;\
DROP TABLE test_table"];
if [status]
{
printf["Could not execute statement[s]"];
mysql_close[mysql];
exit[0];
}
/* process each statement result */
do {
/* did current statement return data? */
result = mysql_store_result[mysql];
if [result]
{
/* yes; process rows and free the result set */
process_result_set[mysql, result];
mysql_free_result[result];
}
else /* no result set or error */
{
if [mysql_field_count[mysql] == 0]
{
printf["%lld rows affected\n",
mysql_affected_rows[mysql]];
}
else /* some error occurred */
{
printf["Could not retrieve result set\n"];
break;
}
}
/* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
if [[status = mysql_next_result[mysql]] > 0]
printf["Could not execute statement\n"];
} while [status == 0];
mysql_close[mysql];
7. Các chức năng này được sử dụng ở cuối một vòng lặp lặp đi lặp lại miễn là có nhiều kết quả hơn. Việc không xử lý kết quả theo cách này có thể dẫn đến kết nối bị rơi với máy chủ.Failure to process the result this way may result in a dropped connection to
the server.Xử lý nhiều kết quả cũng là bắt buộc nếu bạn thực hiện các câu lệnh
/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
printf["mysql_real_connect[] failed\n"];
mysql_close[mysql];
exit[1];
}
/* execute multiple statements */
status = mysql_query[mysql,
"DROP TABLE IF EXISTS test_table;\
CREATE TABLE test_table[id INT];\
INSERT INTO test_table VALUES[10];\
UPDATE test_table SET id=20 WHERE id=10;\
SELECT * FROM test_table;\
DROP TABLE test_table"];
if [status]
{
printf["Could not execute statement[s]"];
mysql_close[mysql];
exit[0];
}
/* process each statement result */
do {
/* did current statement return data? */
result = mysql_store_result[mysql];
if [result]
{
/* yes; process rows and free the result set */
process_result_set[mysql, result];
mysql_free_result[result];
}
else /* no result set or error */
{
if [mysql_field_count[mysql] == 0]
{
printf["%lld rows affected\n",
mysql_affected_rows[mysql]];
}
else /* some error occurred */
{
printf["Could not retrieve result set\n"];
break;
}
}
/* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
if [[status = mysql_next_result[mysql]] > 0]
printf["Could not execute statement\n"];
} while [status == 0];
mysql_close[mysql];
8 cho các thủ tục được lưu trữ. Kết quả từ một thủ tục được lưu trữ có những đặc điểm này:Các câu lệnh trong quy trình có thể tạo ra các tập kết quả [ví dụ: nếu nó thực thi các câu lệnh
SELECT
]. Các bộ kết quả này được trả về theo thứ tự mà chúng được sản xuất khi thủ tục thực thi.Nói chung, người gọi không thể biết có bao nhiêu kết quả đặt một thủ tục sẽ trở lại. Thực hiện thủ tục có thể phụ thuộc vào các vòng lặp hoặc các câu lệnh có điều kiện khiến đường dẫn thực thi khác với cuộc gọi này sang cuộc gọi tiếp theo. Do đó, bạn phải chuẩn bị để lấy nhiều kết quả.
Kết quả cuối cùng từ thủ tục là kết quả trạng thái bao gồm không có kết quả. Trạng thái cho biết thủ tục thành công hay xảy ra lỗi.
Khả năng nhiều tuyên bố và kết quả chỉ có thể được sử dụng với mysql_real_query[]
hoặc mysql_query[]
. Chúng không thể được sử dụng với giao diện câu lệnh đã chuẩn bị. Trình xử lý câu lệnh được chuẩn bị được xác định chỉ hoạt động với các chuỗi có chứa một câu lệnh duy nhất. Xem Chương & NBSP; 6, C Giao diện câu lệnh đã chuẩn bị API.
Để cho phép thực hiện nhiều lần thực hiện và xử lý kết quả, các tùy chọn sau có thể được sử dụng:
Hàm
4 có đối số/* connect to server with the CLIENT_MULTI_STATEMENTS option */ if [mysql_real_connect [mysql, host_name, user_name, password, db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL] { printf["mysql_real_connect[] failed\n"]; mysql_close[mysql]; exit[1]; } /* execute multiple statements */ status = mysql_query[mysql, "DROP TABLE IF EXISTS test_table;\ CREATE TABLE test_table[id INT];\ INSERT INTO test_table VALUES[10];\ UPDATE test_table SET id=20 WHERE id=10;\ SELECT * FROM test_table;\ DROP TABLE test_table"]; if [status] { printf["Could not execute statement[s]"]; mysql_close[mysql]; exit[0]; } /* process each statement result */ do { /* did current statement return data? */ result = mysql_store_result[mysql]; if [result] { /* yes; process rows and free the result set */ process_result_set[mysql, result]; mysql_free_result[result]; } else /* no result set or error */ { if [mysql_field_count[mysql] == 0] { printf["%lld rows affected\n", mysql_affected_rows[mysql]]; } else /* some error occurred */ { printf["Could not retrieve result set\n"]; break; } } /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */ if [[status = mysql_next_result[mysql]] > 0] printf["Could not execute statement\n"]; } while [status == 0]; mysql_close[mysql];
CREATE TABLE
3 trong đó hai giá trị tùy chọn có liên quan:CREATE TABLE
4 cho phép chương trình máy khách xử lý nhiều kết quả. Tùy chọn này phải được bật nếu bạn thực thi các câu lệnh
8 cho các quy trình được lưu trữ tạo ra các bộ kết quả. Mặt khác, các quy trình đó dẫn đến lỗi/* connect to server with the CLIENT_MULTI_STATEMENTS option */ if [mysql_real_connect [mysql, host_name, user_name, password, db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL] { printf["mysql_real_connect[] failed\n"]; mysql_close[mysql]; exit[1]; } /* execute multiple statements */ status = mysql_query[mysql, "DROP TABLE IF EXISTS test_table;\ CREATE TABLE test_table[id INT];\ INSERT INTO test_table VALUES[10];\ UPDATE test_table SET id=20 WHERE id=10;\ SELECT * FROM test_table;\ DROP TABLE test_table"]; if [status] { printf["Could not execute statement[s]"]; mysql_close[mysql]; exit[0]; } /* process each statement result */ do { /* did current statement return data? */ result = mysql_store_result[mysql]; if [result] { /* yes; process rows and free the result set */ process_result_set[mysql, result]; mysql_free_result[result]; } else /* no result set or error */ { if [mysql_field_count[mysql] == 0] { printf["%lld rows affected\n", mysql_affected_rows[mysql]]; } else /* some error occurred */ { printf["Could not retrieve result set\n"]; break; } } /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */ if [[status = mysql_next_result[mysql]] > 0] printf["Could not execute statement\n"]; } while [status == 0]; mysql_close[mysql];
CREATE TABLE
6 không thể trả về kết quả được đặt trong bối cảnh đã cho.CREATE TABLE
4 được bật theo mặc định.must be enabled if you execute
8 statements for stored procedures that produce result sets. Otherwise, such procedures result in an error/* connect to server with the CLIENT_MULTI_STATEMENTS option */ if [mysql_real_connect [mysql, host_name, user_name, password, db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL] { printf["mysql_real_connect[] failed\n"]; mysql_close[mysql]; exit[1]; } /* execute multiple statements */ status = mysql_query[mysql, "DROP TABLE IF EXISTS test_table;\ CREATE TABLE test_table[id INT];\ INSERT INTO test_table VALUES[10];\ UPDATE test_table SET id=20 WHERE id=10;\ SELECT * FROM test_table;\ DROP TABLE test_table"]; if [status] { printf["Could not execute statement[s]"]; mysql_close[mysql]; exit[0]; } /* process each statement result */ do { /* did current statement return data? */ result = mysql_store_result[mysql]; if [result] { /* yes; process rows and free the result set */ process_result_set[mysql, result]; mysql_free_result[result]; } else /* no result set or error */ { if [mysql_field_count[mysql] == 0] { printf["%lld rows affected\n", mysql_affected_rows[mysql]]; } else /* some error occurred */ { printf["Could not retrieve result set\n"]; break; } } /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */ if [[status = mysql_next_result[mysql]] > 0] printf["Could not execute statement\n"]; } while [status == 0]; mysql_close[mysql];
CREATE TABLE
6 can't return a result set in the given context.CREATE TABLE
4 is enabled by default.CREATE TABLE
8 cho phépmysql_real_query[]
vàmysql_query[]
thực thi các chuỗi câu lệnh chứa nhiều câu lệnh được phân tách bằng dấu chấm phẩy. Tùy chọn này cũng cho phépCREATE TABLE
4 ngầm, do đó, một đối sốCREATE TABLE
3 củaCREATE TABLE
8 với
4 tương đương với một đối số của/* connect to server with the CLIENT_MULTI_STATEMENTS option */ if [mysql_real_connect [mysql, host_name, user_name, password, db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL] { printf["mysql_real_connect[] failed\n"]; mysql_close[mysql]; exit[1]; } /* execute multiple statements */ status = mysql_query[mysql, "DROP TABLE IF EXISTS test_table;\ CREATE TABLE test_table[id INT];\ INSERT INTO test_table VALUES[10];\ UPDATE test_table SET id=20 WHERE id=10;\ SELECT * FROM test_table;\ DROP TABLE test_table"]; if [status] { printf["Could not execute statement[s]"]; mysql_close[mysql]; exit[0]; } /* process each statement result */ do { /* did current statement return data? */ result = mysql_store_result[mysql]; if [result] { /* yes; process rows and free the result set */ process_result_set[mysql, result]; mysql_free_result[result]; } else /* no result set or error */ { if [mysql_field_count[mysql] == 0] { printf["%lld rows affected\n", mysql_affected_rows[mysql]]; } else /* some error occurred */ { printf["Could not retrieve result set\n"]; break; } } /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */ if [[status = mysql_next_result[mysql]] > 0] printf["Could not execute statement\n"]; } while [status == 0]; mysql_close[mysql];
INSERT INTO
5. Đó là,CREATE TABLE
8 là đủ để cho phép thực thi nhiều câu chuyện và tất cả các xử lý nhiều kết quả.
Sau khi kết nối với máy chủ đã được thiết lập, bạn có thể sử dụng chức năng
5 để bật hoặc tắt thực thi nhiều lần bằng cách chuyển cho nó một đối số là/* connect to server with the CLIENT_MULTI_STATEMENTS option */ if [mysql_real_connect [mysql, host_name, user_name, password, db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL] { printf["mysql_real_connect[] failed\n"]; mysql_close[mysql]; exit[1]; } /* execute multiple statements */ status = mysql_query[mysql, "DROP TABLE IF EXISTS test_table;\ CREATE TABLE test_table[id INT];\ INSERT INTO test_table VALUES[10];\ UPDATE test_table SET id=20 WHERE id=10;\ SELECT * FROM test_table;\ DROP TABLE test_table"]; if [status] { printf["Could not execute statement[s]"]; mysql_close[mysql]; exit[0]; } /* process each statement result */ do { /* did current statement return data? */ result = mysql_store_result[mysql]; if [result] { /* yes; process rows and free the result set */ process_result_set[mysql, result]; mysql_free_result[result]; } else /* no result set or error */ { if [mysql_field_count[mysql] == 0] { printf["%lld rows affected\n", mysql_affected_rows[mysql]]; } else /* some error occurred */ { printf["Could not retrieve result set\n"]; break; } } /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */ if [[status = mysql_next_result[mysql]] > 0] printf["Could not execute statement\n"]; } while [status == 0]; mysql_close[mysql];
INSERT INTO
8 hoặcINSERT INTO
9. Kích hoạt thực thi nhiều tuyên bố với chức năng này cũng cho phép xử lý các kết quả đơn giản cho một chuỗi nhiều câu trong đó mỗi câu lệnh tạo ra một kết quả duy nhất, nhưng không đủ để cho phép xử lý các quy trình được lưu trữ tạo ra các bộ kết quả.“simple” results for a multiple-statement string where each statement produces a single result, but is not sufficient to permit processing of stored procedures that produce result sets.
Quy trình sau đây phác thảo một chiến lược được đề xuất để xử lý nhiều tuyên bố:
Vượt qua
CREATE TABLE
8 cho
4, để kích hoạt hoàn toàn thực thi nhiều trường hợp và xử lý nhiều kết quả./* connect to server with the CLIENT_MULTI_STATEMENTS option */ if [mysql_real_connect [mysql, host_name, user_name, password, db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL] { printf["mysql_real_connect[] failed\n"]; mysql_close[mysql]; exit[1]; } /* execute multiple statements */ status = mysql_query[mysql, "DROP TABLE IF EXISTS test_table;\ CREATE TABLE test_table[id INT];\ INSERT INTO test_table VALUES[10];\ UPDATE test_table SET id=20 WHERE id=10;\ SELECT * FROM test_table;\ DROP TABLE test_table"]; if [status] { printf["Could not execute statement[s]"]; mysql_close[mysql]; exit[0]; } /* process each statement result */ do { /* did current statement return data? */ result = mysql_store_result[mysql]; if [result] { /* yes; process rows and free the result set */ process_result_set[mysql, result]; mysql_free_result[result]; } else /* no result set or error */ { if [mysql_field_count[mysql] == 0] { printf["%lld rows affected\n", mysql_affected_rows[mysql]]; } else /* some error occurred */ { printf["Could not retrieve result set\n"]; break; } } /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */ if [[status = mysql_next_result[mysql]] > 0] printf["Could not execute statement\n"]; } while [status == 0]; mysql_close[mysql];
-
Sau khi gọi
mysql_real_query[]
hoặcmysql_query[]
và xác minh rằng nó thành công, hãy nhập một vòng lặp trong đó bạn xử lý kết quả báo cáo. Đối với mỗi lần lặp của vòng lặp, xử lý kết quả câu lệnh hiện tại, truy xuất một tập kết quả hoặc số lượng hàng bị ảnh hưởng. Nếu xảy ra lỗi, thoát khỏi vòng lặp.
Vào cuối vòng lặp, hãy gọi
7 để kiểm tra xem một kết quả khác có tồn tại và bắt đầu truy xuất cho nó không. Nếu không có thêm kết quả, hãy thoát khỏi vòng lặp./* connect to server with the CLIENT_MULTI_STATEMENTS option */ if [mysql_real_connect [mysql, host_name, user_name, password, db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL] { printf["mysql_real_connect[] failed\n"]; mysql_close[mysql]; exit[1]; } /* execute multiple statements */ status = mysql_query[mysql, "DROP TABLE IF EXISTS test_table;\ CREATE TABLE test_table[id INT];\ INSERT INTO test_table VALUES[10];\ UPDATE test_table SET id=20 WHERE id=10;\ SELECT * FROM test_table;\ DROP TABLE test_table"]; if [status] { printf["Could not execute statement[s]"]; mysql_close[mysql]; exit[0]; } /* process each statement result */ do { /* did current statement return data? */ result = mysql_store_result[mysql]; if [result] { /* yes; process rows and free the result set */ process_result_set[mysql, result]; mysql_free_result[result]; } else /* no result set or error */ { if [mysql_field_count[mysql] == 0] { printf["%lld rows affected\n", mysql_affected_rows[mysql]]; } else /* some error occurred */ { printf["Could not retrieve result set\n"]; break; } } /* more results? -1 = no, >0 = error, 0 = yes [keep looping] */ if [[status = mysql_next_result[mysql]] > 0] printf["Could not execute statement\n"]; } while [status == 0]; mysql_close[mysql];
Một thực hiện có thể của chiến lược trước được hiển thị sau. Phần cuối cùng của vòng lặp có thể được giảm xuống thành một bài kiểm tra đơn giản về việc
/* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
printf["mysql_real_connect[] failed\n"];
mysql_close[mysql];
exit[1];
}
/* execute multiple statements */
status = mysql_query[mysql,
"DROP TABLE IF EXISTS test_table;\
CREATE TABLE test_table[id INT];\
INSERT INTO test_table VALUES[10];\
UPDATE test_table SET id=20 WHERE id=10;\
SELECT * FROM test_table;\
DROP TABLE test_table"];
if [status]
{
printf["Could not execute statement[s]"];
mysql_close[mysql];
exit[0];
}
/* process each statement result */
do {
/* did current statement return data? */
result = mysql_store_result[mysql];
if [result]
{
/* yes; process rows and free the result set */
process_result_set[mysql, result];
mysql_free_result[result];
}
else /* no result set or error */
{
if [mysql_field_count[mysql] == 0]
{
printf["%lld rows affected\n",
mysql_affected_rows[mysql]];
}
else /* some error occurred */
{
printf["Could not retrieve result set\n"];
break;
}
}
/* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
if [[status = mysql_next_result[mysql]] > 0]
printf["Could not execute statement\n"];
} while [status == 0];
mysql_close[mysql];
7 có trả lại không khác. Mã bằng văn bản phân biệt giữa không còn kết quả và lỗi, cho phép một thông báo được in cho lần xuất hiện sau./* connect to server with the CLIENT_MULTI_STATEMENTS option */
if [mysql_real_connect [mysql, host_name, user_name, password,
db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS] == NULL]
{
printf["mysql_real_connect[] failed\n"];
mysql_close[mysql];
exit[1];
}
/* execute multiple statements */
status = mysql_query[mysql,
"DROP TABLE IF EXISTS test_table;\
CREATE TABLE test_table[id INT];\
INSERT INTO test_table VALUES[10];\
UPDATE test_table SET id=20 WHERE id=10;\
SELECT * FROM test_table;\
DROP TABLE test_table"];
if [status]
{
printf["Could not execute statement[s]"];
mysql_close[mysql];
exit[0];
}
/* process each statement result */
do {
/* did current statement return data? */
result = mysql_store_result[mysql];
if [result]
{
/* yes; process rows and free the result set */
process_result_set[mysql, result];
mysql_free_result[result];
}
else /* no result set or error */
{
if [mysql_field_count[mysql] == 0]
{
printf["%lld rows affected\n",
mysql_affected_rows[mysql]];
}
else /* some error occurred */
{
printf["Could not retrieve result set\n"];
break;
}
}
/* more results? -1 = no, >0 = error, 0 = yes [keep looping] */
if [[status = mysql_next_result[mysql]] > 0]
printf["Could not execute statement\n"];
} while [status == 0];
mysql_close[mysql];