鐩綍
鏈珷浠嬬粛浜哅ySQL鍙娇鐢ㄧ殑API锛屼粠鍝噷鑾峰緱瀹冧滑锛屼互鍙婂浣曚娇鐢ㄥ畠浠 璇︾粏浠嬬粛C API锛岃繖鏄洜涓哄畠鏄敱MySQL鍥㈤槦寮鍙戠殑锛岃屼笖瀹冧篃鏄ぇ澶氭暟鍏朵粬API鐨勫熀纭銆 鏈珷杩樹粙缁嶄簡libmysqld搴擄紙宓屽叆寮忔湇鍔″櫒锛夛紝浠ュ強瀵瑰簲鐢ㄧ▼搴忓紑鍙戜汉鍛樻湁鐢ㄧ殑涓浜涚▼搴忋
浣跨敤宓屽叆寮廙ySQL鏈嶅姟鍣ㄥ簱锛岃兘澶熷湪瀹㈡埛绔簲鐢ㄧ▼搴忎腑浣跨敤鍏峰鍏ㄩ儴鐗规х殑MySQL鏈嶅姟鍣ㄣ 涓昏浼樼偣鍦ㄤ簬锛屽鍔犱簡閫熷害锛屽苟浣垮緱宓屽叆寮忓簲鐢ㄧ▼搴忕殑绠$悊鏇寸畝鍗曘
宓屽叆寮忔湇鍔″櫒搴撴槸浠ySQL鐨勫鎴风锛忔湇鍔″櫒鐗堟湰涓哄熀纭鐨勶紝閲囩敤C/C++璇█缂栧啓銆 鍏剁粨鏋滄槸宓屽叆寮忔湇鍔″櫒涔熸槸鐢–/C++璇█缂栧啓鐨勩 鍦ㄥ叾浠栬瑷涓紝宓屽叆寮忔湇鍔″櫒涓嶅彲鐢ㄣ
API涓庡祵鍏ュ紡MySQL鐗堟湰鍜屽鎴风锛忔湇鍔″櫒鐗堟湰绛夋晥銆 瑕佹兂鏇存敼鏃х殑绾跨▼寮忓簲鐢ㄧ▼搴忎互浣跨敤宓屽叆寮忓簱锛屾甯告儏鍐典笅锛屼粎闇娣诲姞瀵逛笅杩板嚱鏁扮殑璋冪敤鍗冲彲銆
鍑芥暟 |
浣曟椂璋冪敤 |
mysql_server_init() |
搴斿湪璋冪敤浠讳綍鍏朵粬MySQL鍑芥暟涔嬪墠璋冪敤锛屾渶濂芥槸鍦main()鍑芥暟涓皟鐢ㄣ |
mysql_server_end() |
搴斿湪绋嬪簭閫鍑哄墠璋冪敤銆 |
mysql_thread_init() |
搴斿湪浣犳墍鍒涘缓鐨勩佺敤浜庤闂甅ySQL鐨勬瘡涓嚎绋嬩腑璋冪敤銆 |
mysql_thread_end() |
搴斿湪璋冪敤pthread_exit()涔嬪墠璋冪敤銆 |
闅忓悗锛屽繀椤诲皢浣犵殑浠g爜涓libmysqld.a閾炬帴璧锋潵锛岃屼笉鏄痩ibmysqlclient.a銆
鍦libmysqlclient.a涓繕鍖呭惈mysql_server_xxx()鍑芥暟锛屼娇鐢ㄨ繖绫诲嚱鏁帮紝閫氳繃灏嗗簲鐢ㄧ▼搴忛摼鎺ュ埌鎭板綋鐨勫簱锛屽嵆鍙湪宓屽叆寮忕増鏈拰瀹㈡埛绔紡鏈嶅姟鍣ㄧ増鏈箣闂村垏鎹€ 璇峰弬瑙25.2.12.1鑺傦紝鈥渕ysql_server_init()鈥銆
宓屽叆寮忔湇鍔″櫒鍜岀嫭绔嬫湇鍔″櫒涔嬮棿鐨勪竴椤瑰樊鍒湪浜庯紝瀵逛簬宓屽叆寮忔湇鍔″櫒锛岄粯璁ゆ儏鍐典笅锛岃繛鎺ラ壌瀹氭槸绂佹鐨勩 瀵逛簬宓屽叆寮忔湇鍔″櫒锛岃鎯充娇鐢ㄩ壌瀹氬姛鑳斤紝鍙湪婵娲烩configure鈥濅互閰嶇疆MySQL鍒嗗彂鐗堟椂浣跨敤鈥--with-embedded-privilege-control鈥濋夐」銆
瑕佹兂鑾峰緱libmysqld搴擄紝搴斾娇鐢鈥--with-embedded-server鈥閫夐」閰嶇疆MySQL銆 璇峰弬瑙2.8.2鑺傦紝鈥滃吀鍨嬮厤缃夐」鈥銆
灏嗕綘鐨勭▼搴忎笌libmysqld閾炬帴鏃讹紝杩樺繀椤诲寘鍚郴缁熺殑pthread搴撲互鍙奙ySQL鏈嶅姟鍣ㄤ娇鐢ㄧ殑涓浜涘簱銆 鎵ц鈥mysql_config --libmysqld-libs鈥濓紝鍙幏寰楀簱鐨勫畬鏁村垪琛ㄣ
瀵逛簬绾跨▼绋嬪簭鐨勭紪璇戝拰閾炬帴锛屽繀椤讳娇鐢ㄦ纭殑鏍囧織锛屽嵆浣夸綘鏈湪浠g爜涓洿鎺ヨ皟鐢ㄤ换浣曠嚎绋嬪嚱鏁颁篃鍚屾牱銆
瑕佹兂缂栬瘧C绋嬪簭浠ュ寘鍚繀瑕佹枃浠讹紝骞跺皢MySQL鏈嶅姟鍣ㄥ簱宓屽叆鍒扮▼搴忕殑缂栬瘧鐗堟湰涓紝鍙娇鐢℅NU C缂栬瘧鍣紙gcc锛夈 缂栬瘧鍣ㄩ渶瑕佺煡閬撳悇绉嶆枃浠剁殑浣嶇疆锛屽苟闇浜嗚В濡備綍缂栬瘧绋嬪簭鐨勬寚浠ゃ 鍦ㄤ笅闈㈢殑绀轰緥涓紝浠嬬粛浜嗗浣曚粠鍛戒护琛岀紪璇戠▼搴忕殑鏂规硶锛
gcc mysql_test.c -o mysql_test -lz \
`/usr/local/mysql/bin/mysql_config --include --libmysqld-libs`
鍦╣cc鍛戒护鍚庣揣璺熺潃鏈紪璇慍绋嬪簭鏂囦欢鐨勫悕绉般 鎺ヤ笅鏉ワ紝缁欏畾鐨勨-o鈥濋夐」鎸囨槑锛屽畠鍚庨潰鐨勬枃浠跺悕鏄紪璇戝櫒灏嗚緭鍑烘枃浠剁殑鍚嶇О锛屽嵆缂栬瘧鍚庣殑绋嬪簭銆 鍦ㄤ笅涓琛岀殑浠g爜涓紝閫氱煡缂栬瘧鍣ㄨ幏鍙栧寘鍚枃浠跺拰搴撶殑浣嶇疆锛屼互鍙婂湪鍏朵笂杩涜缂栬瘧鐨勭郴缁熺殑鍏朵粬璁剧疆銆 鐢变簬鈥mysql_config鈥濆瓨鍦ㄧ殑闂锛屽湪姝ゆ坊鍔犱簡鈥-lz鈥濋夐」锛堝帇缂╋級銆 鈥mysql_config鈥濋儴鍒嗗寘鍚湪backticks涓紝鑰屼笉鏄崟寮曞彿鍐呫
宓屽叆寮忔湇鍔″櫒瀛樺湪涓嬭堪闄愬埗锛
路 涓嶆敮鎸ISAM琛ㄣ 锛堣繖鏍峰仛鐨勪富瑕佺洰鐨勬槸涓轰簡浣垮簱鏇村皬锛夈
路 娌℃湁鑷畾涔夊嚱鏁帮紙UDF锛夈
路 娌℃湁瀵规牳蹇冭浆鍌ㄧ殑鍫嗘爤璺熻釜銆
路 娌℃湁鍐呴儴RAID鏀寔銆 锛堢敱浜庡ぇ澶氭暟褰撳墠鎿嶄綔绯荤粺鍧囨敮鎸佸ぇ鏂囦欢锛岄氬父鎯呭喌涓嬩笉闇瑕佸畠锛夈
路 涓嶈兘灏嗗叾璁剧疆涓衡滀富鈥濇垨鈥滀粠鈥濓紙鏃犲鍒讹級銆
路 鍦ㄥ唴瀛樿緝浣庣殑绯荤粺涓婏紝鍙兘鏃犳硶浣跨敤寰堝ぇ鐨勭粨鏋滈泦銆
路 涓嶈兘浣跨敤濂楁帴瀛楁垨TCP/IP浠庡閮ㄨ繘绋嬭繛鎺ュ埌宓屽叆寮忔湇鍔″櫒銆 浣嗘槸锛屼綘鍙互杩炴帴鍒颁腑闂村簲鐢ㄧ▼搴忥紝闅忓悗锛岃涓棿搴旂敤绋嬪簭鍙唬琛ㄨ繙绋嬪鎴风鎴栧閮ㄨ繘绋嬭繛鎺ュ埌宓屽叆寮忔湇鍔″櫒銆
閫氳繃缂栬緫鈥mysql_embed.h鈥濆寘鍚枃浠跺苟閲嶆柊缂栬瘧MySQL锛屽彲鏇存敼鏌愪簺闄愬埗銆
瀵逛簬浠讳綍鑳藉涓mysqld鏈嶅姟鍣ㄧ鍙g洃鐫g▼搴忎竴璧风粰瀹氱殑閫夐」锛屼篃鍙互涓庡祵鍏ュ紡鏈嶅姟鍣ㄥ簱涓璧蜂娇鐢ㄣ傚湪鏁扮粍涓紝鍙皢鏈嶅姟鍣ㄩ夐」浣滀负鍙傞噺鎸囧畾缁欑敤浜庡垵濮嬪寲鏈嶅姟鍣ㄧ殑mysql_server_init()銆備篃鑳藉湪璇稿my.cnf鐨勯夐」鏂囦欢涓粰瀹氬畠浠瑕佹兂涓C绋嬪簭鎸囧畾閫夐」鏂囦欢锛岃浣跨敤鈥--defaults-file鈥閫夐」浣滀负鍑芥暟mysql_server_init()鐨勭2涓弬閲忕殑鍏冪礌涔嬩竴銆傚叧浜mysql_server_init()鍑芥暟鐨勬洿澶氫俊鎭紝璇峰弬瑙25.2.12.1鑺傦紝鈥渕ysql_server_init()鈥銆
浣跨敤閫夐」鏂囦欢锛岃兘澶熺畝鍖栧鎴风锛忔湇鍔″櫒搴旂敤绋嬪簭鍜屽祵鍏ヤ簡MySQL鐨勫簲鐢ㄧ▼搴忎箣闂寸殑鍒囨崲銆傚皢甯哥敤閫夐」缃簬[server]缁勩傚畠浠彲琚袱绉MySQL鐗堟湰璇诲彇銆傚鎴风锛忔湇鍔″櫒閫夐」搴旇鏀惧湪[mysqld]閮ㄥ垎銆傚皢宓屽叆寮MySQL鏈嶅姟鍣ㄥ簱鐨勯夐」鏀惧湪[embedded]閮ㄥ垎銆傚皢涓庡簲鐢ㄧ▼搴忕浉鍏崇殑閫夐」鏀惧湪鏍囪涓[ApplicationName_SERVER]鐨勯儴鍒嗐璇峰弬瑙4.3.2鑺傦紝鈥滀娇鐢ㄩ夐」鏂囦欢鈥銆
路 鎴戜滑灏嗘彁渚涗竴浜涢夐」浠ョ渷鍘MySQL鐨勬煇浜涢儴鍒嗭紝浠庤屼娇搴撳彉寰楁洿灏忋
路 浠嶆湁寰堝閫熷害浼樺寲宸ヤ綔闇瑕佸畬鎴愩
路 閿欒灏嗚鍐欏叆stderr銆傛垜浠皢澧炲姞1涓夐」涓哄畠浠寚瀹氭枃浠跺悕銆
路 浣跨敤宓屽叆寮忕増鏈椂锛岄渶瑕佹洿鏀InnoDB锛屼娇涔嬩笉鍐嶅啑闀裤傚鏋滀綘鐨勬暟鎹簱涓嶅惈InnoDB琛紝瑕佹兂鎶戝埗鐩稿叧娑堟伅锛屽彲涓虹粍[libmysqd_server]涓嬬殑閫夐」鏂囦欢澧炲姞鈥--skip-innodb鈥閫夐」锛屾垨鍦ㄧ敤mysql_server_init()鍒濆鍖栨湇鍔″櫒鏃舵坊鍔犺閫夐」銆
鍦Linux鎴FreeBSD绯荤粺涓婏紝鏃犻渶鏇存敼灏辫兘浣跨敤涓嬮潰杩欎袱涓ず渚嬬▼搴忋傚浜庡叾浠栨搷浣滅郴缁燂紝闇瑕佽繘琛屽皬鐨勪慨鏀癸紝涓昏鏄枃浠惰矾寰勩傝璁¤繖涓や釜绀轰緥鐨勭洰鐨勫湪浜庯紝涓轰綘鎻愪緵瓒冲鐨勭粏鑺備俊鎭紝浠ヤ究鐞嗚В闂锛屽畠浠槸瀹為檯搴旂敤绋嬪簭鐨勫繀瑕佺粍鎴愰儴浠姐傜1涓ず渚嬪崄鍒嗙洿瑙傘傜2涓ず渚嬮噰鐢ㄤ簡涓浜涢敊璇鏌ュ姛鑳斤紝鐣ヤ负澶嶆潅銆傚湪绗1涓ず渚嬬殑鍚庨潰锛岀粰鍑轰簡鐢ㄤ簬缂栬瘧绋嬪簭鐨勫懡浠よ鏉$洰銆傚湪绗2涓ず渚嬬殑鍚庨潰锛岀粰鍑轰簡GNUmake鏂囦欢锛岃鏂囦欢鍙敤浜庣紪璇戙
绀轰緥锛1
test1_libmysqld.c
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "mysql.h"
MYSQL *mysql;
MYSQL_RES *results;
MYSQL_ROW record;
static char *server_options[] = { "mysql_test", "--defaults-file=my.cnf" };
int num_elements = sizeof(server_options)/ sizeof(char *);
static char *server_groups[] = { "libmysqld_server", "libmysqld_client" };
int main(void)
{
mysql_server_init(num_elements, server_options, server_groups);
mysql = mysql_init(NULL);
mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");
mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
mysql_real_connect(mysql, NULL,NULL,NULL, "database1", 0,NULL,0);
mysql_query(mysql, "SELECT column1, column2 FROM table1");
results = mysql_store_result(mysql);
while((record = mysql_fetch_row(results))) {
printf("%s - %s \n", record[0], record[1]);
}
mysql_free_result(results);
mysql_close(mysql);
mysql_server_end();
return 0;
}
涓嬮潰缁欏嚭浜嗙紪璇戜笂杩扮▼搴忕殑鍛戒护琛屽懡浠わ細
gcc test1_libmysqld.c -o test1_libmysqld -lz \
`/usr/local/mysql/bin/mysql_config --include --libmysqld-libs`
绀轰緥锛2
瑕佹兂妫楠岃绀轰緥锛屽垱寤轰竴涓笌MySQL婧愮洰褰曞悓绾х殑test2_libmysqld鐩綍銆傚皢test2_libmysqld.c婧愭枃浠跺拰GNUmakefile淇濆瓨鍒拌鐩綍锛屽苟鍦test2_libmysqld鐩綍涓嬭繍琛GNU make銆
test2_libmysqld.c
/*
* A simple example client, using the embedded MySQL server library
*/
#include <mysql.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
MYSQL *db_connect(const char *dbname);
void db_disconnect(MYSQL *db);
void db_do_query(MYSQL *db, const char *query);
const char *server_groups[] = {
"test2_libmysqld_SERVER", "embedded", "server", NULL
};
int
main(int argc, char **argv)
{
MYSQL *one, *two;
/* mysql_server_init() must be called before any other mysql
* functions.
*
* You can use mysql_server_init(0, NULL, NULL), and it
* initializes the server using groups = {
* "server", "embedded", NULL
* }.
*
* In your $HOME/.my.cnf file, you probably want to put:
[test2_libmysqld_SERVER]
language = /path/to/source/of/mysql/sql/share/english
* You could, of course, modify argc and argv before passing
* them to this function. Or you could create new ones in any
* way you like. But all of the arguments in argv (except for
* argv[0], which is the program name) should be valid options
* for the MySQL server.
*
* If you link this client against the normal mysqlclient
* library, this function is just a stub that does nothing.
*/
mysql_server_init(argc, argv, (char **)server_groups);
one = db_connect("test");
two = db_connect(NULL);
db_do_query(one, "SHOW TABLE STATUS");
db_do_query(two, "SHOW DATABASES");
mysql_close(two);
mysql_close(one);
/* This must be called after all other mysql functions */
mysql_server_end();
exit(EXIT_SUCCESS);
}
static void
die(MYSQL *db, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
(void)putc('\n', stderr);
if (db)
db_disconnect(db);
exit(EXIT_FAILURE);
}
MYSQL *
db_connect(const char *dbname)
{
MYSQL *db = mysql_init(NULL);
if (!db)
die(db, "mysql_init failed: no memory");
/*
* Notice that the client and server use separate group names.
* This is critical, because the server does not accept the
* client's options, and vice versa.
*/
mysql_options(db, MYSQL_READ_DEFAULT_GROUP, "test2_libmysqld_CLIENT");
if (!mysql_real_connect(db, NULL, NULL, NULL, dbname, 0, NULL, 0))
die(db, "mysql_real_connect failed: %s", mysql_error(db));
return db;
}
void
db_disconnect(MYSQL *db)
{
mysql_close(db);
}
void
db_do_query(MYSQL *db, const char *query)
{
if (mysql_query(db, query) != 0)
goto err;
if (mysql_field_count(db) > 0)
{
MYSQL_RES *res;
MYSQL_ROW row, end_row;
int num_fields;
if (!(res = mysql_store_result(db)))
goto err;
num_fields = mysql_num_fields(res);
while ((row = mysql_fetch_row(res)))
{
(void)fputs(">> ", stdout);
for (end_row = row + num_fields; row < end_row; ++row)
(void)printf("%s\t", row ? (char*)*row : "NULL");
(void)fputc('\n', stdout);
}
(void)fputc('\n', stdout);
mysql_free_result(res);
}
else
(void)printf("Affected rows: %lld\n", mysql_affected_rows(db));
return;
err:
die(db, "db_do_query failed: %s [%s]", mysql_error(db), query);
}
GNUmakefile
# This assumes the MySQL software is installed in /usr/local/mysql
inc := /usr/local/mysql/include/mysql
lib := /usr/local/mysql/lib
# If you have not installed the MySQL software yet, try this instead
#inc := $(HOME)/mysql-5.1/include
#lib := $(HOME)/mysql-5.1/libmysqld
CC := gcc
CPPFLAGS := -I$(inc) -D_THREAD_SAFE -D_REENTRANT
CFLAGS := -g -W -Wall
LDFLAGS := -static
# You can change -lmysqld to -lmysqlclient to use the
# client/server library
LDLIBS = -L$(lib) -lmysqld -lz -lm -lcrypt
ifneq (,$(shell grep FreeBSD /COPYRIGHT 2>/dev/null))
# FreeBSD
LDFLAGS += -pthread
else
# Assume Linux
LDLIBS += -lpthread
endif
# This works for simple one-file test programs
sources := $(wildcard *.c)
objects := $(patsubst %c,%o,$(sources))
targets := $(basename $(sources))
all: $(targets)
clean:
rm -f $(targets) $(objects) *.core
鎴戜滑榧撳姳鎵鏈変汉鍦GPL鎴栧吋瀹硅鍙殑鏃楀笢涓嬮氳繃鍙戝竷浠g爜鏉ユ帹骞垮厤璐硅蒋浠躲傚浜庢湁鑳藉姏瀹屾垚璇ョ被浜嬮」鐨勪汉鍛橈紝涔熷彲浠ラ夋嫨浠MySQL AB璐拱MySQL鐨勫晢鐢ㄨ鍙傝鎯呰鍙傝http://www.mysql.com/company/legal/licensing/銆
C API浠g爜鏄笌MySQL涓璧锋彁渚涚殑銆傚畠鍖呭惈鍦mysqlclient搴撲腑锛屽苟鍏佽C绋嬪簭璁块棶鏁版嵁搴撱
MySQL婧愮爜鍒嗗彂鐗堢殑寰堝瀹㈡埛绔槸鐢C璇█缂栧啓鐨勩傚鏋滀綘姝e湪瀵绘壘鑳芥紨绀哄浣曚娇鐢C API鐨勭ず渚嬶紝鍙弬鐪嬭繖浜涘鎴风绋嬪簭銆備綘鍙互鍦MySQL婧愮爜鍒嗗彂鐗堢殑瀹㈡埛绔洰褰曚笅鎵惧埌瀹冧滑銆
澶у鏁板叾浠栧鎴风API锛堥櫎浜Connector/J鍜Connector/NET锛夐噰鐢mysqlclient搴撴潵涓MySQL鏈嶅姟鍣ㄨ繘琛岄氫俊銆傝繖鎰忓懗鐫锛堜緥濡傦級锛屼綘鍙互鍒╃敤寰堝鐩稿悓鐜鍙橀噺锛堜笌鍏朵粬瀹㈡埛绔▼搴忎娇鐢ㄧ殑鐜鍙橀噺鐩稿悓锛夊甫鏉ョ殑濂藉锛岃繖鏄洜涓哄畠浠槸浠庡簱涓紩鐢ㄧ殑銆傚叧浜庤繖浜涘彉閲忕殑璇︾粏娓呭崟锛岃鍙傝绗8绔狅細瀹㈡埛绔拰瀹炵敤宸ュ叿绋嬪簭銆
瀹㈡埛绔叿鏈夋渶澶х殑閫氫俊缂撳啿鍖哄ぇ灏忋傚垵濮嬪垎閰嶇殑缂撳啿鍖哄ぇ灏忥紙16KB锛夊皢鑷姩澧炲姞鍒版渶澶э紙鏈澶т负16MB锛夈傜敱浜庣紦鍐插尯澶у皬灏嗘寜闇澧炲姞锛岀畝鍗曞湴澧炲姞榛樿鐨勬渶澶ч檺鍒讹紝浠庡叾鏈韩鏉ヨ涓嶄細澧炲姞璧勬簮浣跨敤銆傝澶у皬妫鏌ヤ富瑕佹槸妫鏌ラ敊璇煡璇㈠拰閫氫俊淇℃伅鍖呫
閫氫俊缂撳啿鍖哄繀椤昏冻澶熷ぇ锛岃冻浠ュ寘鍚1鏉SQL璇彞锛堢敤浜庡鎴风-鏈嶅姟鍣ㄩ氫俊锛変互鍙1琛岃繑鍥炵殑鏁版嵁锛堢敤浜庢湇鍔″櫒-瀹㈡埛绔氫俊锛夈傛瘡涓嚎绋嬬殑閫氫俊缂撳啿鍖哄皢鍔ㄦ佸鍔狅紝浠ュ鐞嗙洿鑷虫渶澶ч檺鍒剁殑浠讳綍鏌ヨ鎴栬銆備緥濡傦紝濡傛灉BLOB鍊煎寘鍚珮杈16MB鐨勬暟鎹紝閭d箞閫氫俊缂撳啿鍖虹殑澶у皬闄愬埗鑷冲皯涓16MB锛堝湪鏈嶅姟鍣ㄥ拰瀹㈡埛绔級銆傚鎴风鐨勯粯璁ゆ渶澶у间负16MB锛屼絾鏈嶅姟鍣ㄧ殑榛樿鏈澶у间负1MB銆備篃鍙互鍦ㄥ惎鍔ㄦ湇鍔″櫒鏃讹紝閫氳繃鏇存敼max_allowed_packet鍙傛暟鐨勫煎鍔犲畠銆璇峰弬瑙7.5.2鑺傦紝鈥滆皟鑺傛湇鍔″櫒鍙傛暟鈥銆
姣忔鏌ヨ鍚庯紝MySQL鏈嶅姟鍣ㄤ細灏嗛氫俊缂撳啿鍖虹殑澶у皬闄嶈嚦net_buffer_length瀛楄妭銆傚浜庡鎴风锛屼笉浼氶檷浣庝笌杩炴帴鐩稿叧缂撳啿鍖哄ぇ灏忥紝鐩磋嚦杩炴帴鍏抽棴涓烘锛屾鏃锛屽鎴风鍐呭瓨灏嗚鏀跺洖銆
鍏充簬浣跨敤绾跨▼鐨勭紪绋嬫柟娉曪紝璇峰弬瑙25.2.15鑺傦紝鈥滃浣曠敓鎴愮嚎绋嬪紡瀹㈡埛绔銆傚叧浜庡湪鐩稿悓绋嬪簭鍒涘缓鍖呭惈鈥滄湇鍔″櫒鈥濆拰鈥滃鎴风鈥濈殑鐙珛搴旂敤绋嬪簭鐨勬洿澶氫俊鎭紙涓嶄笌澶栭儴MySQL鏈嶅姟鍣ㄩ氫俊锛夛紝璇峰弬瑙25.1鑺傦紝鈥渓ibmysqld锛屽祵鍏ュ紡MySQL鏈嶅姟鍣ㄥ簱鈥銆
璇ョ粨鏋勪唬琛1涓暟鎹簱杩炴帴鐨勫彞鏌勩傚嚑涔庢墍鏈夌殑MySQL鍑芥暟鍧囦娇鐢ㄥ畠銆備笉搴斿皾璇曟嫹璐MYSQL缁撴瀯銆備笉淇濊瘉杩欑被鎷疯礉缁撴灉浼氭湁鐢ㄣ
璇ョ粨鏋勪唬琛ㄨ繑鍥炶鐨勬煡璇㈢粨鏋滐紙SELECT, SHOW, DESCRIBE, EXPLAIN锛夈傚湪鏈妭鐨勫墿浣欓儴鍒嗭紝灏嗘煡璇㈣繑鍥炵殑淇℃伅绉颁负鈥滅粨鏋滈泦鈥濄
杩欐槸1琛屾暟鎹殑鈥滅被鍨嬪畨鍏ㄢ濊〃绀恒傚畠鐩墠鏄寜鐓ц鏁板瓧鑺傚瓧绗︿覆鐨勬暟缁勫疄鏂界殑銆傦紙濡傛灉瀛楁鍊煎彲鑳藉寘鍚簩杩涘埗鏁版嵁锛屼笉鑳藉皢鍏跺綋浣鐢盢ull缁堢粨鐨勫瓧绗︿覆瀵瑰緟锛岃繖鏄洜涓鸿繖绫诲煎彲鑳戒細鍖呭惈Null瀛楄妭锛夈傝鏄氳繃璋冪敤mysql_fetch_row()鑾峰緱鐨勩
璇ョ粨鏋勫寘鍚叧浜庡瓧娈电殑淇℃伅锛屽瀛楁鍚嶃佺被鍨嬪拰澶у皬銆傝繖閲岃缁嗕粙缁嶄簡鍏舵垚鍛樸傞氳繃閲嶅璋冪敤mysql_fetch_field()锛屽彲涓烘瘡涓瓧娈佃幏寰MYSQL_FIELD缁撴瀯銆傚瓧娈靛间笉鏄缁撴瀯鐨勭粍鎴愰儴浠斤紝瀹冧滑鍖呭惈鍦MYSQL_ROW缁撴瀯涓
杩欐槸MySQL瀛楁鍒楄〃鍋忕Щ閲忕殑鈥滅被鍨嬪畨鍏ㄢ濊〃绀猴紙鐢mysql_field_seek()浣跨敤锛夈傚亸绉婚噺鏄鍐呯殑瀛楁缂栧彿锛屼粠0寮濮嬨
鐢ㄤ簬琛屾暟浠ュ強mysql_affected_rows()銆mysql_num_rows()鍜mysql_insert_id()鐨勭被鍨嬨傝绫诲瀷鎻愪緵鐨勮寖鍥翠负0锝1.84e19銆
鍦ㄦ煇浜涚郴缁熶笂锛屼笉鑳芥墦鍗扮被鍨my_ulonglong鐨勫笺傝鎯虫墦鍗拌繖绫诲硷紝璇峰皢鍏惰浆鎹负鏃犵鍙烽暱鏁存暟绫诲瀷骞朵娇鐢%lu鎵撳嵃鏍煎紡锛屼緥濡傦細
printf ("Number of rows: %lu\n", (unsigned long) mysql_num_rows(result));
涓嬮潰鍒楀嚭浜MYSQL_FIELD缁撴瀯鍖呭惈鐨勬垚鍛橈細
路 char * name
瀛楁鍚嶇О锛岀敱Null缁堢粨鐨勫瓧绗︿覆銆傚鏋滅敤AS瀛愬彞涓鸿瀛楁鎸囧畾浜嗗埆鍚嶏紝鍚嶇О鐨勫间篃鏄埆鍚嶃
路 char * org_name
娈靛悕绉帮紝鐢Null缁堢粨鐨勫瓧绗︿覆銆傚拷鐣ュ埆鍚嶃
路 char * table
鍖呭惈璇ュ瓧娈电殑琛ㄧ殑鍚嶇О锛屽鏋滆瀛楁涓嶆槸璁$畻鍑虹殑瀛楁鐨勮瘽銆傚浜庤绠楀嚭鐨勫瓧娈碉紝琛ㄥ间负绌虹殑瀛楃涓层傚鏋滅敤AS瀛愬彞涓鸿琛ㄦ寚瀹氫簡鍒悕锛岃〃鐨勫间篃鏄埆鍚嶃
路 char * org_table
琛ㄧ殑鍚嶇О锛岀敱Null缁堢粨鐨勫瓧绗︿覆銆傚拷鐣ュ埆鍚嶃
路 char * db
瀛楁婧愯嚜鐨勬暟鎹殑鍚嶇О锛岀敱Null缁堢粨鐨勫瓧绗︿覆銆傚鏋滆瀛楁鏄绠楀嚭鐨勫瓧娈碉紝db涓虹┖鐨勫瓧绗︿覆銆
路 char * catalog
catalog鍚嶇О銆傝鍊兼绘槸"def"銆
路 char * def
璇ュ瓧娈电殑榛樿鍊硷紝鐢Null缁堢粨鐨勫瓧绗︿覆銆備粎褰撲娇鐢mysql_list_fields()鏃舵墠璁剧疆瀹冦
路 unsigned long length
瀛楁鐨勫搴︼紝濡傝〃瀹氫箟涓墍鎸囧畾鐨勯偅鏍枫
路 unsigned long max_length
鐢ㄤ簬缁撴灉闆嗙殑瀛楁鐨勬渶澶у搴︼紙瀵逛簬瀹為檯浣嶄簬缁撴灉闆嗕腑鐨勮锛屾渶闀垮瓧娈靛肩殑闀垮害锛夈傚鏋滀娇鐢mysql_store_result()鎴mysql_list_fields()锛屽畠灏嗗寘鍚瓧娈电殑鏈澶ч暱搴︺傚鏋滀娇鐢mysql_use_result()锛岃鍙橀噺鐨勫间负0銆
路 unsigned int name_length
鍚嶇О鐨勯暱搴︺
路 unsigned int org_name_length
org_name鐨勯暱搴︺
路 unsigned int table_length
琛ㄧ殑闀垮害銆
路 unsigned int org_table_length
org_table鐨勯暱搴︺
路 unsigned int db_length
db鐨勯暱搴︺
路 unsigned int catalog_length
catalog鐨勯暱搴︺
路 unsigned int def_length
def鐨勯暱搴︺
路 unsigned int flags
鐢ㄤ簬瀛楁鐨勪笉鍚屸滀綅鏍囧織鈥濄傛爣蹇楃殑鍊煎彲浠ユ湁0涓垨澶氫釜涓嬭堪浣嶉泦鍚堬細
鏍囧織鍊 |
鏍囧織鎻忚堪 |
NOT_NULL_FLAG |
瀛楁涓嶈兘涓NULL |
PRI_KEY_FLAG |
瀛楁鏄富閿殑缁勬垚閮ㄥ垎 |
UNIQUE_KEY_FLAG |
瀛楁鏄敮涓閿殑缁勬垚閮ㄥ垎 |
MULTIPLE_KEY_FLAG |
瀛楁鏄潪鍞竴閿殑缁勬垚閮ㄥ垎 |
UNSIGNED_FLAG |
瀛楁鍏锋湁UNSIGNED灞炴 |
ZEROFILL_FLAG |
瀛楁鍏锋湁ZEROFILL灞炴 |
BINARY_FLAG |
瀛楁鍏锋湁BINARY灞炴 |
AUTO_INCREMENT_FLAG |
瀛楁鍏锋湁AUTO_INCREMENT灞炴 |
ENUM_FLAG |
瀛楁鏄ENUM锛堜笉鍐嶉噸瑙嗭級 |
SET_FLAG |
瀛楁鏄 SET锛堜笉鍐嶉噸瑙嗭級 |
BLOB_FLAG |
瀛楁鏄BLOB鎴TEXT锛堜笉鍐嶉噸瑙嗭級 |
TIMESTAMP_FLAG |
瀛楁鏄TIMESTAMP锛堜笉鍐嶉噸瑙嗭級 |
涓嶅啀閲嶈BLOB_FLAG銆ENUM_FLAG銆SET_FLAG鍜TIMESTAMP_FLAG鏍囧織锛屽師鍥犲湪浜庯紝瀹冧滑鎸囧嚭浜嗗瓧娈电殑绫诲瀷锛岃屼笉鏄被鍨嬬殑灞炴с傛洿鍙彇鐨勬柟寮忔槸浣跨敤MYSQL_TYPE_BLOB銆MYSQL_TYPE_ENUM銆MYSQL_TYPE_SET鎴MYSQL_TYPE_TIMESTAMP娴嬭瘯field->type銆
鍦ㄤ笅闈㈢殑绀轰緥涓紝浠嬬粛浜嗘爣蹇楀肩殑鍏稿瀷鐢ㄦ硶锛
if (field->flags & NOT_NULL_FLAG)
printf("Field can't be null\n");
鍙互浣跨敤涓嬭堪鏂归潰鐨勫畯鏉ュ畾涔夋爣蹇楀肩殑甯冨皵鐘舵侊細
鏍囧織鐘舵 |
鎻忚堪 |
IS_NOT_NULL(flags) |
濡傛灉璇ュ瓧娈靛畾涔変负NOT NULL锛屼负鈥鐪鈥銆 |
IS_PRI_KEY(flags) |
濡傛灉璇ュ瓧娈垫槸涓婚敭锛屼负鈥鐪鈥銆 |
IS_BLOB(flags) |
濡傛灉璇ュ瓧娈垫槸BLOB鎴TEXT锛屼负鈥鐪鈥锛堜笉鍐嶉噸瑙锛岀敤娴嬭瘯field->type鍙栬屼唬涔嬶級銆 |
路 unsigned int decimals
鐢ㄤ簬鏁板煎瓧娈电殑鍗佽繘鍒舵暟鏁扮洰銆
路 unsigned int charset_nr
鐢ㄤ簬瀛楁鐨勫瓧绗﹂泦缂栧彿銆
路 enum enum_field_types type
瀛楁鐨勭被鍨嬨傜被鍨嬪煎彲浠ユ槸涓嬫爣鎵鍒楃殑MYSQL_TYPE_绗﹀彿涔嬩竴锛
绫诲瀷鍊 |
绫诲瀷鎻忚堪 |
MYSQL_TYPE_TINY |
TINYINT瀛楁 |
MYSQL_TYPE_SHORT |
SMALLINT瀛楁 |
MYSQL_TYPE_LONG |
INTEGER瀛楁 |
MYSQL_TYPE_INT24 |
MEDIUMINT瀛楁 |
MYSQL_TYPE_LONGLONG |
BIGINT瀛楁 |
MYSQL_TYPE_DECIMAL |
DECIMAL鎴NUMERIC瀛楁 |
MYSQL_TYPE_NEWDECIMAL |
绮惧害鏁板DECIMAL鎴NUMERIC |
MYSQL_TYPE_FLOAT |
FLOAT瀛楁 |
MYSQL_TYPE_DOUBLE |
DOUBLE鎴REAL瀛楁 |
MYSQL_TYPE_BIT |
BIT瀛楁 |
MYSQL_TYPE_TIMESTAMP |
TIMESTAMP瀛楁 |
MYSQL_TYPE_DATE |
DATE瀛楁 |
MYSQL_TYPE_TIME |
TIME瀛楁 |
MYSQL_TYPE_DATETIME |
DATETIME瀛楁 |
MYSQL_TYPE_YEAR |
YEAR瀛楁 |
MYSQL_TYPE_STRING |
CHAR瀛楁 |
MYSQL_TYPE_VAR_STRING |
VARCHAR瀛楁 |
MYSQL_TYPE_BLOB |
BLOB鎴TEXT瀛楁锛堜娇鐢max_length鏉ョ‘瀹氭渶澶ч暱搴︼級 |
MYSQL_TYPE_SET |
SET瀛楁 |
MYSQL_TYPE_ENUM |
ENUM瀛楁 |
MYSQL_TYPE_GEOMETRY |
Spatial瀛楁 |
MYSQL_TYPE_NULL |
NULL-type瀛楁 |
MYSQL_TYPE_CHAR |
涓嶅啀閲嶈锛岀敤MYSQL_TYPE_TINY鍙栦唬 |
鍙互浣跨敤IS_NUM()瀹忔潵娴嬭瘯瀛楁鏄惁鍏锋湁鏁板肩被鍨嬨傚皢绫诲瀷鍊间紶閫掔粰IS_NUM()锛濡傛灉瀛楁涓烘暟鍊肩被鍨嬶紝浼氬皢鍏惰瘎浼颁负鈥滅湡鈥濓細
if (IS_NUM(field->type))
printf("Field is numeric\n");
杩欓噷褰掔撼浜C API鍙娇鐢ㄧ殑鍑芥暟锛屽苟鍦ㄤ笅涓鑺傝缁嗕粙缁嶄簡瀹冧滑銆傝鍙傝25.2.3鑺傦紝鈥淐 API鍑芥暟鎻忚堪鈥銆
鍑芥暟 |
鎻忚堪 |
mysql_affected_rows() |
杩斿洖涓婃UPDATE銆DELETE鎴INSERT鏌ヨ鏇存敼锛忓垹闄わ紡鎻掑叆鐨勮鏁般 |
mysql_autocommit() |
鍒囨崲 autocommit妯″紡锛ON/OFF |
mysql_change_user() |
鏇存敼鎵撳紑杩炴帴涓婄殑鐢ㄦ埛鍜屾暟鎹簱銆 |
mysql_charset_name() |
杩斿洖鐢ㄤ簬杩炴帴鐨勯粯璁ゅ瓧绗﹂泦鐨勫悕绉般 |
mysql_close() |
鍏抽棴鏈嶅姟鍣ㄨ繛鎺ャ |
mysql_commit() |
鎻愪氦浜嬪姟銆 |
mysql_connect() |
杩炴帴鍒MySQL鏈嶅姟鍣ㄣ傝鍑芥暟宸蹭笉鍐嶈閲嶈锛屼娇鐢mysql_real_connect()鍙栦唬銆 |
mysql_create_db() |
鍒涘缓鏁版嵁搴撱傝鍑芥暟宸蹭笉鍐嶈閲嶈锛屼娇鐢SQL璇彞CREATE DATABASE鍙栬屼唬涔嬨 |
mysql_data_seek() |
鍦ㄦ煡璇㈢粨鏋滈泦涓煡鎵惧睘鎬ц缂栧彿銆 |
mysql_debug() |
鐢ㄧ粰瀹氱殑瀛楃涓叉墽琛DBUG_PUSH銆 |
mysql_drop_db() |
鎾ら攢鏁版嵁搴撱傝鍑芥暟宸蹭笉鍐嶈閲嶈锛屼娇鐢SQL璇彞DROP DATABASE鍙栬屼唬涔嬨 |
mysql_dump_debug_info() |
璁╂湇鍔″櫒灏嗚皟璇曚俊鎭啓鍏ユ棩蹇椼 |
mysql_eof() |
纭畾鏄惁璇诲彇浜嗙粨鏋滈泦鐨勬渶鍚庝竴琛屻傝鍑芥暟宸蹭笉鍐嶈閲嶈锛屽彲浠ヤ娇鐢mysql_errno()鎴mysql_error()鍙栬屼唬涔嬨 |
mysql_errno() |
杩斿洖涓婃璋冪敤鐨MySQL鍑芥暟鐨勯敊璇紪鍙枫 |
mysql_error() |
杩斿洖涓婃璋冪敤鐨MySQL鍑芥暟鐨勯敊璇秷鎭 |
mysql_escape_string() |
涓轰簡鐢ㄥ湪SQL璇彞涓紝瀵圭壒娈婂瓧绗﹁繘琛岃浆涔夊鐞嗐 |
mysql_fetch_field() |
杩斿洖涓嬩竴涓〃瀛楁鐨勭被鍨嬨 |
mysql_fetch_field_direct() |
缁欏畾瀛楁缂栧彿锛岃繑鍥炶〃瀛楁鐨勭被鍨嬨 |
mysql_fetch_fields() |
杩斿洖鎵鏈夊瓧娈电粨鏋勭殑鏁扮粍銆 |
mysql_fetch_lengths() |
杩斿洖褰撳墠琛屼腑鎵鏈夊垪鐨勯暱搴︺ |
mysql_fetch_row() |
浠庣粨鏋滈泦涓幏鍙栦笅涓琛 |
mysql_field_seek() |
灏嗗垪鍏夋爣缃簬鎸囧畾鐨勫垪銆 |
mysql_field_count() |
杩斿洖涓婃鎵ц璇彞鐨勭粨鏋滃垪鐨勬暟鐩 |
mysql_field_tell() |
杩斿洖涓婃mysql_fetch_field()鎵浣跨敤瀛楁鍏夋爣鐨勪綅缃 |
mysql_free_result() |
閲婃斁缁撴灉闆嗕娇鐢ㄧ殑鍐呭瓨銆 |
mysql_get_client_info() |
浠ュ瓧绗︿覆褰㈠紡杩斿洖瀹㈡埛绔増鏈俊鎭 |
mysql_get_client_version() |
浠ユ暣鏁板舰寮忚繑鍥炲鎴风鐗堟湰淇℃伅銆 |
mysql_get_host_info() |
杩斿洖鎻忚堪杩炴帴鐨勫瓧绗︿覆銆 |
mysql_get_server_version() |
浠ユ暣鏁板舰寮忚繑鍥炴湇鍔″櫒鐨勭増鏈彿銆 |
mysql_get_proto_info() |
杩斿洖杩炴帴鎵浣跨敤鐨勫崗璁増鏈 |
mysql_get_server_info() |
杩斿洖鏈嶅姟鍣ㄧ殑鐗堟湰鍙枫 |
mysql_info() |
杩斿洖鍏充簬鏈杩戞墍鎵ц鏌ヨ鐨勪俊鎭 |
mysql_init() |
鑾峰彇鎴栧垵濮嬪寲MYSQL缁撴瀯銆 |
mysql_insert_id() |
杩斿洖涓婁竴涓煡璇负AUTO_INCREMENT鍒楃敓鎴愮殑ID銆 |
mysql_kill() |
鏉姝荤粰瀹氱殑绾跨▼銆 |
mysql_library_end() |
鏈缁堢‘瀹MySQL C API搴撱 |
mysql_library_init() |
鍒濆鍖MySQL C API搴撱 |
mysql_list_dbs() |
杩斿洖涓庣畝鍗曟鍒欒〃杈惧紡鍖归厤鐨勬暟鎹簱鍚嶇О銆 |
mysql_list_fields() |
杩斿洖涓庣畝鍗曟鍒欒〃杈惧紡鍖归厤鐨勫瓧娈靛悕绉般 |
mysql_list_processes() |
杩斿洖褰撳墠鏈嶅姟鍣ㄧ嚎绋嬬殑鍒楄〃銆 |
mysql_list_tables() |
杩斿洖涓庣畝鍗曟鍒欒〃杈惧紡鍖归厤鐨勮〃鍚嶃 |
mysql_more_results() |
妫鏌ユ槸鍚﹁繕瀛樺湪鍏朵粬缁撴灉銆 |
mysql_next_result() |
鍦ㄥ璇彞鎵ц杩囩▼涓繑鍥/鍒濆鍖栦笅涓涓粨鏋溿 |
mysql_num_fields() |
杩斿洖缁撴灉闆嗕腑鐨勫垪鏁般 |
mysql_num_rows() |
杩斿洖缁撴灉闆嗕腑鐨勮鏁般 |
mysql_options() |
涓mysql_connect()璁剧疆杩炴帴閫夐」銆 |
mysql_ping() |
妫鏌ヤ笌鏈嶅姟鍣ㄧ殑杩炴帴鏄惁宸ヤ綔锛屽鏈夊繀瑕侀噸鏂拌繛鎺ャ |
mysql_query() |
鎵ц鎸囧畾涓衡滀互Null缁堢粨鐨勫瓧绗︿覆鈥濈殑SQL鏌ヨ銆 |
mysql_real_connect() |
杩炴帴鍒MySQL鏈嶅姟鍣ㄣ |
mysql_real_escape_string() |
鑰冭檻鍒拌繛鎺ョ殑褰撳墠瀛楃闆嗭紝涓轰簡鍦SQL璇彞涓娇鐢紝瀵瑰瓧绗︿覆涓殑鐗规畩瀛楃杩涜杞箟澶勭悊銆 |
mysql_real_query() |
鎵ц鎸囧畾涓鸿鏁板瓧绗︿覆鐨SQL鏌ヨ銆 |
mysql_refresh() |
鍒锋柊鎴栧浣嶈〃鍜岄珮閫熺紦鍐层 |
mysql_reload() |
閫氱煡鏈嶅姟鍣ㄥ啀娆″姞杞芥巿鏉冭〃銆 |
mysql_rollback() |
鍥炴粴浜嬪姟銆 |
mysql_row_seek() |
浣跨敤浠mysql_row_tell()杩斿洖鐨勫硷紝鏌ユ壘缁撴灉闆嗕腑鐨勮鍋忕Щ銆 |
mysql_row_tell() |
杩斿洖琛屽厜鏍囦綅缃 |
mysql_select_db() |
閫夋嫨鏁版嵁搴撱 |
mysql_server_end() |
鏈缁堢‘瀹氬祵鍏ュ紡鏈嶅姟鍣ㄥ簱銆 |
mysql_server_init() |
鍒濆鍖栧祵鍏ュ紡鏈嶅姟鍣ㄥ簱銆 |
mysql_set_server_option() |
涓鸿繛鎺ヨ缃夐」锛堝澶氳鍙ワ級銆 |
mysql_sqlstate() |
杩斿洖鍏充簬涓婁竴涓敊璇殑SQLSTATE閿欒浠g爜銆 |
mysql_shutdown() |
鍏抽棴鏁版嵁搴撴湇鍔″櫒銆 |
mysql_stat() |
浠ュ瓧绗︿覆褰㈠紡杩斿洖鏈嶅姟鍣ㄧ姸鎬併 |
mysql_store_result() |
妫绱㈠畬鏁寸殑缁撴灉闆嗚嚦瀹㈡埛绔 |
mysql_thread_id() |
杩斿洖褰撳墠绾跨▼ID銆 |
mysql_thread_safe() |
濡傛灉瀹㈡埛绔凡缂栬瘧涓虹嚎绋嬪畨鍏ㄧ殑锛岃繑鍥1銆 |
mysql_use_result() |
鍒濆鍖栭愯鐨勭粨鏋滈泦妫绱€ |
mysql_warning_count() |
杩斿洖涓婁竴涓SQL璇彞鐨勫憡璀︽暟銆 |
涓MySQL浜や簰鏃讹紝搴旂敤绋嬪簭搴斾娇鐢ㄨ涓鑸у師鍒欙細
1. 閫氳繃璋冪敤mysql_library_init()锛屽垵濮嬪寲MySQL搴撱傚簱鍙互鏄mysqlclient C瀹㈡埛绔簱锛屾垨mysqld宓屽叆寮忔湇鍔″櫒搴擄紝鍏蜂綋鎯呭喌鍙栧喅浜庡簲鐢ㄧ▼搴忔槸鍚︿笌鈥-libmysqlclient鈥鎴栤-libmysqld鈥鏍囧織閾炬帴銆
2. 閫氳繃璋冪敤mysql_init()鍒濆鍖栬繛鎺ュ鐞嗙▼搴忥紝骞堕氳繃璋冪敤mysql_real_connect()杩炴帴鍒版湇鍔″櫒銆
3. 鍙戝嚭SQL璇彞骞跺鐞嗗叾缁撴灉銆傦紙鍦ㄤ笅闈㈢殑璁ㄨ涓紝璇︾粏浠嬬粛浜嗕娇鐢ㄥ畠鐨勬柟娉曪級銆
4. 閫氳繃璋冪敤mysql_close()锛屽叧闂涓MySQL鏈嶅姟鍣ㄧ殑杩炴帴銆
5. 閫氳繃璋冪敤mysql_library_end()锛岀粨鏉MySQL搴撶殑浣跨敤銆
璋冪敤mysql_library_init()鍜mysql_library_end()鐨勭洰鐨勫湪浜庯紝涓MySQL搴撴彁渚涙伆褰撶殑鍒濆鍖栧拰缁撴潫澶勭悊銆傚浜庝笌瀹㈡埛绔簱閾炬帴鐨勫簲鐢ㄧ▼搴忥紝瀹冧滑鎻愪緵浜嗘敼杩涚殑鍐呭瓨绠$悊鍔熻兘銆傚鏋滀笉璋冪敤mysql_library_end()锛屽唴瀛樺潡浠嶅皢淇濇寔鍒嗛厤鐘舵侊紙杩欎笉浼氬鍔犲簲鐢ㄧ▼搴忎娇鐢ㄧ殑鍐呭瓨閲忥紝浣嗘煇浜涘唴瀛樻硠婕忔娴嬪櫒灏嗘姉璁畠锛銆傚浜庝笌宓屽叆寮忔湇鍔″櫒閾炬帴鐨勫簲鐢ㄧ▼搴忥紝杩欎簺璋冪敤浼氬惎鍔ㄥ苟鍋滄鏈嶅姟鍣ㄣ
mysql_library_init()鍜mysql_library_end()瀹為檯涓婃槸#define绗﹀彿锛岃繖绫荤鍙蜂娇寰楀畠浠瓑鏁堜簬mysql_server_init()鍜mysql_server_end()锛屼絾鍏跺悕绉版洿娓呮鍦版寚鏄庯紝鏃犺搴旂敤绋嬪簭浣跨敤鐨勬槸mysqlclient鎴mysqld搴擄紝鍚姩鎴栫粨鏉MySQL搴撴椂锛屽簲璋冪敤瀹冧滑銆瀵逛簬鏃╂湡鐨MySQL鐗堟湰锛屽彲璋冪敤mysql_server_init()鍜mysql_server_end()鍙栬屼唬涔嬨
濡傛灉鎰挎剰锛屽彲鐪佺暐瀵mysql_library_init()鐨勮皟鐢紝杩欐槸鍥犱负锛屽繀瑕佹椂锛mysql_init()浼氳嚜鍔ㄨ皟鐢ㄥ畠銆
瑕佹兂杩炴帴鍒版湇鍔″櫒锛屽彲璋冪敤mysql_init()鏉ュ垵濮嬪寲杩炴帴澶勭悊绋嬪簭锛岀劧鍚庣敤璇ュ鐞嗙▼搴忥紙浠ュ強鍏朵粬淇℃伅锛屽涓绘満鍚嶃佺敤鎴峰悕鍜屽瘑鐮侊級璋冪敤mysql_real_connect()銆傚缓绔嬭繛鎺ュ悗锛屽湪浣庝簬5.0.3鐗堢殑API涓紝mysql_real_connect()浼氬皢鍐嶈繛鎺ユ爣蹇楋紙MYSQL缁撴瀯鐨勪竴閮ㄥ垎锛夎缃负1锛屾垨鍦ㄨ緝鏂扮殑鐗堟湰涓紝灏嗗叾璁剧疆涓0銆傚浜庤鏍囧織锛屽尖1鈥濇寚鏄庯紝濡傛灉鍥犺繛鎺ヤ涪澶辫屾棤娉曟墽琛岃鍙ワ紝鏀惧純涔嬪墠锛屼細灏濊瘯鍐嶆杩炴帴鍒版湇鍔″櫒銆備粠MySQL 5.0.13寮濮嬶紝鍙互鍦mysql_options()涓婁娇鐢MYSQL_OPT_RECONNECT閫夐」锛屼互鎺у埗鍐嶈繛鎺ヨ涓恒傚畬鎴愯繛鎺ュ悗锛岃皟鐢mysql_close()涓瀹冦
褰撹繛鎺ュ浜庢椿鍔ㄧ姸鎬佹椂锛屽鎴风鎴栬浼氫娇鐢mysql_query()鎴mysql_real_query()鍚戞湇鍔″櫒鍙戝嚭SQL鏌ヨ銆備袱鑰呯殑宸埆鍦ㄤ簬锛mysql_query()棰勬湡鐨勬煡璇负鎸囧畾鐨勩佺敱Null缁堢粨鐨勫瓧绗︿覆锛岃mysql_real_query()棰勬湡鐨勬槸璁℃暟瀛楃涓层傚鏋滃瓧绗︿覆鍖呭惈浜岃繘鍒舵暟鎹紙鍏朵腑鍙兘鍖呭惈Null瀛楄妭锛夛紝灏卞繀椤讳娇鐢mysql_real_query()銆
瀵逛簬姣忎釜闈SELECT鏌ヨ锛堜緥濡INSERT銆UPDATE銆DELETE锛夛紝閫氳繃璋冪敤mysql_affected_rows()锛屽彲鍙戠幇鏈夊灏戣宸茶鏀瑰彉锛堝奖鍝嶏級銆
瀵逛簬SELECT鏌ヨ锛鑳藉妫绱綔涓虹粨鏋滈泦鐨勮銆傛敞鎰忥紝鏌愪簺璇彞鍥犲叾杩斿洖琛岋紝绫讳技涓SELECT銆鍖呮嫭SHOW銆DESCRIBE鍜EXPLAIN銆傚簲鎸夌収瀵瑰緟SELECT璇彞鐨勬柟寮忓鐞嗗畠浠
瀹㈡埛绔鐞嗙粨鏋滈泦鐨勬柟寮忔湁涓ょ銆備竴绉嶆柟寮忔槸锛岄氳繃璋冪敤mysql_store_result()锛屼竴娆℃у湴妫绱㈡暣涓粨鏋滈泦銆傝鍑芥暟鑳戒粠鏈嶅姟鍣ㄨ幏寰楁煡璇㈣繑鍥炵殑鎵鏈夎锛屽苟灏嗗畠浠繚瀛樺湪瀹㈡埛绔傜浜岀鏂瑰紡鏄拡瀵瑰鎴风鐨勶紝閫氳繃璋冪敤mysql_use_result()锛屽鈥鎸夎鈥缁撴灉闆嗘绱㈣繘琛屽垵濮嬪寲澶勭悊銆傝鍑芥暟鑳藉垵濮嬪寲妫绱㈢粨鏋滐紝浣嗕笉鑳戒粠鏈嶅姟鍣ㄨ幏寰椾换浣曞疄闄呰銆
鍦ㄨ繖涓ょ鎯呭喌涓嬶紝鍧囪兘閫氳繃璋冪敤mysql_fetch_row()璁块棶琛屻傞氳繃mysql_store_result()锛mysql_fetch_row()鑳藉璁块棶浠ュ墠浠庢湇鍔″櫒鑾峰緱鐨勮銆傞氳繃mysql_use_result()锛mysql_fetch_row()鑳藉瀹為檯鍦版绱㈡潵鑷湇鍔″櫒鐨勮銆傞氳繃璋冪敤mysql_fetch_lengths()锛岃兘鑾峰緱鍏充簬鍚勮涓暟鎹ぇ灏忕殑淇℃伅銆
瀹屾垚缁撴灉闆嗘搷浣滃悗锛岃璋冪敤mysql_free_result()閲婃斁缁撴灉闆嗕娇鐢ㄧ殑鍐呭瓨銆
杩欎袱绉嶆绱㈡満鍒舵槸浜掕ˉ鐨勩傚鎴风绋嬪簭搴旈夋嫨鏈鑳芥弧瓒冲叾瑕佹眰鐨勬柟娉曘傚疄闄呬笂锛屽鎴风鏈甯镐娇鐢ㄧ殑鏄mysql_store_result()銆
mysql_store_result()鐨1涓紭鐐瑰湪浜庯紝鐢变簬灏嗚鍏ㄩ儴鎻愬彇鍒颁簡瀹㈡埛绔笂锛屼綘涓嶄粎鑳借繛缁闂锛岃繕鑳戒娇鐢mysql_data_seek()鎴mysql_row_seek()鍦ㄧ粨鏋滈泦涓悜鍓嶆垨鍚戝悗绉诲姩锛屼互鏇存敼缁撴灉闆嗗唴褰撳墠琛岀殑浣嶇疆銆傞氳繃璋冪敤mysql_num_rows()锛岃繕鑳藉彂鐜版湁澶氬皯琛屻傚彟涓鏂归潰锛屽浜庡ぇ鐨勭粨鏋滈泦锛mysql_store_result()鎵闇鐨勫唴瀛樺彲鑳戒細寰堝ぇ锛屼綘寰堝彲鑳介亣鍒板唴瀛樻孩鍑虹姸鍐点
mysql_use_result()鐨1涓紭鐐瑰湪浜庯紝瀹㈡埛绔墍闇鐨勭敤浜缁撴灉闆嗙殑鍐呭瓨杈冨皯锛屽師鍥犲湪浜庯紝涓娆″畠浠呯淮鎶や竴琛岋紙鐢变簬鍒嗛厤寮閿杈冧綆锛mysql_use_result()鑳芥洿蹇锛夈傚畠鐨勭己鐐瑰湪浜庯紝浣犲繀椤诲揩閫熷鐞嗘瘡涓琛屼互閬垮厤濡ㄧ鏈嶅姟鍣紝浣犱笉鑳介殢鏈鸿闂粨鏋滈泦涓殑琛岋紙鍙兘杩炵画璁块棶琛岋級锛屼綘涓嶇煡閬撶粨鏋滈泦涓湁澶氬皯琛岋紝鐩磋嚦鍏ㄩ儴妫绱簡瀹冧滑涓烘銆備笉浠呭姝わ紝鍗充娇鍦ㄦ绱㈣繃绋嬩腑浣犲垽瀹氬凡鎵惧埌鎵瀵绘壘鐨勪俊鎭紝涔熷繀椤绘绱㈡墍鏈夌殑琛屻
閫氳繃API锛屽鎴风鑳藉鎭板綋鍦板鏌ヨ浣滃嚭鍝嶅簲锛堜粎鍦ㄥ繀瑕佹椂妫绱㈣锛夛紝鑰屾棤闇鐭ラ亾鏌ヨ鏄惁鏄SELECT鏌ヨ銆傚彲浠ュ湪姣忔mysql_query()鎴mysql_real_query()鍚庯紝閫氳繃璋冪敤mysql_store_result()瀹屾垚璇ユ搷浣溿傚鏋滅粨鏋滈泦璋冪敤鎴愬姛锛鏌ヨ涓SELECT锛岃屼笖鑳藉璇诲彇琛屻濡傛灉缁撴灉闆嗚皟鐢ㄥけ璐ワ紝鍙皟鐢mysql_field_count()鏉ュ垽鏂粨鏋滄槸鍚︾殑纭槸鎵棰勬湡鐨勩傚鏋mysql_field_count()杩斿洖0锛鏌ヨ涓嶈繑鍥炴暟鎹紙琛ㄦ槑瀹冩槸INSERT銆UPDATE銆DELETE绛夛級锛鑰屼笖涓嶈繑鍥炶銆傚鏋mysql_field_count()鏄潪0鍊硷紝鏌ヨ搴旇繑鍥炶锛屼絾娌℃湁杩斿洖琛屻傝繖琛ㄦ槑鏌ヨ鏄け璐ヤ簡鐨SELECT銆傚叧浜庡浣曞疄鐜拌鎿嶄綔鐨勭ず渚嬶紝璇峰弬瑙佸叧浜mysql_field_count()鐨勪粙缁嶃
鏃犺鏄mysql_store_result()杩樻槸mysql_use_result()锛屽潎鍏佽浣犺幏鍙栧叧浜庢瀯鎴愮粨鏋滈泦鐨勫瓧娈电殑淇℃伅锛堝瓧娈垫暟鐩紝瀹冧滑鐨勫悕绉板拰绫诲瀷绛夛級銆傞氳繃閲嶅璋冪敤mysql_fetch_field()锛屽彲浠ユ寜椤哄簭璁块棶琛屽唴鐨勫瓧娈典俊鎭紝鎴栬咃紝閫氳繃璋冪敤mysql_fetch_field_direct()锛岃兘澶熷湪琛屽唴鎸夊瓧娈电紪鍙疯闂瓧娈典俊鎭傞氳繃璋冪敤mysql_field_seek()锛屽彲浠ユ敼鍙樺綋鍓嶅瓧娈电殑鍏夋爣浣嶇疆銆傚瀛楁鍏夋爣鐨勮缃皢褰卞搷鍚庣画鐨mysql_fetch_field()璋冪敤銆傛澶栵紝浣犱篃鑳介氳繃璋冪敤mysql_fetch_fields()锛屼竴娆℃у湴鑾峰緱鍏充簬瀛楁鐨勬墍鏈変俊鎭
涓轰簡妫娴嬪拰閫氭姤閿欒锛MySQL鎻愪緵浜嗕娇鐢mysql_errno()鍜mysql_error()鍑芥暟璁块棶閿欒淇℃伅鐨勬満鍒躲傚畠浠兘杩斿洖鍏充簬鏈杩戣皟鐢ㄧ殑鍑芥暟鐨勯敊璇唬鐮佹垨閿欒娑堟伅锛屾渶杩戣皟鐢ㄧ殑鍑芥暟鍙兘鎴愬姛涔熷彲鑳藉け璐ワ紝杩欐牱锛屼綘灏辫兘鍒ゆ柇閿欒鏄湪浣曟椂鍑虹幇鐨勶紝浠ュ強閿欒鏄粈涔堛
鍦ㄦ湰鑺傛墍浣滅殑浠嬬粛涓紝鎸夌収C缂栫▼璇█锛涓篘ULL鐨勫弬鏁版垨杩斿洖鍊艰〃绀NULL锛岃屼笉鏄MySQL Null鍊笺
杩斿洖鍊肩殑鍑芥暟閫氬父浼氳繑鍥炴寚閽堟垨鏁存暟銆傞櫎闈炰綔浜嗗叾浠栬瀹氾紝杩斿洖鎸囬拡鐨勫嚱鏁板皢杩斿洖闈Null鍊硷紝浠ユ寚鏄庢垚鍔燂紝鎴栬繑鍥NULL鍊间互鎸囨槑鍑洪敊銆傝繑鍥炴暣鏁扮殑鍑芥暟灏嗚繑鍥0浠ユ寚鏄庢垚鍔燂紝鎴栬繑鍥為潪0鍊间互鎸囨槑鍑洪敊銆傛敞鎰忥紝闈0鍊间粎琛ㄦ槑杩欑偣銆傞櫎闈炲湪鍑芥暟鎻忚堪涓綔浜嗗叾浠栬鏄庯紝涓嶈瀵归潪0鍊艰繘琛屾祴璇曪細
if (result) /* correct */
... error ...
if (result < 0) /* incorrect */
... error ...
if (result == -1) /* incorrect */
... error ...
褰撳嚱鏁拌繑鍥為敊璇椂锛屽湪鍑芥暟鎻忚堪鐨勨滈敊璇濋儴鍒嗗皢鍒楀嚭鍙兘鐨勯敊璇被鍨嬨傞氳繃璋冪敤mysql_errno()鍙彂鐜板嚭鐜扮殑閿欒鏄粈涔堛傞氳繃璋冪敤mysql_error()锛屽彲鑾峰緱閿欒鐨勫瓧绗︿覆琛ㄧず銆
my_ulonglong mysql_affected_rows(MYSQL *mysql)
鎻忚堪
杩斿洖涓婃UPDATE鏇存敼鐨勮鏁帮紝涓婃DELETE鍒犻櫎鐨勮鏁帮紝鎴涓婃INSERT璇彞鎻掑叆鐨勮鏁般傚浜UPDATE銆DELETE鎴INSERT璇彞锛屽彲鍦mysql_query()鍚庣珛鍒昏皟鐢ㄣ瀵逛簬SELECT璇彞锛mysql_affected_rows()鐨勫伐浣滄柟寮忎笌mysql_num_rows()绫讳技銆
杩斿洖鍊
澶т簬0鐨勬暣鏁拌〃鏄庡彈褰卞搷鎴栨绱㈢殑琛屾暟銆傗0鈥濊〃绀UPDATE璇彞鏈洿鏂璁板綍锛屽湪鏌ヨ涓病鏈変笌WHERE鍖归厤鐨勮锛屾垨鏈墽琛屾煡璇€傗-1鈥濊〃绀烘煡璇㈣繑鍥為敊璇紝鎴栬咃紝瀵逛簬SELECT鏌ヨ锛屽湪璋冪敤mysql_store_result()涔嬪墠璋冪敤浜mysql_affected_rows()銆傜敱浜mysql_affected_rows()杩斿洖鏃犵鍙峰硷紝閫氳繃姣旇緝杩斿洖鍊煎拰鈥(my_ulonglong)-1鈥鎴栫瓑鏁堢殑鈥(my_ulonglong)~0鈥锛屾鏌ユ槸鍚︿负鈥-1鈥濄
閿欒
鏃犮
绀轰緥锛
mysql_query(&mysql,"UPDATE products SET cost=cost*1.25 WHERE group=10");
printf("%ld products updated",(long) mysql_affected_rows(&mysql));
濡傛灉鍦ㄨ繛鎺ヨ嚦mysqld鏃舵寚瀹氫簡鏍囧織CLIENT_FOUND_ROWS锛屽浜UPDATE璇彞锛mysql_affected_rows()灏嗚繑鍥WHERE璇彞鍖归厤鐨勮鏁般
娉ㄦ剰锛屼娇鐢REPLACE鍛戒护鏃讹紝濡傛灉鏂拌鏇夸唬浜嗘棫琛岋紝mysql_affected_rows()杩斿洖2銆傝繖鏄洜涓猴紝鍦ㄨ鎯呭喌涓嬶紝鍒犻櫎浜嗛噸澶嶈鍚庢彃鍏ヤ簡1琛屻
濡傛灉浣跨敤鈥INSERT ... ON DUPLICATE KEY UPDATE鈥濇潵鎻掑叆琛岋紝濡傛灉琛屾槸浣滀负鏂拌鎻掑叆鐨锛宮ysql_affected_rows()杩斿洖1锛屽鏋滄槸鏇存柊浜嗗凡鏈夌殑琛岋紝杩斿洖2銆
my_bool mysql_autocommit(MYSQL *mysql, my_bool mode)
鎻忚堪
濡傛灉妯″紡涓衡1鈥濓紝鍚敤autocommit妯″紡锛涘鏋滄ā寮忎负鈥0鈥濓紝绂佹autocommit妯″紡銆
杩斿洖鍊
濡傛灉鎴愬姛锛岃繑鍥0锛屽鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
鏃犮
my_bool mysql_change_user(MYSQL *mysql, const char *user, const char *password, const char *db)
鎻忚堪
鏇存敼鐢ㄦ埛锛屽苟浣跨敱db鎸囧畾鐨勬暟鎹簱鎴愪负鐢mysql鎸囧畾鐨勮繛鎺ヤ笂鐨勯粯璁ゆ暟鎹簱锛堝綋鍓嶆暟鎹簱锛夈鍦ㄥ悗缁煡璇腑锛屽浜庝笉鍖呭惈鏄惧紡鏁版嵁搴撳尯鍒嗙鐨勮〃寮曠敤锛岃鏁版嵁搴撴槸榛樿鏁版嵁搴撱
濡傛灉涓嶈兘纭畾宸茶繛鎺ョ殑鐢ㄦ埛鎴栫敤鎴蜂笉鍏锋湁浣跨敤鏁版嵁搴撶殑鏉冮檺锛mysql_change_user()灏嗗け璐ャ傚湪杩欑鎯呭喌涓嬶紝涓嶄細鏀瑰彉鐢ㄦ埛鍜屾暟鎹簱銆
濡傛灉涓嶆墦绠楁嫢鏈夐粯璁ゆ暟鎹簱锛屽彲灏db鍙傛暟璁剧疆涓NULL銆
璇ュ懡浠ゆ绘槸浼氭墽琛屾椿鍔ㄤ簨鍔$殑ROLLBACK鎿嶄綔锛屽叧闂墍鏈夌殑涓存椂琛紝瑙i攣鎵鏈夌殑閿佸畾琛紝骞跺浣嶇姸鎬侊紝灏卞儚杩涜浜嗘柊杩炴帴閭f牱銆傚嵆浣挎湭鏇存敼鐢ㄦ埛锛屼篃浼氬嚭鐜拌鎯呭喌銆
杩斿洖鍊
0琛ㄧず鎴愬姛锛岄潪0鍊艰〃绀哄嚭鐜伴敊璇
閿欒
涓庝粠mysql_real_connect()鑾峰緱鐨勭浉鍚屻
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑涓㈠け浜嗕笌鏈嶅姟鍣ㄧ殑杩炴帴銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
路 ER_UNKNOWN_COM_ERROR
MySQL鏈嶅姟鍣ㄦ湭瀹炴柦璇ュ懡浠わ紙鎴栬鏄緝浣庣増鏈殑鏈嶅姟鍣級銆
路 ER_ACCESS_DENIED_ERROR
鐢ㄦ埛鎴栧瘑鐮侀敊璇
路 ER_BAD_DB_ERROR
鏁版嵁搴撲笉瀛樺湪銆
路 ER_DBACCESS_DENIED_ERROR
鐢ㄦ埛娌℃湁璁块棶鏁版嵁搴撶殑鏉冮檺銆
路 ER_WRONG_DB_NAME
鏁版嵁搴撳悕绉拌繃闀裤
绀轰緥锛
if (mysql_change_user(&mysql, "user", "password", "new_database"))
{
fprintf(stderr, "Failed to change user. Error: %s\n",
mysql_error(&mysql));
}
void mysql_close(MYSQL *mysql)
鎻忚堪
鍏抽棴鍓嶉潰鎵撳紑鐨勮繛鎺ャ傚鏋滃彞鏌勬槸鐢mysql_init()鎴mysql_connect()鑷姩鍒嗛厤鐨勶紝mysql_close()杩樺皢瑙i櫎鍒嗛厤鐢mysql鎸囧悜鐨勮繛鎺ュ彞鏌勩
杩斿洖鍊
鏃犮
閿欒
鏃犮
my_bool mysql_commit(MYSQL *mysql)
鎻忚堪
鎻愪氦褰撳墠浜嬪姟銆
璇ュ嚱鏁扮殑鍔ㄤ綔鍙completion_type绯荤粺鍙橀噺鐨勫兼帶鍒躲傚挨鍏舵槸锛濡傛灉completion_type鐨勫间负2锛岀粓缁撲簨鍔″苟鍏抽棴瀹㈡埛绔繛鎺ュ悗锛屾湇鍔″櫒灏嗘墽琛岄噴鏀炬搷浣溿傚鎴风绋嬪簭搴旇皟鐢mysql_close()锛屼粠瀹㈡埛绔竴渚у叧闂繛鎺ャ
杩斿洖鍊
濡傛灉鎴愬姛锛岃繑鍥0锛屽鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
鏃犮
MYSQL *mysql_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd)
鎻忚堪
璇ュ嚱鏁板凡杩囨椂銆傛渶濂戒娇鐢mysql_real_connect()鍙栬屼唬涔嬨
mysql_connect()璇曞浘寤虹珛涓庤繍琛屽湪涓绘満涓婄殑MySQL鏁版嵁搴撳紩鎿庣殑杩炴帴銆傚湪鑳藉鎵ц浠讳綍鍏朵粬API鍑芥暟涔嬪墠锛mysql_connect()蹇呴』鎴愬姛瀹屾垚锛屼絾mysql_get_client_info()渚嬪銆
杩欎簺鍙傛暟鐨勬剰涔変笌mysql_real_connect()鐨勫搴斿弬鏁扮殑鎰忎箟鐩稿悓锛宸埆鍦ㄤ簬杩炴帴鍙傛暟鍙互涓NULL銆傚湪杩欑鎯呭喌涓嬶紝C API灏嗚嚜鍔ㄤ负杩炴帴缁撴瀯鍒嗛厤鍐呭瓨锛屽苟褰撹皟鐢mysql_close()鏃堕噴鏀惧垎閰嶇殑鍐呭瓨銆傝鏂规硶鐨勭己鐐规槸锛屽鏋滆繛鎺ュけ璐ワ紝浣犳棤娉曟绱㈤敊璇秷鎭瑕佹兂浠mysql_errno()鎴mysql_error()鑾峰緱閿欒娑堟伅锛蹇呴』鎻愪緵鏈夋晥鐨MYSQL鎸囬拡銆
杩斿洖鍊
涓mysql_real_connect()鐨勭浉鍚屻
閿欒
涓mysql_real_connect()鐨勭浉鍚屻
int mysql_create_db(MYSQL *mysql, const char *db)
鎻忚堪
鍒涘缓鐢db鍙傛暟鍛藉悕鐨勬暟鎹簱銆
璇ュ嚱鏁板凡杩囨椂銆傛渶濂戒娇鐢mysql_query()鏉ュ彂鍑SQL CREATE DATABASE璇彞銆
杩斿洖鍊
濡傛灉鏁版嵁搴撳凡鎴愬姛鍒涘缓锛岃繑鍥0锛屽鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
绀轰緥锛
if(mysql_create_db(&mysql, "my_database"))
{
fprintf(stderr, "Failed to create new database. Error: %s\n",
mysql_error(&mysql));
}
void mysql_data_seek(MYSQL_RES *result, my_ulonglong offset)
鎻忚堪
鍦ㄦ煡璇㈢粨鏋滈泦涓鎵句换鎰忚銆傚亸绉诲间负琛屽彿锛岃寖鍥翠粠0鍒mysql_num_rows(result)-1銆
璇ュ嚱鏁拌姹傜粨鏋滈泦缁撴瀯鍖呭惈鏌ヨ鐨勬墍鏈夌粨鏋滐紝鍥犳锛so mysql_data_seek()浠呭簲涓mysql_store_result()鑱斿悎浣跨敤锛岃屼笉鏄涓mysql_use_result()銆
杩斿洖鍊
鏃犮
閿欒
鏃犮
void mysql_debug(const char *debug)
鎻忚堪
鐢ㄧ粰瀹氱殑瀛楃涓叉墽琛DBUG_PUSH銆mysql_debug()閲囩敤Fred Fish璋冭瘯搴撱傝鎯充娇鐢ㄨ鍑芥暟锛屽繀椤荤紪璇戝鎴风搴擄紝浣夸箣鏀寔璋冭瘯鍔熻兘銆傝鍙傝E.1鑺傦紝鈥滆皟璇昅ySQL鏈嶅姟鍣ㄢ銆傝鍙傝E.2鑺傦紝鈥滆皟璇昅ySQL瀹㈡埛绔銆
杩斿洖鍊
鏃犮
閿欒
鏃犮
绀轰緥锛
杩欓噷缁欏嚭鐨勮皟鐢ㄥ皢浣垮鎴风搴撳湪瀹㈡埛绔満鍣ㄧ殑/tmp/client.trace涓敓鎴1涓窡韪枃浠躲
mysql_debug("d:t:O,/tmp/client.trace");
int mysql_drop_db(MYSQL *mysql, const char *db)
鎻忚堪
鎾ら攢鐢db鍙傛暟鍛藉悕鏁版嵁搴撱
璇ュ嚱鏁板凡杩囨椂銆傛渶濂戒娇鐢mysql_query()鏉ュ彂鍑SQL DROP DATABASE璇彞
杩斿洖鍊
濡傛灉鎴愬姛鎾ら攢浜嗘暟鎹簱锛岃繑鍥0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
绀轰緥锛
if(mysql_drop_db(&mysql, "my_database"))
fprintf(stderr, "Failed to drop the database: Error: %s\n",
mysql_error(&mysql));
int mysql_dump_debug_info(MYSQL *mysql)
鎻忚堪
鎸囩ず鏈嶅姟鍣ㄥ皢涓浜涜皟璇曚俊鎭啓鍏ユ棩蹇椼傝鎯充娇涔嬪伐浣滐紝宸茶繛鎺ョ殑鐢ㄦ埛蹇呴』鍏锋湁SUPER鏉冮檺銆
杩斿洖鍊
濡傛灉鍛戒护鎴愬姛锛岃繑鍥0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
my_bool mysql_eof(MYSQL_RES *result)
鎻忚堪
璇ュ嚱鏁板凡杩囨椂銆傚簲浣跨敤mysql_errno()鎴mysql_error()鍙栬屼唬涔嬨
mysql_eof()纭畾鏄惁宸茶鍙栦簡缁撴灉闆嗙殑鏈鍚1琛屻
濡傛灉閫氳繃鎴愬姛璋冪敤mysql_store_result()鑾峰緱浜嗙粨鏋滈泦锛屽鎴风灏嗗湪1娆℃搷浣滀腑鏀跺埌鏁翠釜缁撴灉闆嗐傚湪璇ユ儏鍐典笅锛浠mysql_fetch_row()杩斿洖鐨NULL鎬昏〃绀哄凡鍒拌揪缁撴灉闆嗘湯灏撅紝鑰屼笖娌″繀瑕佽皟鐢mysql_eof()銆涓mysql_store_result()涓璧蜂娇鐢ㄦ椂锛mysql_eof()鎬昏繑鍥鈥鐪鈥銆
鍙︿竴鏂归潰锛屽鏋滀綘浣跨敤mysql_use_result()鏉ュ垵濮嬪寲缁撴灉闆嗘绱紝褰撻噸澶嶈皟鐢mysql_fetch_row()鏃讹紝灏嗛愪釜鍦颁粠鏈嶅姟鍣ㄨ幏鍙栫粨鏋滈泦鐨勮銆傜敱浜庡湪璇ヨ繃绋嬩腑锛屽彲鑳藉嚭鐜拌繛鎺ヤ笂鐨勯敊璇紝浠mysql_fetch_row()杩斿洖鐨NULL鍊间笉涓瀹氳〃绀哄凡姝e父鍦版姷杈剧粨鏋滈泦鏈熬銆鍦ㄨ鎯呭喌涓嬶紝鍙互浣跨敤mysql_eof()鏉ュ垽瀹氬嚭鐜颁簡浠涔堟儏鍐点傚鏋滄姷杈剧粨鏋滈泦鏈熬锛mysql_eof()杩斿洖闈0鍊硷紝濡傛灉鍑虹幇閿欒锛岃繑鍥0銆
浠庡巻鍙茬殑瑙掑害涓婄湅锛mysql_eof()鍦ㄦ棩鏈熶笂鏃╀簬鏍囧噯鐨MySQL閿欒鍑芥暟mysql_errno()鍜mysql_error()銆傜敱浜庤繖绫婚敊璇嚱鏁版彁渚涗簡鐩稿悓鐨勪俊鎭紝瀹冧滑浼樺厛浜庡凡杩囨椂鐨mysql_eof()銆備簨瀹炰笂锛屽畠浠彁渚涗簡鏇村淇℃伅锛岃繖鏄洜涓猴紝mysql_eof()浠呰繑鍥炲竷灏斿硷紝閿欒鍑芥暟鑳藉鍦ㄥ嚭鐜伴敊璇椂鎸囨槑閿欒鐨勫師鍥犮
杩斿洖鍊
濡傛灉鏈嚭鐜伴敊璇紝杩斿洖0銆傚鏋滄姷杈剧粨鏋滈泦鐨勬湯灏撅紝杩斿洖闈0鍊笺
閿欒
鏃犮
绀轰緥锛
鍦ㄤ笅闈㈢殑绀轰緥涓紝浠嬬粛浜嗕娇鐢mysql_eof()鐨勬柟娉曪細
mysql_query(&mysql,"SELECT * FROM some_table");
result = mysql_use_result(&mysql);
while((row = mysql_fetch_row(result)))
{
// do something with data
}
if(!mysql_eof(result)) // mysql_fetch_row() failed due to an error
{
fprintf(stderr, "Error: %s\n", mysql_error(&mysql));
}
浣嗘槸锛屼綘涔熻兘浣跨敤鏍囧噯鐨MySQL閿欒鍑芥暟瀹炵幇鐩稿悓鐨勭粨鏋滐細
mysql_query(&mysql,"SELECT * FROM some_table");
result = mysql_use_result(&mysql);
while((row = mysql_fetch_row(result)))
{
// do something with data
}
if(mysql_errno(&mysql)) // mysql_fetch_row() failed due to an error
{
fprintf(stderr, "Error: %s\n", mysql_error(&mysql));
}
unsigned int mysql_errno(MYSQL *mysql)
鎻忚堪
瀵逛簬鐢mysql鎸囧畾鐨勮繛鎺ワ紝mysql_errno()杩斿洖鏈杩戣皟鐢ㄧ殑API鍑芥暟鐨勯敊璇唬鐮侊紝璇ュ嚱鏁拌皟鐢ㄥ彲鑳芥垚鍔熶篃鍙兘澶辫触銆傗0鈥濊繑鍥炲艰〃绀烘湭鍑虹幇閿欒銆傚湪MySQL errmsg.h澶存枃浠朵腑锛屽垪鍑轰簡瀹㈡埛绔敊璇秷鎭紪鍙枫傚湪闄勫綍B锛閿欒浠g爜鍜屾秷鎭涓紝涔熷垪鍑轰簡杩欎簺閿欒銆
娉ㄦ剰锛屽鏋滄垚鍔燂紝鏌愪簺鍑芥暟锛屽mysql_fetch_row()绛夛紝涓嶄細璁剧疆mysql_errno()銆
缁忛獙瑙勫垯鏄紝濡傛灉鎴愬姛锛屾墍鏈夊悜鏈嶅姟鍣ㄨ姹備俊鎭殑鍑芥暟鍧囦細澶嶄綅mysql_errno()銆
杩斿洖鍊
濡傛灉澶辫触锛岃繑鍥炰笂娆mysql_xxx()璋冪敤鐨勯敊璇唬鐮併鈥0鈥琛ㄧず鏈嚭鐜伴敊璇
閿欒
鏃犮
const char *mysql_error(MYSQL *mysql)
鎻忚堪
瀵逛簬鐢mysql鎸囧畾鐨勮繛鎺ワ紝瀵逛簬澶辫触鐨勬渶杩戣皟鐢ㄧ殑API鍑芥暟锛mysql_error()杩斿洖鍖呭惈閿欒娑堟伅鐨勩佺敱Null缁堢粨鐨勫瓧绗︿覆銆傚鏋滆鍑芥暟鏈け璐ワ紝mysql_error()鐨勮繑鍥炲煎彲鑳芥槸浠ュ墠鐨勯敊璇紝鎴栨寚鏄庢棤閿欒鐨勭┖瀛楃涓层
缁忛獙瑙勫垯鏄紝濡傛灉鎴愬姛锛屾墍鏈夊悜鏈嶅姟鍣ㄨ姹備俊鎭殑鍑芥暟鍧囦細澶嶄綅mysql_error()銆
瀵逛簬澶嶄綅mysql_errno()鐨勫嚱鏁帮紝涓嬭堪涓や釜娴嬭瘯鏄瓑鏁堢殑锛
if(mysql_errno(&mysql))
{
// an error occurred
}
if(mysql_error(&mysql)[0] != '\0')
{
// an error occurred
}
閫氳繃閲嶆柊缂栬瘧MySQL瀹㈡埛绔簱锛屽彲浠ユ洿鏀瑰鎴风閿欒娑堟伅鐨勮瑷銆傜洰鍓嶏紝鑳藉閫夋嫨鏁扮璇█鏄剧ず閿欒娑堟伅锛岃鍙傝5.10.2鑺傦紝鈥滆缃敊璇秷鎭瑷鈥銆
杩斿洖鍊
杩斿洖鎻忚堪閿欒鐨勩佺敱Null缁堢粨鐨勫瓧绗︿覆銆傚鏋滄湭鍑虹幇閿欒锛岃繑鍥炵┖瀛楃涓层
閿欒
鏃犮
搴斾娇鐢mysql_real_escape_string()鍙栬屼唬涔嬶紒
璇ュ嚱鏁颁笌mysql_real_escape_string()绛夊悓锛浣mysql_real_escape_string()浼氬皢杩炴帴澶勭悊绋嬪簭浣滀负鍏剁1涓弬閲忥紝骞舵寜鐓у綋鍓嶅瓧绗﹂泦瀵瑰瓧绗︿覆杩涜杞箟澶勭悊銆mysql_escape_string()涓嶉噰鐢ㄨ繛鎺ュ弬閲忥紝鑰屼笖涓嶈冭檻褰撳墠瀛楃闆嗚缃
MYSQL_FIELD *mysql_fetch_field(MYSQL_RES *result)
鎻忚堪
杩斿洖閲囩敤MYSQL_FIELD缁撴瀯鐨勭粨鏋滈泦鐨勫垪銆閲嶅璋冪敤璇ュ嚱鏁帮紝浠ユ绱㈠叧浜庣粨鏋滈泦涓墍鏈夊垪鐨勪俊鎭傛湭鍓╀綑瀛楁鏃讹紝mysql_fetch_field()杩斿洖NULL銆
姣忔鎵ц鏂扮殑SELECT鏌ヨ鏃讹紝灏嗗浣mysql_fetch_field()锛屼互杩斿洖鍏充簬绗1涓瓧娈电殑淇℃伅銆傝皟鐢mysql_field_seek()涔熶細褰卞搷mysql_fetch_field()杩斿洖鐨勫瓧娈点
濡傛灉璋冪敤浜mysql_query()浠ュ湪琛ㄤ笂鎵цSELECT锛浣嗘湭璋冪敤mysql_store_result()锛屽鏋滆皟鐢ㄤ簡mysql_fetch_field()浠ヨ姹BLOB瀛楁鐨勯暱搴︼紝MySQL灏嗚繑鍥為粯璁ょ殑Blob闀垮害锛8KB锛夈備箣鎵浠ラ夋嫨8KB鏄洜涓MySQL涓嶇煡閬BLOB鐨勬渶澶ч暱搴︺傚簲鍦ㄦ棩鍚庝娇鍏舵垚涓哄彲閰嶇疆鐨勩涓鏃︽绱簡缁撴灉闆嗭紝field->max_length灏嗗寘鍚壒瀹氭煡璇腑璇ュ垪鐨勬渶澶у肩殑闀垮害銆
杩斿洖鍊
褰撳墠鍒楃殑MYSQL_FIELD缁撴瀯銆傚鏋滄湭鍓╀綑浠讳綍鍒楋紝杩斿洖NULL銆
閿欒
鏃犮
绀轰緥锛
MYSQL_FIELD *field;
while((field = mysql_fetch_field(result)))
{
printf("field name %s\n", field->name);
}
MYSQL_FIELD *mysql_fetch_field_direct(MYSQL_RES *result, unsigned int fieldnr)
鎻忚堪
缁欏畾缁撴灉闆嗗唴鏌1鍒楃殑瀛楁缂栧彿fieldnr锛屼互MYSQL_FIELD缁撴瀯褰㈠紡杩斿洖鍒楃殑瀛楁瀹氫箟銆傚彲浠ヤ娇鐢ㄨ鍑芥暟妫绱换鎰忓垪鐨勫畾涔夈Fieldnr鐨勫煎簲鍦ㄤ粠0鍒mysql_num_fields(result)-1鐨勮寖鍥村唴銆
杩斿洖鍊
瀵逛簬鎸囧畾鍒楋紝杩斿洖MYSQL_FIELD缁撴瀯銆
閿欒
鏃犮
绀轰緥锛
unsigned int num_fields;
unsigned int i;
MYSQL_FIELD *field;
num_fields = mysql_num_fields(result);
for(i = 0; i < num_fields; i++)
{
field = mysql_fetch_field_direct(result, i);
printf("Field %u is %s\n", i, field->name);
}
MYSQL_FIELD *mysql_fetch_fields(MYSQL_RES *result)
鎻忚堪
瀵逛簬缁撴灉闆嗭紝杩斿洖鎵鏈MYSQL_FIELD缁撴瀯鐨勬暟缁勩傛瘡涓粨鏋勬彁渚涗簡缁撴灉闆嗕腑1鍒楃殑瀛楁瀹氫箟銆
杩斿洖鍊
鍏充簬缁撴灉闆嗘墍鏈夊垪鐨MYSQL_FIELD缁撴瀯鐨勬暟缁勩
閿欒
鏃犮
绀轰緥锛
unsigned int num_fields;
unsigned int i;
MYSQL_FIELD *fields;
num_fields = mysql_num_fields(result);
fields = mysql_fetch_fields(result);
for(i = 0; i < num_fields; i++)
{
printf("Field %u is %s\n", i, fields[i].name);
}
unsigned long *mysql_fetch_lengths(MYSQL_RES *result)
鎻忚堪
杩斿洖缁撴灉闆嗗唴褰撳墠琛岀殑鍒楃殑闀垮害銆傚鏋滄墦绠楀鍒跺瓧娈靛硷紝璇ラ暱搴︿俊鎭湁鍔╀簬浼樺寲锛岃繖鏄洜涓猴紝浣犺兘閬垮厤璋冪敤strlen()銆傛澶栵紝濡傛灉缁撴灉闆嗗寘鍚簩杩涘埗鏁版嵁锛屽繀椤讳娇鐢ㄨ鍑芥暟鏉ョ‘瀹氭暟鎹殑澶у皬锛屽師鍥犲湪浜庯紝瀵逛簬鍖呭惈Null瀛楃鐨勪换浣曞瓧娈碉紝strlen()灏嗚繑鍥為敊璇殑缁撴灉銆
瀵逛簬绌哄垪浠ュ強鍖呭惈NULL鍊肩殑鍒楋紝鍏堕暱搴︿负0銆傝鎯充簡瑙e尯鍒嗚繖涓ょ被鎯呭喌鐨勬柟娉曪紝璇峰弬瑙佸叧浜mysql_fetch_row()鐨勪粙缁嶃
杩斿洖鍊
鏃犵鍙烽暱鏁存暟鐨勬暟缁勮〃绀哄悇鍒楃殑澶у皬锛堜笉鍖呮嫭浠讳綍缁堢粨NULL瀛楃锛夈傚鏋滃嚭鐜伴敊璇紝杩斿洖NULL銆
閿欒
mysql_fetch_lengths()浠呭缁撴灉闆嗙殑褰撳墠琛屾湁鏁堛傚鏋滃湪璋冪敤mysql_fetch_row()涔嬪墠鎴栨绱簡缁撴灉闆嗕腑鐨勬墍鏈夎鍚庤皟鐢ㄤ簡瀹冿紝灏嗚繑鍥NULL銆
绀轰緥锛
MYSQL_ROW row;
unsigned long *lengths;
unsigned int num_fields;
unsigned int i;
row = mysql_fetch_row(result);
if (row)
{
num_fields = mysql_num_fields(result);
lengths = mysql_fetch_lengths(result);
for(i = 0; i < num_fields; i++)
{
printf("Column %u is %lu bytes in length.\n", i, lengths[i]);
}
}
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result)
鎻忚堪
妫绱㈢粨鏋滈泦鐨勪笅涓琛屻傚湪mysql_store_result()涔嬪悗浣跨敤鏃讹紝濡傛灉娌℃湁瑕佹绱㈢殑琛岋紝mysql_fetch_row()杩斿洖NULL銆傚湪mysql_use_result()涔嬪悗浣跨敤鏃讹紝濡傛灉娌℃湁瑕佹绱㈢殑琛屾垨鍑虹幇浜嗛敊璇紝mysql_fetch_row()杩斿洖NULL銆
琛屽唴鍊肩殑鏁扮洰鐢mysql_num_fields(result)缁欏嚭銆傚鏋滆涓繚瀛樹簡璋冪敤mysql_fetch_row()杩斿洖鐨勫硷紝灏嗘寜鐓row[0]鍒row[mysql_num_fields(result)-1]锛岃闂繖浜涘肩殑鎸囬拡銆傝涓殑NULL鍊肩敱NULL鎸囬拡鎸囨槑銆
鍙互閫氳繃璋冪敤mysql_fetch_lengths()鏉ヨ幏寰楄涓瓧娈靛肩殑闀垮害銆傚浜庣┖瀛楁浠ュ強鍖呭惈NULL鐨勫瓧娈碉紝闀垮害涓0銆傞氳繃妫鏌ュ瓧娈靛肩殑鎸囬拡锛岃兘澶熷尯鍒嗗畠浠傚鏋滄寚閽堜负NULL锛屽瓧娈典负NULL锛屽惁鍒欏瓧娈典负绌恒
杩斿洖鍊
涓嬩竴琛岀殑MYSQL_ROW缁撴瀯銆傚鏋滄病鏈夋洿澶氳妫绱㈢殑琛屾垨鍑虹幇浜嗛敊璇紝杩斿洖NULL銆
閿欒
娉ㄦ剰锛屽湪瀵mysql_fetch_row()鐨勪袱娆¤皟鐢ㄤ箣闂达紝涓嶄細澶嶄綅閿欒銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
绀轰緥锛
MYSQL_ROW row;
unsigned int num_fields;
unsigned int i;
num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result)))
{
unsigned long *lengths;
lengths = mysql_fetch_lengths(result);
for(i = 0; i < num_fields; i++)
{
printf("[%.*s] ", (int) lengths[i], row[i] ? row[i] : "NULL");
}
printf("\n");
}
unsigned int mysql_field_count(MYSQL *mysql)
鎻忚堪
杩斿洖浣滅敤鍦ㄨ繛鎺ヤ笂鐨勬渶杩戞煡璇㈢殑鍒楁暟銆
璇ュ嚱鏁扮殑姝e父浣跨敤鏄湪mysql_store_result()杩斿洖NULL锛堝洜鑰屾病鏈夌粨鏋滈泦鎸囬拡锛夋椂銆傚湪杩欑鎯呭喌涓嬶紝鍙皟鐢mysql_field_count()鏉ュ垽瀹mysql_store_result()鏄惁搴旂敓鎴愰潪绌虹粨鏋溿傝繖鏍凤紝瀹㈡埛绔氨鑳介噰鍙栨伆褰撶殑鍔ㄤ綔锛岃屾棤闇鐭ラ亾鏌ヨ鏄惁鏄SELECT锛堟垨绫讳技SELECT鐨勶級璇彞銆鍦ㄨ繖閲岀粰鍑虹殑绀轰緥涓紝婕旂ず浜嗗畬鎴愬畠鐨勬柟娉曘
璇峰弬瑙25.2.13.1鑺傦紝鈥滀负浠涔堝湪mysql_query()杩斿洖鎴愬姛鍚庯紝mysql_store_result()鏈夋椂浼氳繑鍥濶ULL鈥.
杩斿洖鍊
琛ㄧず缁撴灉闆嗕腑鍒楁暟鐨勬棤绗﹀彿鏁存暟銆
閿欒
鏃犮
绀轰緥锛
MYSQL_RES *result;
unsigned int num_fields;
unsigned int num_rows;
if (mysql_query(&mysql,query_string))
{
// error
}
else // query succeeded, process any data returned by it
{
result = mysql_store_result(&mysql);
if (result) // there are rows
{
num_fields = mysql_num_fields(result);
// retrieve rows, then call mysql_free_result(result)
}
else // mysql_store_result() returned nothing; should it have?
{
if(mysql_field_count(&mysql) == 0)
{
// query does not return data
// (it was not a SELECT)
num_rows = mysql_affected_rows(&mysql);
}
else // mysql_store_result() should have returned data
{
fprintf(stderr, "Error: %s\n", mysql_error(&mysql));
}
}
}
鍙︿竴绉嶅彲閫夌殑鏂规硶鏄紝鐢mysql_errno(&mysql)鏇挎崲mysql_field_count(&mysql)璋冪敤銆鍦ㄨ鎯呭喌涓嬶紝鏃犺璇彞鏄惁鏄SELECT锛浣犲皢鐩存帴浠mysql_store_result()鏌ユ壘閿欒锛鑰屼笉鏄粠mysql_field_count()鐨勫艰繘琛屾帹鏂
MYSQL_FIELD_OFFSET mysql_field_seek(MYSQL_RES *result, MYSQL_FIELD_OFFSET offset)
鎻忚堪
灏嗗瓧娈靛厜鏍囪缃埌缁欏畾鐨勫亸绉诲銆傚mysql_fetch_field()鐨勪笅涓娆¤皟鐢ㄥ皢妫绱笌璇ュ亸绉荤浉鍏崇殑鍒楀畾涔夈
瑕佹兂鏌ユ壘琛岀殑寮濮嬶紝璇蜂紶閫掑间负0鐨勫亸绉婚噺銆
杩斿洖鍊
瀛楁鍏夋爣鐨勫墠涓涓笺
閿欒
鏃犮
MYSQL_FIELD_OFFSET mysql_field_tell(MYSQL_RES *result)
鎻忚堪
杩斿洖涓婁竴涓mysql_fetch_field()鎵浣跨敤鐨勫瓧娈靛厜鏍囩殑瀹氫箟銆傝鍊煎彲鐢ㄤ綔mysql_field_seek()鐨勫弬閲忋
杩斿洖鍊
瀛楁鍏夋爣鐨勫綋鍓嶅亸绉婚噺銆
閿欒
鏃犮
void mysql_free_result(MYSQL_RES *result)
鎻忚堪
閲婃斁鐢mysql_store_result()銆mysql_use_result()銆mysql_list_dbs()绛変负缁撴灉闆嗗垎閰嶇殑鍐呭瓨銆傚畬鎴愬缁撴灉闆嗙殑鎿嶄綔鍚庯紝蹇呴』璋冪敤mysql_free_result()閲婃斁缁撴灉闆嗕娇鐢ㄧ殑鍐呭瓨銆
閲婃斁瀹屾垚鍚庯紝涓嶈灏濊瘯璁块棶缁撴灉闆嗐
杩斿洖鍊
鏃犮
閿欒
鏃犮
void mysql_get_character_set_info(MYSQL *mysql, MY_CHARSET_INFO *cs)
鎻忚堪
璇ュ嚱鏁版彁渚涗簡鍏充簬榛樿瀹㈡埛绔瓧绗﹂泦鐨勪俊鎭傚彲浠ヤ娇鐢mysql_set_character_set()鍑芥暟鏇存敼榛樿鐨勫瓧绗﹂泦銆
璇ュ嚱鏁版槸鍦MySQL 5.0.10涓鍔犵殑銆
绀轰緥锛
if (!mysql_set_character_set(&mysql, "utf8"))
{
MY_CHARSET_INFO cs;
mysql_get_character_set_info(&mysql, &cs);
printf("character set information:\n");
printf("character set name: %s\n", cs.name);
printf("collation name: %s\n", cs.csname);
printf("comment: %s\n", cs.comment);
printf("directory: %s\n", cs.dir);
printf("multi byte character min. length: %d\n", cs.mbminlen);
printf("multi byte character max. length: %d\n", cs.mbmaxlen);
}
unsigned long mysql_get_client_version(void)
鎻忚堪
杩斿洖琛ㄧず瀹㈡埛绔簱鐗堟湰鐨勬暣鏁般傝鍊肩殑鏍煎紡鏄XYYZZ锛屽叾涓X鏄富鐗堟湰鍙凤紝YY鏄彂甯冪骇鍒紝ZZ鏄彂甯冪骇鍒唴鐨勭増鏈彿銆渚嬪锛屽40102琛ㄧず瀹㈡埛绔簱鐨勭増鏈槸4.1.2銆
杩斿洖鍊
琛ㄧずMySQL瀹㈡埛绔簱鐗堟湰鐨勬暣鏁般
閿欒
鏃犮
char *mysql_get_host_info(MYSQL *mysql)
鎻忚堪
杩斿洖鎻忚堪浜嗘墍浣跨敤杩炴帴绫诲瀷鐨勫瓧绗︿覆锛屽寘鎷湇鍔″櫒涓绘満鍚嶃
杩斿洖鍊
浠h〃鏈嶅姟鍣ㄤ富鏈哄悕鍜岃繛鎺ョ被鍨嬬殑瀛楃涓层
閿欒
鏃犮
unsigned int mysql_get_proto_info(MYSQL *mysql)
鎻忚堪
杩斿洖褰撳墠杩炴帴鎵浣跨敤鐨勫崗璁増鏈
杩斿洖鍊
浠h〃褰撳墠杩炴帴鎵浣跨敤鍗忚鐗堟湰鐨勬棤绗﹀彿鏁存暟銆
閿欒
鏃犮
char *mysql_get_server_info(MYSQL *mysql)
鎻忚堪
杩斿洖浠h〃鏈嶅姟鍣ㄧ増鏈彿鐨勫瓧绗︿覆銆
杩斿洖鍊
浠h〃鏈嶅姟鍣ㄧ増鏈彿鐨勫瓧绗︿覆銆
閿欒
鏃犮
unsigned long mysql_get_server_version(MYSQL *mysql)
鎻忚堪
浠ユ暣鏁板舰寮忚繑鍥炴湇鍔″櫒鐨勭増鏈彿銆
杩斿洖鍊
琛ㄧずMySQL鏈嶅姟鍣ㄧ増鏈殑鏁板硷紝鏍煎紡濡備笅锛
major_version*10000 + minor_version *100 + sub_version
渚嬪锛屽浜5.0.12锛岃繑鍥500012銆
鍦ㄥ鎴风绋嬪簭涓紝涓轰簡蹇熺‘瀹氭煇浜涗笌鐗堟湰鐩稿叧鐨勬湇鍔″櫒鍔熻兘鏄惁瀛樺湪锛岃鍑芥暟寰堟湁鐢ㄣ
閿欒
鏃犮
unsigned long mysql_hex_string(char *to, const char *from, unsigned long length)
鎻忚堪
璇ュ嚱鏁扮敤浜庡垱寤哄彲鐢ㄥ湪SQL璇彞涓殑鍚堟硶SQL瀛楃涓层傝鍙傝9.1.1鑺傦紝鈥滃瓧绗︿覆鈥銆
璇ュ瓧绗︿覆浠庡舰寮忎笂缂栫爜涓哄崄鍏繘鍒舵牸寮忥紝姣忎釜瀛楃缂栫爜涓2涓崄鍏繘鍒舵暟銆傜粨鏋滆缃叆鍏朵腑锛屽苟娣诲姞1涓粓缁Null瀛楄妭銆
鈥from鈥濇墍鎸囧悜鐨勫瓧绗︿覆蹇呴』鏄暱搴﹀瓧鑺傗long鈥濄傚繀椤讳负鈥to鈥濆垎閰嶇紦鍐插尯锛岀紦鍐插尯鑷冲皯涓length*2+1瀛楄妭闀裤傚綋mysql_hex_string()杩斿洖鏃讹紝鈥渢o鈥鐨勫唴瀹逛负鐢Null缁堢粨鐨勫瓧绗︿覆銆傝繑鍥炲兼槸缂栫爜瀛楃涓茬殑闀垮害锛屼笉鍖呮嫭缁堢粨鐢Null瀛楃銆
鍙噰鐢0xvalue鎴X'value'鏍煎紡灏嗚繑鍥炲肩疆浜SQL璇彞涓備絾鏄紝杩斿洖鍊间笉鍖呮嫭0x鎴X'...'銆傝皟鐢ㄨ呭繀椤绘彁渚涙墍甯屾湜鐨勬牸寮忔槸浣曠銆
绀轰緥锛
char query[1000],*end;
end = strmov(query,"INSERT INTO test_table values(");
end = strmov(end,"0x");
end += mysql_hex_string(end,"What's this",11);
end = strmov(end,",0x");
end += mysql_hex_string(end,"binary data: \0\r\n",16);
*end++ = ')';
if (mysql_real_query(&mysql,query,(unsigned int) (end - query)))
{
fprintf(stderr, "Failed to insert row, Error: %s\n",
mysql_error(&mysql));
}
绀轰緥涓墍浣跨敤鐨strmov()鍑芥暟鍖呭惈鍦mysqlclient搴撲腑锛瀹冪殑宸ヤ綔鏂瑰紡绫讳技浜strcpy()锛屼絾杩斿洖鎸囧悜绗1涓弬鏁扮粓缁Null鐨勬寚閽堛
杩斿洖鍊
缃簬鈥to鈥濅腑鐨勫肩殑闀垮害锛屼笉鍖呮嫭缁堢粨鐢Null瀛楃銆
閿欒
鏃犮
char *mysql_info(MYSQL *mysql)
鎻忚堪
妫绱㈠瓧绗︿覆锛岃瀛楃涓叉彁渚涗簡鍏充簬鏈杩戞墽琛屾煡璇㈢殑淇℃伅锛屼絾浠呭杩欓噷鍒楀嚭鐨勮鍙ユ湁鏁堛傚浜庡叾浠栬鍙ワ紝mysql_info()杩斿洖NULL銆傚瓧绗︿覆鐨勬牸寮忓彇鍐充簬鏌ヨ鐨勭被鍨嬶紝濡傛湰鑺傛墍杩般傛暟鍊间粎鏄鏄庢х殑锛屽瓧绗︿覆鍖呭惈涓庢煡璇㈢浉閫傚簲鐨勫笺
路 INSERT INTO ... SELECT ...
瀛楃涓叉牸寮忥細璁板綍锛100锛涘壇鏈紝0锛涜鍛婏紝0
路 INSERT INTO ... VALUES (...),(...),(...)...
瀛楃涓叉牸寮忥細璁板綍锛3锛涘壇鏈紝0锛涜鍛婏紝0
路 LOAD DATA INFILE ...
瀛楃涓叉牸寮忥細璁板綍锛1锛涘垹闄わ紝0锛涜烦杩囷紝0锛涜鍛婏紝0
路 ALTER TABLE
瀛楃涓叉牸寮忥細璁板綍锛3锛涘壇鏈紝0锛涜鍛婏紝0
路 UPDATE
瀛楃涓叉牸寮忥細鍖归厤琛岋紝40锛涙洿鏀癸紝40锛涜鍛婏紝0
娉ㄦ剰锛mysql_info()涓INSERT ... VALUES杩斿洖闈NULL鍊硷紝INSERT ... VALUES浠呯敤浜澶氳褰㈠紡鐨勮鍙ワ紙涔熷氨鏄锛屼粎褰撴寚瀹氫簡澶氫釜鍊煎垪琛ㄦ椂锛夈
杩斿洖鍊
瀛楃涓诧紝瀹冭〃绀烘渶杩戞墍鎵ц鏌ヨ鐨勯澶栦俊鎭傚鏋滆鏌ヨ鏃犲彲鐢ㄤ俊鎭紝杩斿洖NULL銆
閿欒
鏃犮
MYSQL *mysql_init(MYSQL *mysql)
鎻忚堪
鍒嗛厤鎴栧垵濮嬪寲涓mysql_real_connect()鐩搁傚簲鐨MYSQL瀵硅薄銆濡傛灉mysql鏄NULL鎸囬拡锛璇ュ嚱鏁板皢鍒嗛厤銆佸垵濮嬪寲銆佸苟杩斿洖鏂板璞°傚惁鍒欙紝灏嗗垵濮嬪寲瀵硅薄锛屽苟杩斿洖瀵硅薄鐨勫湴鍧銆傚鏋mysql_init()鍒嗛厤浜嗘柊鐨勫璞★紝褰撹皟鐢mysql_close()鏉ュ叧闂繛鎺ユ椂銆傚皢閲婃斁璇ュ璞°
杩斿洖鍊
鍒濆鍖栫殑MYSQL*鍙ユ焺銆傚鏋滄棤瓒冲鍐呭瓨浠ュ垎閰嶆柊鐨勫璞★紝杩斿洖NULL銆
閿欒
鍦ㄥ唴瀛樹笉瓒崇殑鎯呭喌涓嬶紝杩斿洖NULL銆
my_ulonglong mysql_insert_id(MYSQL *mysql)
鎻忚堪
杩斿洖鐢变互鍓嶇殑INSERT鎴UPDATE璇彞涓AUTO_INCREMENT鍒楃敓鎴愮殑鍊笺傚湪鍖呭惈AUTO_INCREMENT瀛楁鐨勮〃涓墽琛屼簡INSERT璇彞鍚庯紝搴斾娇鐢ㄨ鍑芥暟銆
鏇村噯纭湴璁诧紝灏嗗湪涓嬭堪鏉′欢涓嬫洿鏂mysql_insert_id()锛
路 灏嗗间繚瀛樺埌AUTO_INCREMENT鍒椾腑鐨INSERT璇彞銆傛棤璁哄兼槸閫氳繃鍦ㄥ垪涓瓨鍌ㄧ壒娈婂NULL鎴0鑷姩鐢熸垚鐨勶紝杩樻槸纭垏鐨勯潪鐗规畩鍊硷紝閮芥垚绔嬨
路 鍦ㄦ湁澶氳INSERT璇彞鐨勬儏鍐典笅锛mysql_insert_id()杩斿洖绗1涓嚜鍔ㄧ敓鎴愮殑AUTO_INCREMENT鍊硷紝濡傛灉鏈敓鎴愯繖绫诲硷紝灏嗚繑鍥炴彃鍏ュ湪AUTO_INCREMENT鍒椾腑鐨鏈鍚1涓‘鍒囧笺
路 閫氳繃灏LAST_INSERT_ID(expr)鎻掑叆鍒颁换鎰忓垪涓互鐢熸垚AUTO_INCREMENT鍊肩殑INSERT璇彞銆
路 閫氳繃鏇存柊浠绘剰鍒楄嚦LAST_INSERT_ID(expr)浠ョ敓鎴AUTO_INCREMENT鍊肩殑INSERT璇彞銆
路 mysql_insert_id()鐨勫间笉鍙楄濡SELECT绛夎繑鍥炵粨鏋滈泦鐨勮鍙ョ殑褰卞搷銆
路 濡傛灉鍓嶉潰鐨勮鍙ヨ繑鍥炰簡閿欒锛mysql_insert_id()鐨勫灏嗘槸涓嶇‘瀹氱殑銆
娉ㄦ剰锛屽鏋滃墠闈㈢殑璇彞鏈娇鐢AUTO_INCREMENT锛mysql_insert_id()杩斿洖0銆傚鏋滈渶瑕佷繚瀛樺硷紝鍦ㄧ敓鎴愬肩殑璇彞鍚庯紝鍔″繀绔嬪埢璋冪敤mysql_insert_id()銆
mysql_insert_id()鐨勫间粎鍙楀湪褰撳墠瀹㈡埛绔繛鎺ュ唴鍙戝嚭鐨勮鍙ョ殑褰卞搷銆備笉鍙楃敱鍏朵粬瀹㈡埛绔彂鍑虹殑璇彞鐨勫奖鍝嶃
璇峰弬瑙12.9.3鑺傦紝鈥滀俊鎭嚱鏁扳銆
姝ゅ杩樺簲娉ㄦ剰锛SQL LAST_INSERT_ID()鍑芥暟鐨勫兼诲寘鍚渶杩戠敓鎴愮殑AUTO_INCREMENT鍊硷紝鑰屼笖鍦ㄨ鍙ヤ箣闂翠笉浼氳澶嶄綅锛鍘熷洜鍦ㄤ簬璇ュ嚱鏁扮殑鍊兼槸鍦ㄦ湇鍔″櫒涓淮鎶ょ殑銆傚彟涓涓尯鍒槸锛屽鏋滆缃簡AUTO_INCREMENT鍒楁潵鎸囧畾闈炵壒娈婂硷紝涓嶄細鏇存柊LAST_INSERT_ID()銆
LAST_INSERT_ID()涓嶅悓浜mysql_insert_id()鐨勫師鍥犲湪浜庯紝LAST_INSERT_ID()鍦ㄨ剼鏈腑寰堝鏄撲娇鐢紝鑰mysql_insert_id()鍒欒瘯鍥炬彁渚涘叧浜庡湪AUTO_INCREMENT鍒椾腑鍑虹幇鎯呭喌鐨勬洿鍑嗙‘淇℃伅銆
杩斿洖鍊
鍦ㄥ墠闈㈢殑璁ㄨ涓簣浠ヤ簡浠嬬粛銆
閿欒
鏃犮
int mysql_kill(MYSQL *mysql, unsigned long pid)
鎻忚堪
璇锋眰鏈嶅姟鍣ㄦ潃姝荤敱pid鎸囧畾鐨勭嚎绋嬨
杩斿洖鍊
0琛ㄧず鎴愬姛锛岄潪0鍊艰〃绀哄嚭鐜伴敊璇
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
void mysql_library_end(void)
鎻忚堪
瀹冩槸mysql_server_end()鍑芥暟鐨勫悓涔夎瘝銆
鍏充簬鍏蜂綋鐨勭敤娉曪紝璇峰弬瑙25.2.2鑺傦紝鈥淐 API鍑芥暟姒傝堪鈥銆
int mysql_library_init(int argc, char **argv, char **groups)
鎻忚堪
杩欐槸mysql_server_init()鍑芥暟鐨勫悓涔夎瘝銆
鍏充簬鍏蜂綋鐨勭敤娉曪紝璇峰弬瑙25.2.2鑺傦紝鈥淐 API鍑芥暟姒傝堪鈥銆
MYSQL_RES *mysql_list_dbs(MYSQL *mysql, const char *wild)
鎻忚堪
杩斿洖鐢辨湇鍔″櫒涓婄殑鏁版嵁搴撳悕绉扮粍鎴愮殑缁撴灉闆嗭紝璇ユ湇鍔″櫒涓庣敱閫氶厤绗﹀弬鏁版寚瀹氱殑绠鍗曟鍒欒〃杈惧紡鍖归厤銆傞氶厤绗﹀弬鏁板彲浠ュ寘鍚氶厤绗︹%鈥濇垨鈥_鈥濓紝涔熷彲浠ユ槸NULL鎸囬拡锛屼互渚夸笌鎵鏈夌殑鏁版嵁搴撳尮閰嶃傝皟鐢mysql_list_dbs()鐨勬柟娉曠被浼间簬鎵ц鏌ヨSHOW database [LIKE wild]銆
蹇呴』鐢mysql_free_result()閲婃斁缁撴灉闆嗐
杩斿洖鍊
鎴愬姛鍚庤繑鍥MYSQL_RES缁撴灉闆嗐濡傛灉鍑虹幇閿欒锛岃繑鍥NULL銆
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_OUT_OF_MEMORY
鍐呭瓨婧㈠嚭銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
MYSQL_RES *mysql_list_fields(MYSQL *mysql, const char *table, const char *wild)
鎻忚堪
杩斿洖鐢辩粰瀹氳〃涓殑瀛楁鍚嶇О缁勬垚鐨勭粨鏋滈泦锛岀粰瀹氳〃涓庣敱閫氶厤绗﹀弬鏁版寚瀹氱殑绠鍗曟鍒欒〃杈惧紡鍖归厤銆傞氶厤绗﹀弬鏁板彲浠ュ寘鍚氶厤绗︹%鈥濇垨鈥_鈥濓紝涔熷彲浠ユ槸NULL鎸囬拡锛屼互渚夸笌鎵鏈夌殑瀛楁鍖归厤銆傝皟鐢mysql_list_fields()鐨勬柟娉曠被浼间簬鎵ц鏌ヨSHOW COLUMNS FROM tbl_name [LIKE wild]銆
娉ㄦ剰锛屽缓璁娇鐢SHOW COLUMNS FROM tbl_name锛岃屼笉鏄mysql_list_fields()銆
蹇呴』鐢mysql_free_result()閲婃斁缁撴灉闆嗐
杩斿洖鍊
濡傛灉鎴愬姛锛岃繑鍥MYSQL_RES缁撴灉闆嗐濡傛灉鍑虹幇閿欒锛岃繑鍥NULL銆
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
MYSQL_RES *mysql_list_processes(MYSQL *mysql)
鎻忚堪
杩斿洖鎻忚堪褰撳墠鏈嶅姟鍣ㄧ嚎绋嬬殑缁撴灉闆嗐傝绫讳俊鎭笌mysqladmin processlist鎴SHOW PROCESSLIST鏌ヨ缁欏嚭鐨勪俊鎭浉鍚屻
蹇呴』鐢mysql_free_result()閲婃斁缁撴灉闆嗐
杩斿洖鍊
濡傛灉鎴愬姛锛岃繑鍥MYSQL_RES缁撴灉闆嗐濡傛灉鍑虹幇閿欒锛岃繑鍥NULL銆
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
MYSQL_RES *mysql_list_tables(MYSQL *mysql, const char *wild)
鎻忚堪
杩斿洖鐢卞綋鍓嶆暟鎹簱鍐呯殑琛ㄥ悕缁勬垚鐨勭粨鏋滈泦锛屽綋鍓嶆暟鎹簱涓庣敱閫氶厤绗﹀弬鏁版寚瀹氱殑绠鍗曟鍒欒〃杈惧紡鍖归厤銆傞氶厤绗﹀弬鏁板彲浠ュ寘鍚氶厤绗︹%鈥濇垨鈥_鈥濓紝涔熷彲浠ユ槸NULL鎸囬拡锛屼互渚夸笌鎵鏈夌殑琛ㄥ尮閰嶃傝皟鐢mysql_list_tables()鐨勬柟娉曠被浼间簬鎵ц鏌ヨHOW tables [LIKE wild]銆
蹇呴』鐢mysql_free_result()閲婃斁缁撴灉闆嗐
杩斿洖鍊
濡傛灉鎴愬姛锛岃繑鍥MYSQL_RES缁撴灉闆嗐 濡傛灉鍑虹幇閿欒锛岃繑鍥NULL銆
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
my_bool mysql_more_results(MYSQL *mysql)
鎻忚堪
濡傛灉褰撳墠鎵ц鐨勬煡璇㈠瓨鍦ㄥ涓粨鏋滐紝杩斿洖鈥滅湡鈥濓紝鑰屼笖搴旂敤绋嬪簭蹇呴』璋冪敤mysql_next_result()鏉ヨ幏鍙栫粨鏋溿
杩斿洖鍊
濡傛灉瀛樺湪澶氫釜缁撴灉锛岃繑鍥鈥鐪鈥锛1锛夛紝濡傛灉涓嶅瓨鍦ㄥ涓粨鏋滐紝杩斿洖鈥鍋鈥锛0锛夈
鍦ㄥぇ澶氭暟鎯呭喌涓嬶紝鍙皟鐢mysql_next_result()鏉ユ祴璇曟槸鍚﹀瓨鍦ㄥ涓粨鏋滐紝濡傛灉瀛樺湪澶氫釜缁撴灉锛屽妫绱㈣繘琛屽垵濮嬪寲鎿嶄綔銆
璇峰弬瑙25.2.9鑺傦紝鈥滃鏌ヨ鎵ц鐨凜 API澶勭悊鈥銆傝鍙傝25.2.3.45鑺傦紝鈥渕ysql_next_result()鈥銆
閿欒
鏃犮
int mysql_next_result(MYSQL *mysql)
鎻忚堪
濡傛灉瀛樺湪澶氫釜鏌ヨ缁撴灉锛mysql_next_result()灏嗚鍙栦笅涓涓煡璇㈢粨鏋滐紝骞跺皢鐘舵佽繑鍥炵粰搴旂敤绋嬪簭銆
濡傛灉鍓嶉潰鐨勬煡璇㈣繑鍥炰簡缁撴灉闆嗭紝蹇呴』涓哄叾璋冪敤mysql_free_result()銆
璋冪敤浜mysql_next_result()鍚庯紝杩炴帴鐘舵佸氨鍍忎綘宸蹭负涓嬩竴鏌ヨ璋冪敤浜mysql_real_query()鎴mysql_query()鏃剁殑涓鏍枫傝繖鎰忓懗鐫浣犺兘璋冪敤mysql_store_result()銆mysql_warning_count()銆mysql_affected_rows()绛夌瓑銆
濡傛灉mysql_next_result()杩斿洖閿欒锛屽皢涓嶆墽琛屼换浣曞叾浠栬鍙ワ紝涔熶笉浼氳幏鍙栦换浣曟洿澶氱殑缁撴灉锛
璇峰弬瑙25.2.9鑺傦紝鈥滃鏌ヨ鎵ц鐨凜 API澶勭悊鈥銆
杩斿洖鍊
杩斿洖鍊 |
鎻忚堪 |
0 |
鎴愬姛骞舵湁澶氫釜缁撴灉銆 |
-1 |
鎴愬姛浣嗘病鏈夊涓粨鏋溿 |
>0 |
鍑洪敊 |
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆備緥濡傦紝娌℃湁涓哄墠闈㈢殑缁撴灉闆嗚皟鐢mysql_use_result()銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
unsigned int mysql_num_fields(MYSQL_RES *result)
瑕佹兂浼犻MYSQL*鍙傞噺鍙栬屼唬涔嬶紝璇蜂娇鐢ㄦ棤绗﹀彿鏁存暟mysql_field_count(MYSQL *mysql)銆
鎻忚堪
杩斿洖缁撴灉闆嗕腑鐨勮鏁般
娉ㄦ剰锛屼綘鍙互浠庢寚鍚戠粨鏋滈泦鐨勬寚閽堟垨鎸囧悜杩炴帴鍙ユ焺鐨勬寚閽堣幏寰楄鏁般傚鏋mysql_store_result()鎴mysql_use_result()杩斿洖NULL锛搴斾娇鐢ㄨ繛鎺ュ彞鏌勶紙鍥犺屾病鏈夌粨鏋滈泦鎸囬拡锛夈傚湪璇ユ儏鍐典笅锛屽彲璋冪敤mysql_field_count()鏉ュ垽鏂mysql_store_result()鏄惁鐢熸垚浜嗛潪绌虹粨鏋溿傝繖鏍凤紝瀹㈡埛绔▼搴忓氨鑳介噰鍙栨伆褰撶殑琛屽姩锛岃屼笉闇瑕佺煡閬撴煡璇㈡槸鍚︽槸SELECT璇彞锛堟垨绫讳技SELECT鐨勮鍙ワ級銆鍦ㄤ笅闈㈢殑绀轰緥涓紝浠嬬粛浜嗘墽琛岃鎿嶄綔鐨勬柟寮忋
璇峰弬瑙25.2.13.1鑺傦紝鈥滀负浠涔堝湪mysql_query()杩斿洖鎴愬姛鍚庯紝mysql_store_result()鏈夋椂浼氳繑鍥濶ULL鈥銆
杩斿洖鍊
琛ㄧず缁撴灉闆嗕腑琛屾暟鐨勬棤绗﹀彿鏁存暟銆
閿欒
鏃犮
绀轰緥锛
MYSQL_RES *result;
unsigned int num_fields;
unsigned int num_rows;
if (mysql_query(&mysql,query_string))
{
// error
}
else // query succeeded, process any data returned by it
{
result = mysql_store_result(&mysql);
if (result) // there are rows
{
num_fields = mysql_num_fields(result);
// retrieve rows, then call mysql_free_result(result)
}
else // mysql_store_result() returned nothing; should it have?
{
if (mysql_errno(&mysql))
{
fprintf(stderr, "Error: %s\n", mysql_error(&mysql));
}
else if (mysql_field_count(&mysql) == 0)
{
// query does not return data
// (it was not a SELECT)
num_rows = mysql_affected_rows(&mysql);
}
}
}
鍙︿竴绉嶅彲閫夋柟寮忔槸锛堝鏋滀綘鐭ラ亾浣犵殑鏌ヨ搴旇繑鍥炵粨鏋滈泦锛夛紝浣跨敤妫鏌モmysql_field_count(&mysql) is = 0鈥濇潵鏇挎崲mysql_errno(&mysql)璋冪敤銆備粎褰撳嚭閿欐椂鎵嶅簲浣跨敤瀹冦
my_ulonglong mysql_num_rows(MYSQL_RES *result)
鎻忚堪
杩斿洖缁撴灉闆嗕腑鐨勮鏁般
mysql_num_rows()鐨勪娇鐢ㄥ彇鍐充簬鏄惁閲囩敤浜mysql_store_result()鎴mysql_use_result()鏉ヨ繑鍥炵粨鏋滈泦銆濡傛灉浣跨敤浜mysql_store_result()锛屽彲浠ョ珛鍒昏皟鐢mysql_num_rows()銆傚鏋滀娇鐢ㄤ簡mysql_use_result()锛mysql_num_rows()涓嶈繑鍥炴纭殑鍊硷紝鐩磋嚦妫绱簡缁撴灉闆嗕腑鐨勬墍鏈夎涓烘銆
杩斿洖鍊
缁撴灉闆嗕腑鐨勮鏁般
閿欒
鏃犮
int mysql_options(MYSQL *mysql, enum mysql_option option, const char *arg)
鎻忚堪
鍙敤浜庤缃澶栫殑杩炴帴閫夐」锛屽苟褰卞搷杩炴帴鐨勮涓恒傚彲澶氭璋冪敤璇ュ嚱鏁版潵璁剧疆鏁颁釜閫夐」銆
搴斿湪mysql_init()涔嬪悗銆佷互鍙mysql_connect()鎴mysql_real_connect()涔嬪墠璋冪敤mysql_options()銆
閫夐」鍙傞噺鎸囩殑鏄綘鎵撶畻璁剧疆鐨勯夐」銆Arg鍙傞噺鏄夐」鐨勫笺傚鏋滈夐」鏄暣鏁帮紝閭d箞arg搴旀寚鍚鏁存暟鐨勫笺
鍙兘鐨勯夐」鍊硷細
閫夐」 |
鍙傞噺绫诲瀷 |
鍔熻兘 |
MYSQL_INIT_COMMAND |
char * |
杩炴帴鍒MySQL鏈嶅姟鍣ㄦ椂灏嗘墽琛岀殑鍛戒护銆傚啀娆¤繛鎺ユ椂灏嗚嚜鍔ㄥ湴鍐嶆鎵ц銆 |
MYSQL_OPT_COMPRESS |
鏈娇鐢 |
浣跨敤鍘嬬缉瀹㈡埛绔紡鏈嶅姟鍣ㄥ崗璁 |
MYSQL_OPT_CONNECT_TIMEOUT |
unsigned int * |
浠ョ涓哄崟浣嶇殑杩炴帴瓒呮椂銆 |
MYSQL_OPT_GUESS_CONNECTION |
鏈娇鐢 |
瀵逛簬涓libmysqld閾炬帴鐨勫簲鐢ㄧ▼搴忥紝鍏佽搴鈥鐚滄祴鈥鏄惁浣跨敤宓屽叆寮忔湇鍔″櫒鎴栬繙绋嬫湇鍔″櫒銆傗鐚滄祴鈥濊〃绀猴紝濡傛灉璁剧疆浜嗕富鏈哄悕浣嗕笉鏄湰鍦颁富鏈猴紝灏嗕娇鐢ㄨ繙绋嬫湇鍔″櫒銆傝琛屼负鏄粯璁よ涓恒 鍙娇鐢MYSQL_OPT_USE_EMBEDDED_CONNECTION 鍜MYSQL_OPT_USE_REMOTE_CONNECTION瑕嗙洊瀹冦瀵逛簬涓libmysqlclient閾炬帴鐨勫簲鐢ㄧ▼搴忥紝璇ラ夐」灏嗚蹇界暐銆 |
MYSQL_OPT_LOCAL_INFILE |
鎸囧悜鍗曞厓鐨勫彲閫夋寚閽 |
濡傛灉鏈粰瀹氭寚閽堬紝鎴栨寚閽堟寚鍚戔unsigned int != 0鈥锛屽皢鍏佽鍛戒护LOAD LOCAL INFILE銆 |
MYSQL_OPT_NAMED_PIPE |
鏈娇鐢 |
浣跨敤鍛藉悕绠¢亾杩炴帴鍒NT骞冲彴涓婄殑MySQL鏈嶅姟鍣ㄣ |
MYSQL_OPT_PROTOCOL |
unsigned int * |
瑕佷娇鐢ㄧ殑鍗忚绫诲瀷銆傚簲鏄mysql.h涓畾涔夌殑mysql_protocol_type鐨勬灇涓惧间箣涓銆 |
MYSQL_OPT_READ_TIMEOUT |
unsigned int * |
浠庢湇鍔″櫒璇诲彇淇℃伅鐨勮秴鏃讹紙鐩墠浠呭湪Windows骞冲彴鐨TCP/IP杩炴帴涓婃湁鏁堬級銆 |
MYSQL_OPT_RECONNECT |
my_bool * |
濡傛灉鍙戠幇杩炴帴涓㈠け锛屽惎鍔ㄦ垨绂佹涓庢湇鍔″櫒鐨勮嚜鍔ㄥ啀杩炴帴銆備粠MySQL 5.0.3寮濮嬶紝榛樿鎯呭喌涓嬬姝㈠啀杩炴帴锛岃繖鏄5.0.13涓殑鏂伴夐」锛屾彁渚涗簡涓绉嶄互鏄惧紡鏂瑰紡璁剧疆鍐嶈繛鎺ヨ涓虹殑鏂规硶銆 |
MYSQL_OPT_SET_CLIENT_IP |
char * |
瀵逛簬涓libmysqld閾炬帴鐨勫簲鐢ㄧ▼搴忥紙鍏峰閴村畾鏀寔鐗规х殑宸茬紪璇libmysqld锛锛屽畠鎰忓懗鐫锛屽嚭浜庨壌瀹氱洰鐨勶紝鐢ㄦ埛灏嗚瑙嗕负浠庢寚瀹氱殑IP鍦板潃锛堟寚瀹氫负瀛楃涓诧級杩涜杩炴帴銆傚浜庝笌libmysqlclient閾炬帴鐨勫簲鐢ㄧ▼搴忥紝锛岃閫夐」灏嗚蹇界暐銆 |
MYSQL_OPT_USE_EMBEDDED_CONNECTION |
鏈娇鐢 |
瀵逛簬涓libmysqld閾炬帴鐨勫簲鐢ㄧ▼搴忥紝瀵逛簬杩炴帴鏉ヨ锛屽畠灏嗗己鍒朵娇鐢ㄥ祵鍏ュ紡鏈嶅姟鍣銆傚浜庝笌libmysqlclient閾炬帴鐨勫簲鐢ㄧ▼搴忥紝锛岃閫夐」灏嗚蹇界暐銆 |
MYSQL_OPT_USE_REMOTE_CONNECTION |
鏈娇鐢 |
瀵逛簬涓libmysqld閾炬帴鐨勫簲鐢ㄧ▼搴忥紝瀵逛簬杩炴帴鏉ヨ锛屽畠灏嗗己鍒朵娇鐢ㄨ繙绋嬫湇鍔″櫒銆傚浜庝笌libmysqlclient閾炬帴鐨勫簲鐢ㄧ▼搴忥紝锛岃閫夐」灏嗚蹇界暐銆 |
MYSQL_OPT_USE_RESULT |
鏈娇鐢 |
涓嶄娇鐢ㄨ閫夐」銆 |
MYSQL_OPT_WRITE_TIMEOUT |
unsigned int * |
鍐欏叆鏈嶅姟鍣ㄧ殑瓒呮椂锛堢洰鍓嶄粎鍦Windows骞冲彴鐨TCP/IP杩炴帴涓婃湁鏁堬級銆 |
MYSQL_READ_DEFAULT_FILE |
char * |
浠庡懡鍚嶉夐」鏂囦欢鑰屼笉鏄粠my.cnf璇诲彇閫夐」銆 |
MYSQL_READ_DEFAULT_GROUP |
char * |
浠my.cnf鎴栫敤MYSQL_READ_DEFAULT_FILE鎸囧畾鐨勬枃浠朵腑鐨勫懡鍚嶇粍璇诲彇閫夐」銆 |
MYSQL_REPORT_DATA_TRUNCATION |
my_bool * |
閫氳繃MYSQL_BIND.error锛屽浜庨澶勭悊璇彞锛屽厑璁告垨绂佹閫氭姤鏁版嵁鎴柇閿欒锛堥粯璁や负绂佹锛夈 |
MYSQL_SECURE_AUTH |
my_bool* |
鏄惁杩炴帴鍒颁笉鏀寔瀵嗙爜娣风紪鍔熻兘鐨勬湇鍔″櫒锛屽湪MySQL 4.1.1鍜屾洿楂樼増鏈腑锛屼娇鐢ㄤ簡瀵嗙爜娣风紪鍔熻兘銆 |
MYSQL_SET_CHARSET_DIR |
char* |
鎸囧悜鍖呭惈瀛楃闆嗗畾涔夋枃浠剁殑鐩綍鐨勮矾寰勫悕銆 |
MYSQL_SET_CHARSET_NAME |
char* |
鐢ㄤ綔榛樿瀛楃闆嗙殑瀛楃闆嗙殑鍚嶇О銆 |
MYSQL_SHARED_MEMORY_BASE_NAME |
char* |
鍛藉悕涓轰笌鏈嶅姟鍣ㄨ繘琛岄氫俊鐨勫叡浜唴瀛樺璞°傚簲涓庝綘鎵撶畻杩炴帴鐨mysqld鏈嶅姟鍣ㄤ娇鐢ㄧ殑閫夐」鈥-shared-memory-base-name鈥鐩稿悓銆 |
娉ㄦ剰锛屽鏋滀娇鐢ㄤ簡MYSQL_READ_DEFAULT_FILE鎴MYSQL_READ_DEFAULT_GROUP锛屾讳細璇诲彇瀹㈡埛绔粍銆
閫夐」鏂囦欢涓寚瀹氱殑缁勫彲鑳藉寘鍚笅杩伴夐」锛
閫夐」 |
鎻忚堪 |
connect-timeout |
浠ョ涓哄崟浣嶇殑杩炴帴瓒呮椂銆傚湪Linux骞冲彴涓婏紝璇ヨ秴鏃朵篃鐢ㄤ綔绛夊緟鏈嶅姟鍣ㄩ娆″洖搴旂殑鏃堕棿銆 |
compress |
浣跨敤鍘嬬缉瀹㈡埛绔紡鏈嶅姟鍣ㄥ崗璁 |
database |
濡傛灉鍦ㄨ繛鎺ュ懡浠や腑鏈寚瀹氭暟鎹簱锛岃繛鎺ュ埌璇ユ暟鎹簱銆 |
debug |
璋冭瘯閫夐」銆 |
disable-local-infile |
绂佹浣跨敤LOAD DATA LOCAL銆 |
host |
榛樿涓绘満鍚嶃 |
init-command |
杩炴帴鍒MySQL鏈嶅姟鍣ㄦ椂灏嗘墽琛岀殑鍛戒护銆傚啀娆¤繛鎺ユ椂灏嗚嚜鍔ㄥ湴鍐嶆鎵ц銆 |
interactive-timeout |
绛夊悓浜庡皢CLIENT_INTERACTIVE鎸囧畾涓mysql_real_connect()銆傝鍙傝25.2.3.51鑺傦紝鈥渕ysql_real_connect()鈥銆 |
local-infile[=(0|1)] |
濡傛灉鏃犲弬閲忔垨鍙傞噺!= 0锛岄偅涔堝皢鍏佽浣跨敤LOAD DATA LOCAL銆 |
max_allowed_packet |
瀹㈡埛绔兘澶熶粠鏈嶅姟鍣ㄨ鍙栫殑鏈澶т俊鎭寘銆 |
multi-results |
鍏佽澶氳鍙ユ墽琛屾垨瀛樺偍绋嬪簭鐨勫涓粨鏋滈泦銆 |
multi-statements |
鍏佽瀹㈡埛绔湪1涓瓧绗︿覆鍐呭彂閫佸鏉¤鍙ャ傦紙鐢扁;鈥濋殧寮锛夈 |
password |
榛樿瀵嗙爜銆 |
pipe |
浣跨敤鍛藉悕绠¢亾杩炴帴鍒NT骞冲彴涓婄殑MySQL鏈嶅姟鍣ㄣ |
protocol={TCP | SOCKET | PIPE | MEMORY} |
杩炴帴鍒版湇鍔″櫒鏃跺皢浣跨敤鐨勫崗璁 |
port |
榛樿绔彛鍙枫 |
return-found-rows |
閫氱煡mysql_info()杩斿洖鍙戠幇鐨勮锛岃屼笉鏄娇鐢UPDATE鏃舵洿鏂扮殑琛銆 |
shared-memory-base-name=name |
鍏变韩鍐呭瓨鍚嶇О锛岀敤浜庤繛鎺ュ埌鏈嶅姟鍣紙榛樿涓"MYSQL"锛夈 |
socket |
榛樿鐨勫鎺ュ瓧鏂囦欢銆 |
user |
榛樿鐢ㄦ埛銆 |
娉ㄦ剰锛屸timeout鈥锛堣秴鏃讹級宸茶鈥connect-timeout鈥锛堣繛鎺ヨ秴鏃讹級鍙栦唬锛屼絾涓轰簡淇濇寔鍚戝悗鍏煎锛鍦MySQL 5.1.2-alpha涓粛鏀寔鈥timeout鈥锛堣秴鏃讹級銆
鍏充簬閫夐」鏂囦欢鐨勬洿澶氫俊鎭紝璇峰弬瑙4.3.2鑺傦紝鈥滀娇鐢ㄩ夐」鏂囦欢鈥銆
杩斿洖鍊
鎴愬姛鏃惰繑鍥0銆傚鏋滀娇鐢ㄤ簡鏈煡閫夐」锛岃繑鍥為潪0鍊笺
绀轰緥锛
MYSQL mysql;
mysql_init(&mysql);
mysql_options(&mysql,MYSQL_OPT_COMPRESS,0);
mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"odbc");
if (!mysql_real_connect(&mysql,"host","user","passwd","database",0,NULL,0))
{
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error(&mysql));
}
璇ヤ唬鐮佽姹傚鎴风浣跨敤鍘嬬缉瀹㈡埛绔紡鏈嶅姟鍣ㄥ崗璁紝骞朵粠my.cnf鏂囦欢鐨obdc閮ㄥ垎璇诲彇棰濆閫夐」銆
int mysql_ping(MYSQL *mysql)
鎻忚堪
妫鏌ヤ笌鏈嶅姟鍣ㄧ殑杩炴帴鏄惁宸ヤ綔銆傚鏋滆繛鎺ヤ涪澶憋紝灏嗚嚜鍔ㄥ皾璇曞啀杩炴帴銆
璇ュ嚱鏁板彲琚棽缃簡杈冮暱鏃堕棿鐨勫鎴风浣跨敤锛岀敤浠ユ鏌ユ湇鍔″櫒鏄惁宸插叧闂簡杩炴帴锛屽苟鍦ㄥ繀瑕佹椂鍐嶆杩炴帴銆
杩斿洖鍊
濡傛灉涓庢湇鍔″櫒鐨勮繛鎺ユ湁鏁堣繑鍥0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺傝繑鍥炵殑闈0鍊间笉琛ㄧずMySQL鏈嶅姟鍣ㄦ湰韬槸鍚﹀凡鍏抽棴锛岃繛鎺ュ彲鑳藉洜鍏朵粬鍘熷洜缁堢锛屽缃戠粶闂绛夈
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
int mysql_query(MYSQL *mysql, const char *query)
鎻忚堪
鎵ц鐢扁Null缁堢粨鐨勫瓧绗︿覆鈥濇煡璇㈡寚鍚戠殑SQL鏌ヨ銆傛甯告儏鍐典笅锛屽瓧绗︿覆蹇呴』鍖呭惈1鏉SQL璇彞锛岃屼笖涓嶅簲涓鸿鍙ユ坊鍔犵粓缁撳垎鍙凤紙鈥;鈥欙級鎴栤\g鈥銆傚鏋滃厑璁稿璇彞鎵ц锛瀛楃涓插彲鍖呭惈澶氭潯鐢卞垎鍙烽殧寮鐨勮鍙ャ傝鍙傝25.2.9鑺傦紝鈥滃鏌ヨ鎵ц鐨凜 API澶勭悊鈥銆
mysql_query()涓嶈兘鐢ㄤ簬鍖呭惈浜岃繘鍒舵暟鎹殑鏌ヨ锛屽簲浣跨敤mysql_real_query()鍙栬屼唬涔嬶紙浜岃繘鍒舵暟鎹彲鑳藉寘鍚瓧绗︹\0鈥锛mysql_query()浼氬皢璇ュ瓧绗﹁В閲婁负鏌ヨ瀛楃涓茬粨鏉燂級銆
濡傛灉甯屾湜浜嗚В鏌ヨ鏄惁搴旇繑鍥炵粨鏋滈泦锛屽彲浣跨敤mysql_field_count()杩涜妫鏌ャ傝鍙傝25.2.3.22鑺傦紝鈥渕ysql_field_count()鈥銆
杩斿洖鍊
濡傛灉鏌ヨ鎴愬姛锛岃繑鍥0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag)
鎻忚堪
mysql_real_connect()灏濊瘯涓庤繍琛屽湪涓绘満涓婄殑MySQL鏁版嵁搴撳紩鎿庡缓绔嬭繛鎺ャ傚湪浣犺兘澶熸墽琛岄渶瑕佹湁鏁MySQL杩炴帴鍙ユ焺缁撴瀯鐨勪换浣曞叾浠API鍑芥暟涔嬪墠锛mysql_real_connect()蹇呴』鎴愬姛瀹屾垚銆
鍙傛暟鐨勬寚瀹氭柟寮忓涓嬶細
路 绗1涓弬鏁板簲鏄凡鏈MYSQL缁撴瀯鐨勫湴鍧銆璋冪敤mysql_real_connect()涔嬪墠锛屽繀椤昏皟鐢mysql_init()鏉ュ垵濮嬪寲MYSQL缁撴瀯銆傞氳繃mysql_options()璋冪敤锛鍙洿鏀瑰绉嶈繛鎺ラ夐」銆傝鍙傝25.2.3.48鑺傦紝鈥渕ysql_options()鈥銆
路 鈥host鈥濈殑鍊煎繀椤绘槸涓绘満鍚嶆垨IP鍦板潃銆傚鏋溾host鈥濇槸NULL鎴栧瓧绗︿覆"localhost"锛岃繛鎺ュ皢琚涓轰笌鏈湴涓绘満鐨勮繛鎺ャ傚鏋滄搷浣滅郴缁熸敮鎸佸鎺ュ瓧锛Unix锛夋垨鍛藉悕绠¢亾锛Windows锛夛紝灏嗕娇鐢ㄥ畠浠屼笉鏄TCP/IP杩炴帴鍒版湇鍔″櫒銆
路 鈥user鈥鍙傛暟鍖呭惈鐢ㄦ埛鐨MySQL鐧诲綍ID銆傚鏋溾user鈥鏄NULL鎴栫┖瀛楃涓""锛岀敤鎴峰皢琚涓哄綋鍓嶇敤鎴枫傚湪UNIX鐜涓嬶紝瀹冩槸褰撳墠鐨勭櫥褰曞悕銆傚湪Windows ODBC涓嬶紝蹇呴』鏄庣‘鎸囧畾褰撳墠鐢ㄦ埛鍚嶃傝鍙傝26.1.9.2鑺傦紝鈥滃湪Windows涓婇厤缃甅yODBC DSN鈥銆
路 鈥passwd鈥鍙傛暟鍖呭惈鐢ㄦ埛鐨勫瘑鐮併濡傛灉鈥passwd鈥鏄NULL锛浠呬細瀵硅鐢ㄦ埛鐨勶紙鎷ユ湁1涓┖瀵嗙爜瀛楁鐨勶級鐢ㄦ埛琛ㄤ腑鐨勬潯鐩繘琛屽尮閰嶆鏌ャ傝繖鏍凤紝鏁版嵁搴撶鐞嗗憳灏辫兘鎸夌壒瀹氱殑鏂瑰紡璁剧疆MySQL鏉冮檺绯荤粺锛屾牴鎹敤鎴锋槸鍚︽嫢鏈夋寚瀹氱殑瀵嗙爜锛岀敤鎴峰皢鑾峰緱涓嶅悓鐨勬潈闄愩
娉ㄩ噴锛璋冪敤mysql_real_connect()涔嬪墠锛屼笉瑕佸皾璇曞姞瀵嗗瘑鐮侊紝瀵嗙爜鍔犲瘑灏嗙敱瀹㈡埛绔API鑷姩澶勭悊銆
路 鈥渄b鈥鏄暟鎹簱鍚嶇О銆傚鏋db涓NULL锛杩炴帴浼氬皢榛樿鐨勬暟鎹簱璁句负璇ュ笺
路 濡傛灉鈥port鈥涓嶆槸0锛鍏跺煎皢鐢ㄤ綔TCP/IP杩炴帴鐨勭鍙e彿銆傛敞鎰忥紝鈥host鈥濆弬鏁板喅瀹氫簡杩炴帴鐨勭被鍨嬨
路 濡傛灉unix_socket涓嶆槸NULL锛岃瀛楃涓叉弿杩颁簡搴斾娇鐢ㄧ殑濂楁帴瀛楁垨鍛藉悕绠¢亾銆傛敞鎰忥紝鈥host鈥濆弬鏁板喅瀹氫簡杩炴帴鐨勭被鍨嬨
路 client_flag鐨勫奸氬父涓0锛屼絾鏄紝涔熻兘灏嗗叾璁剧疆涓轰笅杩版爣蹇楃殑缁勫悎锛屼互鍏佽鐗瑰畾鍔熻兘锛
鏍囧織鍚嶇О |
鏍囧織鎻忚堪 |
CLIENT_COMPRESS |
浣跨敤鍘嬬缉鍗忚銆 |
CLIENT_FOUND_ROWS |
杩斿洖鍙戠幇鐨勮鏁帮紙鍖归厤鐨勶級锛岃屼笉鏄彈褰卞搷鐨勮鏁般 |
CLIENT_IGNORE_SPACE |
鍏佽鍦ㄥ嚱鏁板悕鍚庝娇鐢ㄧ┖鏍笺備娇鎵鏈夌殑鍑芥暟鍚嶆垚涓轰繚鐣欏瓧銆 |
CLIENT_INTERACTIVE |
鍏抽棴杩炴帴涔嬪墠锛屽厑璁interactive_timeout锛堝彇浠d簡wait_timeout锛夌鐨勪笉娲诲姩鏃堕棿銆瀹㈡埛绔殑浼氳瘽wait_timeout鍙橀噺琚涓轰細璇interactive_timeout鍙橀噺鐨勫笺 |
CLIENT_LOCAL_FILES |
鍏佽LOAD DATA LOCAL澶勭悊鍔熻兘銆 |
CLIENT_MULTI_STATEMENTS |
閫氱煡鏈嶅姟鍣紝瀹㈡埛绔彲鑳藉湪鍗曚釜瀛楃涓插唴鍙戦佸鏉¤鍙ワ紙鐢扁;鈥欓殧寮锛夈傚鏋滄湭璁剧疆璇ユ爣蹇楋紝灏嗙姝㈠璇彞鎵ц銆 |
CLIENT_MULTI_RESULTS |
閫氱煡鏈嶅姟鍣紝瀹㈡埛绔兘澶熷鐞嗘潵鑷璇彞鎵ц鎴栧瓨鍌ㄧ▼搴忕殑澶氫釜缁撴灉闆嗐傚鏋滆缃簡CLIENT_MULTI_STATEMENTS锛屽皢鑷姩璁剧疆瀹冦 |
CLIENT_NO_SCHEMA |
绂佹db_name.tbl_name.col_name璇硶銆傚畠鐢ㄤ簬ODBC銆傚鏋滀娇鐢ㄤ簡璇ヨ娉曪紝瀹冧細浣垮垎鏋愮▼搴忕敓鎴愰敊璇紝鍦ㄦ崟鑾锋煇浜ODBC绋嬪簭涓殑缂洪櫡鏃讹紝瀹冨緢鏈夌敤銆 |
CLIENT_ODBC |
瀹㈡埛绔槸ODBC瀹㈡埛绔傚畠灏mysqld鍙樺緱鏇翠负ODBC鍙嬪ソ銆 |
CLIENT_SSL |
浣跨敤SSL锛堝姞瀵嗗崗璁級銆傝閫夐」涓嶅簲鐢卞簲鐢ㄧ▼搴忚缃紝瀹冩槸鍦ㄥ鎴风搴撳唴閮ㄨ缃殑銆 |
瀵逛簬鏌愪簺鍙傛暟锛岃兘澶熶粠閫夐」鏂囦欢鑾峰緱鍙栧硷紝鑰屼笉鏄彇寰mysql_real_connect()璋冪敤涓殑纭垏鍊笺備负姝わ紝鍦ㄨ皟鐢mysql_real_connect()涔嬪墠锛屽簲涓MYSQL_READ_DEFAULT_FILE鎴MYSQL_READ_DEFAULT_GROUP閫夐」涓璧疯皟鐢mysql_options()銆傞殢鍚庯紝鍦mysql_real_connect()璋冪敤涓紝涓哄噯澶囦粠閫夐」鏂囦欢璇诲彇鍊肩殑姣忎釜鍙傛暟鎸囧畾鈥鏃犲鈥濆硷細
路 瀵逛簬host锛屾寚瀹NULL鍊兼垨绌哄瓧绗︿覆("")銆
路 瀵逛簬user锛屾寚瀹NULL鍊兼垨绌哄瓧绗︿覆銆
路 瀵逛簬passwd锛屾寚瀹NULL鍊笺傦紙瀵逛簬瀵嗙爜锛mysql_real_connect()璋冪敤涓殑绌哄瓧绗︿覆鐨勫间笉鑳借閫夐」鏂囦欢涓殑瀛楃涓茶鐩栵紝杩欐槸鍥犱负锛岀┖瀛楃涓叉槑纭寚鏄MySQL璐︽埛蹇呴』鏈夌┖瀵嗙爜锛夈
路 瀵逛簬db锛屾寚瀹NULL鍊兼垨绌哄瓧绗︿覆
路 瀵逛簬port锛屾寚瀹鈥0鈥鍊笺
路 瀵逛簬unix_socket锛鎸囧畾NULL鍊笺
瀵逛簬鏌愪竴鍙傛暟锛屽鏋滃湪閫夐」鏂囦欢涓湭鍙戠幇鍊硷紝灏嗕娇鐢ㄥ畠鐨勯粯璁ゅ硷紝濡傛湰鑺傚墠闈粙缁嶇殑閭f牱銆
杩斿洖鍊
濡傛灉杩炴帴鎴愬姛锛岃繑鍥MYSQL*杩炴帴鍙ユ焺銆傚鏋滆繛鎺ュけ璐ワ紝杩斿洖NULL銆傚浜庢垚鍔熺殑杩炴帴锛岃繑鍥炲间笌绗1涓弬鏁扮殑鍊肩浉鍚屻
閿欒
路 CR_CONN_HOST_ERROR
鏃犳硶杩炴帴鍒MySQL鏈嶅姟鍣ㄣ
路 CR_CONNECTION_ERROR
鏃犳硶杩炴帴鍒版湰鍦MySQL鏈嶅姟鍣ㄣ
路 CR_IPSOCK_ERROR
鏃犳硶鍒涘缓IP濂楁帴瀛椼
路 CR_OUT_OF_MEMORY
鍐呭瓨婧㈠嚭銆
路 CR_SOCKET_CREATE_ERROR
鏃犳硶鍒涘缓Unix濂楁帴瀛椼
路 CR_UNKNOWN_HOST
鏃犳硶鎵惧埌涓绘満鍚嶇殑IP鍦板潃銆
路 CR_VERSION_ERROR
鍗忚涓嶅尮閰嶏紝璧峰洜浜庯細璇曞浘杩炴帴鍒板叿鏈夌壒瀹氬鎴风搴擄紙璇ュ鎴风搴撲娇鐢ㄤ簡涓嶅悓鐨勫崗璁増鏈級鐨勬湇鍔″櫒銆傚鏋滀娇鐢ㄥ緢鏃╃殑瀹㈡埛绔簱鏉ュ缓绔嬩笌杈冩柊鐨勬湇鍔″櫒锛堟湭浣跨敤鈥--old-protocol鈥濋夐」寮濮嬬殑锛夌殑杩炴帴锛屽氨浼氬嚭鐜拌鎯呭喌銆
路 CR_NAMEDPIPEOPEN_ERROR
鏃犳硶鍦Windows骞冲彴涓嬪垱寤哄懡鍚嶇閬撱
路 CR_NAMEDPIPEWAIT_ERROR
鍦Windows骞冲彴涓嬬瓑寰呭懡鍚嶇閬撳け璐ャ
路 CR_NAMEDPIPESETSTATE_ERROR
鍦Windows骞冲彴涓嬭幏鍙栫閬撳鐞嗙▼搴忓け璐ャ
路 CR_SERVER_LOST
濡傛灉connect_timeout > 0锛岃屼笖鍦ㄨ繛鎺ユ湇鍔″櫒鏃舵墍鐢ㄦ椂闂撮暱浜connect_timeout绉掞紝鎴栧湪鎵цinit-command鏃舵湇鍔″櫒娑堝け銆
绀轰緥锛
MYSQL mysql;
mysql_init(&mysql);
mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"your_prog_name");
if (!mysql_real_connect(&mysql,"host","user","passwd","database",0,NULL,0))
{
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error(&mysql));
}
閫氳繃浣跨敤mysql_options()锛MySQL搴撳皢璇诲彇my.cnf鏂囦欢鐨[client]鍜[your_prog_name]閮ㄥ垎锛屼互纭繚绋嬪簭宸ヤ綔锛屽嵆浣挎煇浜轰互鏌愮闈炴爣鍑嗙殑鏂瑰紡璁剧疆MySQL涔熷悓鏍枫
娉ㄦ剰锛屼竴鏃﹀缓绔嬩簡杩炴帴锛mysql_real_connect()灏嗚缃啀杩炴帴鏍囧織锛MYSQL缁撴瀯鐨勭粍鎴愰儴浠斤級鐨勫硷紝鍦ㄤ綆浜5.0.3鐗堢殑API涓紝灏嗗叾璁句负鈥1鈥濓紝鍦ㄨ緝鏂扮殑鐗堟湰涓紝灏嗗叾璁句负鈥0鈥濄傚浜庤鏍囧織锛屽尖1鈥濊〃绀猴紝濡傛灉鍥犺繛鎺ヤ涪澶辫屾棤娉曟墽琛岃鍙ワ紝鏀惧純鍓嶏紝灏嗗皾璇曞啀娆¤繛鎺ュ埌鏈嶅姟鍣ㄣ備粠MySQL 5.0.13寮濮嬶紝鍙互瀵mysql_options()浣跨敤MYSQL_OPT_RECONNECT閫夐」锛屽鍐嶈繛鎺ヨ涓鸿繘琛屾帶鍒躲
unsigned long mysql_real_escape_string(MYSQL *mysql, char *to, const char *from, unsigned long length)
娉ㄦ剰锛mysql蹇呴』鏄湁鏁堢殑寮鏀惧紡杩炴帴銆涔嬫墍浠ラ渶瑕佸畠鏄洜涓猴紝杞箟鍔熻兘鍙栧喅浜庢湇鍔″櫒浣跨敤鐨勫瓧绗﹂泦銆
鎻忚堪
璇ュ嚱鏁扮敤浜庡垱寤哄彲鍦SQL璇彞涓娇鐢ㄧ殑鍚堟硶SQL瀛楃涓层傝鍙傝9.1.1鑺傦紝鈥滃瓧绗︿覆鈥銆
鎸夌収杩炴帴鐨勫綋鍓嶅瓧绗﹂泦锛屽皢鈥from鈥濅腑鐨勫瓧绗︿覆缂栫爜涓鸿浆涔SQL瀛楃涓层傚皢缁撴灉缃簬鈥to鈥涓紝骞舵坊鍔1涓粓缁撶敤NULL瀛楄妭銆傜紪鐮佺殑瀛楃涓NUL (ASCII 0)銆佲\n鈥欍佲\r鈥欍佲\鈥欍佲'鈥欍佲"鈥欍佷互鍙Control-Z锛堣鍙傝9.1鑺傦紝鈥滄枃瀛楀尖锛夈傦紙涓ユ牸鍦拌锛MySQL浠呴渶瑕佸弽鏂滄潬鍜屽紩鍙峰瓧绗︼紝鐢ㄤ簬寮曠敤杞箟鏌ヨ涓殑瀛楃涓层傝鍑芥暟鑳藉紩鐢ㄥ叾浠栧瓧绗︼紝浠庤屼娇寰楀畠浠湪鏃ュ織鏂囦欢涓叿鏈夋洿濂界殑鍙鎬э級銆
鈥from鈥濇寚鍚戠殑瀛楃涓插繀椤绘槸闀垮害瀛楄妭鈥long鈥濄傚繀椤讳负鈥to鈥濈紦鍐插尯鍒嗛厤鑷冲皯length*2+1瀛楄妭銆鍦ㄦ渶鍧忕殑鎯呭喌涓嬶紝姣忎釜瀛楃鎴栬闇瑕佷娇鐢2涓瓧鑺傝繘琛岀紪鐮侊紝鑰屼笖杩橀渶瑕佺粓缁Null瀛楄妭銆傚綋mysql_real_escape_string()杩斿洖鏃讹紝鈥to鈥濈殑鍐呭鏄敱Null缁堢粨鐨勫瓧绗︿覆銆傝繑鍥炲兼槸缂栫爜瀛楃涓茬殑闀垮害锛屼笉鍖呮嫭缁堢粨鐢Null瀛楃銆
濡傛灉闇瑕佹洿鏀硅繛鎺ョ殑瀛楃闆嗭紝搴斾娇鐢mysql_set_character_set()鍑芥暟锛岃屼笉鏄墽琛SET NAMES (鎴SET CHARACTER SET)璇彞銆mysql_set_character_set()鐨勫伐浣滄柟寮忕被浼间簬SET NAMES锛屼絾瀹冭繕鑳藉奖鍝mysql_real_escape_string()鎵浣跨敤鐨勫瓧绗﹂泦锛岃SET NAMES鍒欎笉鑳姐
绀轰緥锛
char query[1000],*end;
end = strmov(query,"INSERT INTO test_table values(");
*end++ = '\'';
end += mysql_real_escape_string(&mysql, end,"What's this",11);
*end++ = '\'';
*end++ = ',';
*end++ = '\'';
end += mysql_real_escape_string(&mysql, end,"binary data: \0\r\n",16);
*end++ = '\'';
*end++ = ')';
if (mysql_real_query(&mysql,query,(unsigned int) (end - query)))
{
fprintf(stderr, "Failed to insert row, Error: %s\n",
mysql_error(&mysql));
}
璇ョず渚嬩腑浣跨敤鐨strmov()鍑芥暟鍖呭惈鍦mysqlclient搴撲腑锛屽伐浣滄柟寮忎笌strcpy()绫讳技锛浣嗕細杩斿洖鎸囧悜绗1涓弬鏁扮粓缁撶敤Null鐨勬寚閽堛
杩斿洖鍊
缃簬鈥to鈥濅腑鐨勫肩殑闀垮害锛屼笉鍖呮嫭缁堢粨鐢Null瀛楃銆
閿欒
鏃犮
int mysql_real_query(MYSQL *mysql, const char *query, unsigned long length)
鎻忚堪
鎵ц鐢扁query鈥濇寚鍚戠殑SQL鏌ヨ锛屽畠搴旀槸瀛楃涓查暱搴﹀瓧鑺傗long鈥濄傛甯告儏鍐典笅锛瀛楃涓插繀椤诲寘鍚1鏉SQL璇彞锛岃屼笖涓嶅簲涓鸿鍙ユ坊鍔缁堢粨鍒嗗彿锛堚;鈥欙級鎴栤\g鈥濄傚鏋滃厑璁稿璇彞鎵ц锛屽瓧绗︿覆鍙寘鍚敱鍒嗗彿闅斿紑鐨勫鏉¤鍙ャ傝鍙傝25.2.9鑺傦紝鈥滃鏌ヨ鎵ц鐨凜 API澶勭悊鈥銆
瀵逛簬鍖呭惈浜岃繘鍒舵暟鎹殑鏌ヨ锛屽繀椤讳娇鐢mysql_real_query()鑰屼笉鏄mysql_query()锛岃繖鏄洜涓猴紝浜岃繘鍒舵暟鎹彲鑳戒細鍖呭惈鈥\0鈥瀛楃銆傛澶栵紝mysql_real_query()姣mysql_query()蹇紝杩欐槸鍥犱负瀹冧笉浼氬湪鏌ヨ瀛楃涓蹭笂璋冪敤strlen()銆
濡傛灉甯屾湜鐭ラ亾鏌ヨ鏄惁搴旇繑鍥炵粨鏋滈泦锛屽彲浣跨敤mysql_field_count()杩涜妫鏌25.2.3.22鑺傦紝鈥渕ysql_field_count()鈥銆
杩斿洖鍊
濡傛灉鏌ヨ鎴愬姛锛岃繑鍥0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
int mysql_refresh(MYSQL *mysql, unsigned int options)
鎻忚堪
璇ュ嚱鏁扮敤浜庡埛鏂拌〃鎴栭珮閫熺紦鍐诧紝鎴栧浣嶅鍒舵湇鍔″櫒淇℃伅銆傝繛鎺ョ殑鐢ㄦ埛蹇呴』鍏锋湁RELOAD鏉冮檺銆
鈥options鈥鍙傞噺鏄涓绉嶄綅鎺╃爜锛岀敱涓嬭堪鍊肩殑浠绘剰缁勫悎鏋勬垚銆傝兘澶熶互鈥Or鈥濓紙鎴栵級鏂瑰紡灏嗗涓肩粍鍚堝湪涓璧凤紝鐢ㄤ竴娆¤皟鐢ㄦ墽琛屽椤规搷浣溿
路 REFRESH_GRANT
鍒锋柊鎺堟潈琛紝涓FLUSH PRIVILEGES绫讳技銆
路 REFRESH_LOG
鍒锋柊鏃ュ織锛屼笌FLUSH LOGS绫讳技銆
路 REFRESH_TABLES
鍒锋柊琛ㄩ珮閫熺紦鍐诧紝涓FLUSH TABLES绫讳技銆
路 REFRESH_HOSTS
鍒锋柊涓绘満楂橀熺紦鍐诧紝涓FLUSH HOSTS绫讳技銆
路 REFRESH_STATUS
澶嶄綅鐘舵佸彉閲忥紝涓FLUSH STATUS绫讳技銆
路 REFRESH_THREADS
鍒锋柊绾跨▼楂橀熺紦鍐层
路 REFRESH_SLAVE
鍦ㄤ粠澶嶅埗鏈嶅姟鍣ㄤ笂锛屽浣嶄富鏈嶅姟鍣ㄤ俊鎭紝骞堕噸鏂板惎鍔ㄤ粠鏈嶅姟鍣紝涓RESET SLAVE绫讳技銆
路 REFRESH_MASTER
鍦ㄤ富澶嶅埗鏈嶅姟鍣ㄤ笂锛屽垹闄や簩杩涘埗鏃ュ織绱㈠紩涓垪鍑虹殑浜岃繘鍒舵棩蹇楁枃浠讹紝骞舵埅鐭储寮曟枃浠讹紝涓RESET MASTER绫讳技銆
杩斿洖鍊
0琛ㄧず鎴愬姛锛岄潪0鍊艰〃绀哄嚭鐜伴敊璇
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
int mysql_reload(MYSQL *mysql)
鎻忚堪
璇锋眰MySQL鏈嶅姟鍣ㄩ噸鏂板姞杞芥巿鏉冭〃銆傝繛鎺ョ殑鐢ㄦ埛蹇呴』鍏锋湁RELOAD鏉冮檺銆
璇ュ嚱鏁板凡杩囨椂銆傛渶濂戒娇鐢mysql_query()鏉ュ彂鍑SQL FLUSH PRIVILEGES璇彞銆
杩斿洖鍊
0琛ㄧず鎴愬姛锛岄潪0鍊艰〃绀哄嚭鐜伴敊璇
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
my_bool mysql_rollback(MYSQL *mysql)
鎻忚堪
鍥炴粴褰撳墠浜嬪姟銆
璇ュ嚱鏁扮殑鍔ㄤ綔鍙栧喅浜completion_type绯荤粺鍙橀噺鐨勫笺灏ゅ叾鏄紝濡傛灉completion_type鐨勫间负鈥2鈥锛岀粓缁撲簨鍔″悗锛屾湇鍔″櫒灏嗘墽琛岄噴鏀炬搷浣滐紝骞跺叧闂鎴风杩炴帴銆傚鎴风绋嬪簭搴旇皟鐢mysql_close()锛浠庡鎴风涓渚у叧闂繛鎺ャ
杩斿洖鍊
濡傛灉鎴愬姛锛岃繑鍥0锛屽鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
鏃犮
MYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET offset)
鎻忚堪
灏嗚鍏夋爣缃簬鏌ヨ缁撴灉闆嗕腑鐨勪换鎰忚銆傗offset鈥鍊兼槸琛屽亸绉婚噺锛屽畠搴旀槸浠mysql_row_tell()鎴mysql_row_seek()杩斿洖鐨勫笺傝鍊间笉鏄缂栧彿锛屽鏋滀綘鎵撶畻鎸夌紪鍙锋煡鎵剧粨鏋滈泦涓殑琛岋紝璇蜂娇鐢mysql_data_seek()銆
璇ュ嚱鏁拌姹傚湪缁撴灉闆嗙殑缁撴瀯涓寘鍚煡璇㈢殑鍏ㄩ儴缁撴灉锛屽洜姝わ紝mysql_row_seek()浠呭簲涓mysql_store_result()涓璧蜂娇鐢紝鑰屼笉鏄笌mysql_use_result()銆
杩斿洖鍊
琛屽厜鏍囩殑鍓嶄竴涓笺傝鍊煎彲浼犻掔粰瀵mysql_row_seek()鐨勫悗缁皟鐢ㄣ
閿欒
鏃犮
MYSQL_ROW_OFFSET mysql_row_tell(MYSQL_RES *result)
鎻忚堪
瀵逛簬涓婁竴涓mysql_fetch_row()锛杩斿洖琛屽厜鏍囩殑褰撳墠浣嶇疆銆傝鍊煎彲鐢ㄤ綔mysql_row_seek()鐨勫弬閲忋
浠呭簲鍦mysql_store_result()涔嬪悗锛岃屼笉鏄mysql_use_result()涔嬪悗浣跨敤mysql_row_tell()銆
杩斿洖鍊
琛屽厜鏍囩殑褰撳墠鍋忕Щ閲忋
閿欒
鏃犮
int mysql_select_db(MYSQL *mysql, const char *db)
鎻忚堪
浣跨敱db鎸囧畾鐨勬暟鎹簱鎴愪负鐢mysql鎸囧畾鐨勮繛鎺ヤ笂鐨榛樿鏁版嵁搴擄紙褰撳墠鏁版嵁搴擄級銆傚湪鍚庣画鏌ヨ涓紝璇ユ暟鎹簱灏嗘槸鏈寘鍚槑纭暟鎹簱鍖哄垎绗︾殑琛ㄥ紩鐢ㄧ殑榛樿鏁版嵁搴撱
闄ら潪宸茶繛鎺ョ殑鐢ㄦ埛鍏锋湁浣跨敤鏁版嵁搴撶殑鏉冮檺锛屽惁鍒mysql_select_db()灏嗗け璐ャ
杩斿洖鍊
0琛ㄧず鎴愬姛锛岄潪0鍊艰〃绀哄嚭鐜伴敊璇
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
int mysql_set_character_set(MYSQL *mysql, char *csname)
鎻忚堪
璇ュ嚱鏁扮敤浜庝负褰撳墠杩炴帴璁剧疆榛樿鐨勫瓧绗﹂泦銆傚瓧绗︿覆csname鎸囧畾浜1涓湁鏁堢殑瀛楃闆嗗悕绉般傝繛鎺ユ牎瀵规垚涓哄瓧绗﹂泦鐨勯粯璁ゆ牎瀵广傝鍑芥暟鐨勫伐浣滄柟寮忎笌SET NAMES璇彞绫讳技锛浣嗗畠杩樿兘璁剧疆mysql->charset鐨勫硷紝浠庤屽奖鍝嶄簡鐢mysql_real_escape_string()璁剧疆鐨勫瓧绗﹂泦銆
璇ュ嚱鏁版槸鍦MySQL 5.0.7涓鍔犵殑銆
杩斿洖鍊
0琛ㄧず鎴愬姛锛岄潪0鍊艰〃绀哄嚭鐜伴敊璇
绀轰緥锛
MYSQL mysql;
mysql_init(&mysql);
if (!mysql_real_connect(&mysql,"host","user","passwd","database",0,NULL,0))
{
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error(&mysql));
}
if (!mysql_set_charset_name(&mysql, "utf8"))
{
printf("New client character set: %s\n", mysql_character_set_name(&mysql));
}
int mysql_set_server_option(MYSQL *mysql, enum enum_mysql_set_option option)
鎻忚堪
鍏佽鎴栫姝㈣繛鎺ョ殑閫夐」銆閫夐」鍙互鍙栦笅杩板间箣涓锛
MYSQL_OPTION_MULTI_STATEMENTS_ON |
鍏佽澶氳鍙ユ敮鎸併 |
MYSQL_OPTION_MULTI_STATEMENTS_OFF |
绂佹澶氳鍙ユ敮鎸併 |
杩斿洖鍊
0琛ㄧず鎴愬姛锛岄潪0鍊艰〃绀哄嚭鐜伴敊璇
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 ER_UNKNOWN_COM_ERROR
鏈嶅姟鍣ㄤ笉鏀寔mysql_set_server_option()锛堝綋鏈嶅姟鍣ㄧ増鏈綆浜4.1.1鏃讹級锛屾垨鏈嶅姟鍣ㄤ笉鏀寔璇曞浘璁剧疆鐨勯夐」銆
int mysql_shutdown(MYSQL *mysql, enum enum_shutdown_level shutdown_level)
鎻忚堪
璇锋眰鏁版嵁搴撴湇鍔″櫒鍏抽棴銆傚凡杩炴帴鐨勭敤鎴峰繀椤诲叿鏈SHUTDOWN鏉冮檺銆MySQL 5.1鏈嶅姟鍣ㄤ粎鏀寔1绉嶅叧闂被鍨嬶紝shutdown_level蹇呴』绛夋晥浜嶴HUTDOWN_DEFAULT銆傝璁¤鍒掍簡棰濆鐨勫叧闂骇鍒紝浠ヤ究鑳藉閫夋嫨鎵闇鐨勭骇鍒傚浜庣敤鏃х増鏈libmysqlclient澶存枃浠剁紪璇戝苟璋冪敤mysql_shutdown()鐨勫姩鎬侀摼鎺ュ彲鎵ц绋嬪簭锛岄渶瑕佷笌鏃х増鐨libmysqlclient鍔ㄦ佸簱涓璧蜂娇鐢ㄣ
鍦5.5鑺傦紝鈥淢ySQL鏈嶅姟鍣ㄥ叧鏈鸿繘绋嬧涓紝浠嬬粛浜嗗叧鏈鸿繘绋嬨
杩斿洖鍊
0琛ㄧず鎴愬姛锛岄潪0鍊艰〃绀哄嚭鐜伴敊璇
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
const char *mysql_sqlstate(MYSQL *mysql)
鎻忚堪
杩斿洖鐢Null缁堢粨鐨勫瓧绗︿覆锛岃瀛楃涓插寘鍚叧浜庝笂娆¢敊璇殑SQLSTATE閿欒浠g爜銆傞敊璇唬鐮佸寘鍚5涓瓧绗︺'00000'琛ㄧず鏃犻敊璇傚叾鍊肩敱ANSI SQL鍜ODBC鎸囧畾銆傚叧浜庡彲鑳藉彇鍊肩殑鍒楄〃锛岃鍙傝闄勫綍B锛閿欒浠g爜鍜屾秷鎭銆
娉ㄦ剰锛屽苟闈炴墍鏈夌殑MySQL閿欒鍧囦細琚槧灏勫埌SQLSTATE閿欒浠g爜銆傚'HY000'锛堜竴鑸敊璇級鐢ㄤ簬鏈槧灏勭殑閿欒銆
杩斿洖鍊
鍖呭惈SQLSTATE閿欒鐮佺殑銆佺敱Null缁堢粨鐨勫瓧绗︿覆銆
鍙﹁鍙傝锛
璇峰弬瑙25.2.3.14鑺傦紝鈥渕ysql_errno()鈥銆傝鍙傝25.2.3.15鑺傦紝鈥渕ysql_error()鈥銆傝鍙傝25.2.7.26鑺傦紝鈥渕ysql_stmt_sqlstate()鈥銆
int mysql_ssl_set(MYSQL *mysql, const char *key, const char *cert, const char *ca, const char *capath, const char *cipher)
鎻忚堪
浣跨敤mysql_ssl_set()锛屽彲閲囩敤SSL寤虹珛瀹夊叏杩炴帴銆傚繀椤诲湪mysql_real_connect()涔嬪墠璋冪敤瀹冦
闄ら潪鍦ㄥ鎴风搴撲腑鍏佽浜OpenSSL鏀寔锛屽惁鍒mysql_ssl_set()涓嶄綔浠讳綍浜嬨
Mysql鏄粠mysql_init()杩斿洖鐨勮繛鎺ュ鐞嗙▼搴忋鍏朵粬鍙傛暟鐨勬寚瀹氬涓嬶細
路 key鏄key鏂囦欢鐨勮矾寰勫悕銆
路 cert鏄瘉涔︽枃浠剁殑璺緞鍚嶃
路 ca鏄瘉涔︽巿鏉冩枃浠剁殑璺緞鍚嶃
路 capath鏄寚鍚戠洰褰曠殑璺緞鍚嶏紝璇ョ洰褰曚腑鍖呭惈浠pem鏍煎紡缁欏嚭鐨勫彈淇′换SSL CA璇佷功銆
路 cipher鏄厑璁稿瘑鐮佺殑鍒楄〃锛岀敤浜SSL鍔犲瘑銆
瀵逛簬浠讳綍鏈娇鐢ㄧ殑SSL鍙傛暟锛屽彲涓哄叾缁欏畾NULL銆
杩斿洖鍊
璇ュ嚱鏁版昏繑鍥0銆傚鏋SSL璁剧疆涓嶆纭紝褰撲綘灏濊瘯杩炴帴鏃讹紝mysql_real_connect()灏嗚繑鍥為敊璇
char *mysql_stat(MYSQL *mysql)
鎻忚堪
杩斿洖鍖呭惈鐗瑰畾淇℃伅鐨勫瓧绗︿覆锛岃淇℃伅涓mysqladmin status鍛戒护鎻愪緵鐨勪俊鎭被浼笺傚寘鎷互绉掍负鍗曚綅鐨勬甯歌繍琛屾椂闂达紝浠ュ強杩愯绾跨▼鐨勬暟鐩紝闂鏁帮紝鍐嶅姞杞芥鏁帮紝浠ュ強鎵撳紑鐨勮〃鏁扮洰銆
杩斿洖鍊
鎻忚堪鏈嶅姟鍣ㄧ姸鎬佺殑瀛楃闆嗐濡傛灉鍑虹幇閿欒锛岃繑鍥NULL銆
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
MYSQL_RES *mysql_store_result(MYSQL *mysql)
鎻忚堪
瀵逛簬鎴愬姛妫绱簡鏁版嵁鐨勬瘡涓煡璇紙SELECT銆SHOW銆DESCRIBE銆EXPLAIN銆CHECK TABLE绛锛夛紝蹇呴』璋冪敤mysql_store_result()鎴mysql_use_result() 銆
瀵逛簬鍏朵粬鏌ヨ锛屼笉闇瑕佽皟鐢mysql_store_result()鎴mysql_use_result()锛屼絾鏄鏋滃湪浠讳綍鎯呭喌涓嬪潎璋冪敤浜mysql_store_result()锛瀹冧篃涓嶄細瀵艰嚧浠讳綍浼ゅ鎴栨ц兘闄嶄綆銆傞氳繃妫鏌mysql_store_result()鏄惁杩斿洖0锛屽彲妫娴嬫煡璇㈡槸鍚︽病鏈夌粨鏋滈泦锛堜互鍚庝細鏇村锛夈
濡傛灉甯屾湜浜嗚В鏌ヨ鏄惁搴旇繑鍥炵粨鏋滈泦锛屽彲浣跨敤mysql_field_count()杩涜妫鏌ャ傝鍙傝25.2.3.22鑺傦紝鈥渕ysql_field_count()鈥銆
mysql_store_result()灏嗘煡璇㈢殑鍏ㄩ儴缁撴灉璇诲彇鍒板鎴风锛鍒嗛厤1涓MYSQL_RES缁撴瀯锛骞跺皢缁撴灉缃簬璇ョ粨鏋勪腑銆
濡傛灉鏌ヨ鏈繑鍥炵粨鏋滈泦锛mysql_store_result()灏嗚繑鍥Null鎸囬拡锛堜緥濡傦紝濡傛灉鏌ヨ鏄INSERT璇彞锛夈
濡傛灉璇诲彇缁撴灉闆嗗け璐ワ紝mysql_store_result()杩樹細杩斿洖Null鎸囬拡銆傞氳繃妫鏌mysql_error()鏄惁杩斿洖闈炵┖瀛楃涓诧紝mysql_errno()鏄惁杩斿洖闈0鍊硷紝鎴mysql_field_count()鏄惁杩斿洖0锛屽彲浠ユ鏌ユ槸鍚﹀嚭鐜颁簡閿欒銆
濡傛灉鏈繑鍥炶锛屽皢杩斿洖绌虹殑缁撴灉闆嗐傦紙绌虹粨鏋滈泦璁剧疆涓嶅悓浜庝綔涓鸿繑鍥炲肩殑绌烘寚閽堬級銆
涓鏃﹁皟鐢ㄤ簡mysql_store_result()骞惰幏寰椾簡涓嶆槸Null鎸囬拡鐨勭粨鏋滐紝鍙皟鐢mysql_num_rows()鏉ユ壘鍑虹粨鏋滈泦涓殑琛屾暟銆
鍙互璋冪敤mysql_fetch_row()鏉ヨ幏鍙栫粨鏋滈泦涓殑琛岋紝鎴栬皟鐢mysql_row_seek()鍜mysql_row_tell()鏉ヨ幏鍙栨垨璁剧疆缁撴灉闆嗕腑鐨勫綋鍓嶈浣嶇疆銆
涓鏃﹀畬鎴愪簡瀵圭粨鏋滈泦鐨勬搷浣滐紝蹇呴』璋冪敤mysql_free_result()銆
璇峰弬瑙25.2.13.1鑺傦紝鈥滀负浠涔堝湪mysql_query()杩斿洖鎴愬姛鍚庯紝mysql_store_result()鏈夋椂浼氳繑鍥濶ULL鈥.
杩斿洖鍊
鍏锋湁澶氫釜缁撴灉鐨MYSQL_RES缁撴灉闆嗗悎銆濡傛灉鍑虹幇閿欒锛岃繑鍥NULL銆
閿欒
濡傛灉鎴愬姛锛mysql_store_result()灏澶嶄綅mysql_error()鍜mysql_errno()銆
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_OUT_OF_MEMORY
鍐呭瓨婧㈠嚭銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
unsigned long mysql_thread_id(MYSQL *mysql)
鎻忚堪
杩斿洖褰撳墠杩炴帴鐨勭嚎绋ID銆傝鍊煎彲鐢ㄤ綔mysql_kill()鐨勫弬閲忎互鏉姝荤嚎绋嬨
濡傛灉杩炴帴涓㈠け锛屽苟浣跨敤mysql_ping()杩涜浜嗗啀杩炴帴锛绾跨▼ID灏嗘敼鍙樸傝繖鎰忓懗鐫浣犱笉搴旇幏鍙栫嚎绋ID骞朵繚瀛樺畠渚涗互鍚庝娇鐢ㄣ傚簲鍦ㄩ渶瑕佹椂鑾峰彇瀹冦
杩斿洖鍊
褰撳墠杩炴帴鐨勭嚎绋ID銆
閿欒
鏃犮
MYSQL_RES *mysql_use_result(MYSQL *mysql)
鎻忚堪
瀵逛簬鎴愬姛妫绱㈡暟鎹殑姣忎釜鏌ヨ锛SELECT銆SHOW銆DESCRIBE銆EXPLAIN锛夛紝蹇呴』璋冪敤mysql_store_result()鎴mysql_use_result()銆
mysql_use_result()灏嗗垵濮嬪寲缁撴灉闆嗘绱紝浣嗗苟涓嶅儚mysql_store_result()閭f牱灏嗙粨鏋滈泦瀹為檯璇诲彇鍒板鎴风銆瀹冨繀椤婚氳繃瀵mysql_fetch_row()鐨勮皟鐢紝瀵规瘡涓琛屽垎鍒繘琛屾绱€杩欏皢鐩存帴浠庢湇鍔″櫒璇诲彇缁撴灉锛岃屼笉浼氬皢鍏朵繚瀛樺湪涓存椂琛ㄦ垨鏈湴缂撳啿鍖哄唴锛屼笌mysql_store_result()鐩告瘮锛岄熷害鏇村揩鑰屼笖浣跨敤鐨勫唴瀛樹篃鏇村皯銆傚鎴风浠呬负褰撳墠琛屽拰閫氫俊缂撳啿鍖哄垎閰嶅唴瀛橈紝鍒嗛厤鐨勫唴瀛樺彲澧炲姞鍒max_allowed_packet瀛楄妭銆
鍙︿竴鏂归潰锛屽鏋滀綘姝e湪瀹㈡埛绔竴渚т负鍚勮杩涜澶ч噺鐨勫鐞嗘搷浣滐紝鎴栬呭皢杈撳嚭鍙戦佸埌浜嗙敤鎴峰彲鑳戒細閿叆鈥^S鈥濓紙鍋滄婊氬姩锛夌殑灞忓箷锛屽氨涓嶅簲浣跨敤mysql_use_result()銆傝繖浼氱粦瀹氭湇鍔″櫒锛屽苟闃绘鍏朵粬绾跨▼鏇存柊浠讳綍琛紙鏁版嵁浠庤繖绫昏〃鑾峰緱锛夈
浣跨敤mysql_use_result()鏃讹紝蹇呴』鎵цmysql_fetch_row()锛岀洿鑷宠繑鍥NULL鍊硷紝鍚﹀垯锛屾湭鑾峰彇鐨勮灏嗕綔涓轰笅涓涓绱㈢殑涓閮ㄥ垎杩斿洖銆C API缁欏嚭鍛戒护涓嶅悓姝ラ敊璇紝濡傛灉蹇樿浜嗘墽琛岃鎿嶄綔锛屽皢涓嶈兘杩愯璇ュ懡浠ゃ
涓嶅簲涓庝粠mysql_use_result()杩斿洖鐨勭粨鏋滀竴璧蜂娇鐢mysql_data_seek()銆mysql_row_seek()銆mysql_row_tell()銆mysql_num_rows()鎴mysql_affected_rows()锛屼篃涓嶅簲鍙戝嚭鍏朵粬鏌ヨ锛岀洿鑷mysql_use_result()瀹屾垚涓烘銆傦紙浣嗘槸锛屾彁鍙栦簡鎵鏈夎鍚庯紝mysql_num_rows()灏嗗噯纭繑鍥炴彁鍙栫殑琛屾暟锛夈
涓鏃﹀畬鎴愪簡瀵圭粨鏋滈泦鐨勬搷浣滐紝蹇呴』璋冪敤mysql_free_result()銆
浣跨敤libmysqld宓屽叆寮忔湇鍔″櫒鏃讹紝鐢变簬鍦ㄨ皟鐢mysql_free_result()涔嬪墠锛屽唴瀛樹娇鐢ㄥ皢闅忕潃姣忎釜妫绱㈢殑琛屽鍔狅紝鍐呭瓨鏁堢泭灏嗗熀鏈抚澶便
杩斿洖鍊
MYSQL_RES缁撴灉缁撴瀯銆濡傛灉鍑虹幇閿欒锛岃繑鍥NULL銆
閿欒
濡傛灉鎴愬姛锛mysql_use_result()灏嗗浣mysql_error()鍜mysql_errno()銆
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_OUT_OF_MEMORY
鍐呭瓨婧㈠嚭銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
MySQL瀹㈡埛绔紡鏈嶅姟鍣ㄥ崗璁彁渚涗簡棰勫鐞嗚鍙ャ傝鍔熻兘閲囩敤浜嗙敱mysql_stmt_init()鍒濆鍖栧嚱鏁拌繑鍥炵殑MYSQL_STMT璇彞澶勭悊绋嬪簭鏁版嵁缁撴瀯銆傚浜庡娆℃墽琛岀殑璇彞锛岄澶勭悊鎵ц鏄竴绉嶆湁鏁堢殑鏂瑰紡銆棣栧厛瀵硅鍙ヨ繘琛岃В鏋愶紝涓烘墽琛屼綔濂藉噯澶囥傛帴涓嬫潵锛屽湪浠ュ悗浣跨敤鍒濆鍖栧嚱鏁拌繑鍥炵殑璇彞鍙ユ焺鎵ц涓娆℃垨澶氭銆
瀵逛簬澶氭鎵ц鐨勮鍙ワ紝棰勫鐞嗘墽琛屾瘮鐩存帴鎵ц蹇紝涓昏鍘熷洜鍦ㄤ簬锛屼粎瀵规煡璇㈡墽琛屼竴娆¤В鏋愭搷浣溿傚湪鐩存帴鎵ц鐨勬儏鍐典笅锛屾瘡娆℃墽琛岃鍙ユ椂锛屽潎灏嗚繘琛屾煡璇€傛澶栵紝鐢变簬姣忔鎵ц棰勫鐞嗚鍙ユ椂浠呴渶鍙戦佸弬鏁扮殑鏁版嵁锛屼粠鑰屽噺灏戜簡缃戠粶閫氫俊閲忋
棰勫鐞嗚鍙ョ殑鍙︿竴涓紭鐐规槸锛屽畠閲囩敤浜嗕簩杩涘埗鍗忚锛屼粠鑰屼娇寰楀鎴风鍜屾湇鍔″櫒涔嬮棿鐨勬暟鎹紶杈撴洿鏈夋晥鐜囥
涓嬭堪璇彞鍙敤浣滈澶勭悊璇彞锛CREATE TABLE銆DELETE銆DO銆INSERT銆REPLACE銆SELECT銆SET銆UPDATE銆佷互鍙婂ぇ澶氭暟SHOW璇彞銆鍦MySQL 5.1涓紝涓嶆敮鎸佸叾浠栬鍙ャ
棰勫鐞嗚鍙ヤ富瑕佷娇鐢MYSQL_STMT鍜MYSQL_BIND鏁版嵁缁撴瀯銆绗3绉嶇粨鏋MYSQL_TIME鐢ㄤ簬浼犺緭鏆傛椂鎬ф暟鎹
璇ョ粨鏋勮〃绀洪澶勭悊璇彞銆傞氳繃璋冪敤mysql_stmt_init()鍒涘缓璇彞锛岃繑鍥炶鍙ュ彞鏌勶紝鍗虫寚鍚MYSQL_STMT鐨勬寚閽堛傝鍙ユ焺鐢ㄦ埛鎵鏈夊悗缁殑涓庤鍙ユ湁鍏崇殑鍑芥暟锛岀洿鑷充娇鐢mysql_stmt_close()鍏抽棴浜嗗畠涓烘銆
MYSQL_STMT缁撴瀯娌℃湁渚涘簲鐢ㄧ▼搴忎娇鐢ㄧ殑鍙傛暟銆傛澶栵紝涓嶅簲灏濊瘯澶嶅埗MYSQL_STMT缁撴瀯銆備笉淇濊瘉杩欑被澶嶅埗鐗╀細鏈夌敤銆
澶氫釜璇彞鍙ユ焺鑳藉涓庡崟涓繛鎺ュ叧鑱旇捣鏉ャ傚鍙ユ焺鏁扮洰鐨勯檺鍒跺彇鍐充簬绯荤粺璧勬簮銆
璇ョ粨鏋勭敤浜庤鍙ヨ緭鍏ワ紙鍙戦佺粰鏈嶅姟鍣ㄧ殑鏁版嵁鍊硷級鍜岃緭鍑猴紙浠庢湇鍔″櫒杩斿洖鐨勭粨鏋滃硷級銆傚浜庤緭鍏ワ紝瀹冧笌mysql_stmt_bind_param()涓璧蜂娇鐢紝鐢ㄤ簬灏嗗弬鏁版暟鎹肩粦瀹氬埌缂撳啿鍖轰笂锛屼互渚mysql_stmt_execute()浣跨敤銆傚浜庤緭鍑猴紝瀹冧笌mysql_stmt_bind_result()涓璧蜂娇鐢紝鐢ㄤ簬缁戝畾缁撴灉缂撳啿鍖猴紝浠ヤ究鐢ㄤ簬with mysql_stmt_fetch()浠ヨ幏鍙栬銆
MYSQL_BIND缁撴瀯鍖呭惈涓嬭堪渚涘簲鐢ㄧ▼搴忎娇鐢ㄧ殑鎴愬憳銆傛瘡涓垚鍛樼敤浜庤緭鍏ュ拰杈撳嚭锛屼絾鍦ㄦ煇浜涙椂鍊欙紝涔熻兘鐢ㄤ簬涓嶅悓鐨勭洰鐨勶紝鍏蜂綋鎯呭喌鍙栧喅浜庢暟鎹紶杈撶殑鏂瑰悜銆
o enum enum_field_types buffer_type
缂撳啿鐨勭被鍨嬨傚湪鏈妭鍚庨潰鍒楀嚭浜嗗厑璁哥殑buffer_type鍊笺傚浜杈撳叆锛buffer_type鎸囨槑浜嗕笌璇彞鍙傛暟鎹嗙粦鐨勫肩被鍨嬨傚浜庤緭鍑猴紝瀹冩寚鏄庝簡浣犲笇鏈涗粠缁撴灉缂撳啿鏀跺埌鐨勫肩被鍨嬨
o void *buffer
瀵逛簬杈撳叆锛岃繖鏄寚鍚戝瓨鍌ㄨ鍙ュ弬鏁版暟鎹肩殑缂撳啿鐨勬寚閽堛傚浜庤緭鍑猴紝瀹冩槸鎸囧悜杩斿洖缁撴灉闆嗗垪鍊肩殑缂撳啿鐨勬寚閽堛傚浜庢暟鍊煎垪绫诲瀷锛岀紦鍐插簲鎸囧悜鎭板綋鐨C绫诲瀷鍙橀噺锛堝鏋滃皢璇ュ彉閲忎笌鍏锋湁UNSIGNED灞炴х殑鍒楀叧鑱旇捣鏉ワ紝鍙橀噺unsigned C绫诲瀷銆傞氳繃浣跨敤is_unsigned鎴愬憳锛屾寚鏄庡彉閲忔槸signed鎴unsigned绫诲瀷锛岃鎯呰鍙傝鏈妭鍚庨潰鐨勪粙缁嶏級銆傚浜庢棩鏈熷拰鏃堕棿鍒楃被鍨嬶紝缂撳啿搴旀寚鍚MYSQL_TIME缁撴瀯銆瀵逛簬瀛楃鍜屼簩杩涘埗瀛楃涓插垪绫诲瀷锛岀紦鍐插簲鎸囧悜瀛楃缂撳啿鍖恒
o unsigned long buffer_length
*buffer鐨勫疄闄呭ぇ灏忥紝鍗曚綅涓哄瓧鑺傘瀹冩寚鏄庝簡鍙繚瀛樺湪缂撳啿鍖哄唴鐨勬渶澶ф暟鎹傚浜庡瓧绗﹀拰浜岃繘鍒C鏁版嵁锛buffer_length鍊兼寚瀹氫簡涓mysql_stmt_bind_param()涓璧蜂娇鐢ㄦ椂鐨*buffer闀垮害锛屾垨涓mysql_stmt_bind_result()涓璧蜂娇鐢ㄦ椂鑳藉鎻愬彇鍒扮紦鍐插尯鍐呯殑鏈澶ф暟鎹
o unsigned long *length
鎸囧悜unsigned long鍙橀噺鐨勬寚閽堬紝璇ュ彉閲忔寚鏄庝簡瀛樺偍鍦*buffer涓暟鎹殑瀹為檯瀛楄妭鏁般鈥渓ength鈥鐢ㄤ簬瀛楃鎴栦簩杩涘埗C鏁版嵁銆傚浜庤緭鍏ュ弬鏁版暟鎹粦瀹氾紝鈥渓ength鈥鎸囧悜unsigned long鍙橀噺锛岃鍙橀噺鎸囨槑浜嗗瓨鍌ㄥ湪*buffer涓弬鏁板肩殑闀垮害锛屼緵mysql_stmt_execute()浣跨敤銆傚浜杈撳嚭鍊肩粦瀹氾紝mysql_stmt_fetch()浼氬皢杩斿洖鐨勫垪鍊间繚瀛樺埌鈥渓ength鈥鎸囧悜鐨鍙橀噺涓
瀵逛簬鏁板煎拰涓存椂鏁版嵁绫诲瀷锛鈥渓ength鈥灏嗚蹇界暐锛屽師鍥犲湪浜庯紝鏁版嵁鍊肩殑闀垮害鏄敱buffer_type鍊煎喅瀹氱殑銆
o my_bool *is_null
璇ユ垚鍛樻寚鍚my_bool鍙橀噺锛屽鏋滃间负NULL锛岃鍙橀噺涓鈥鐪鈥锛屽鏋滃间负闈Null锛璇ュ彉閲忎负鈥滃亣鈥濄傚浜庤緭鍏ワ紝灏*is_null璁剧疆涓衡滅湡鈥濓紝鎸囨槑浠ヨ鍙ュ弬鏁扮殑褰㈠紡浼犻NULL鍊笺傚浜庤緭鍑猴紝濡傛灉浠庤鍙ヨ繑鍥炵殑缁撴灉闆嗗垪鍊间负NULL锛屽綋鑾峰彇浜嗚鍚庯紝璇ュ煎皢琚涓衡滅湡鈥濄
鈥渋s_null鈥鏄寚鍚戝竷灏旂被鍨嬬殑鎸囬拡锛岃屼笉鏄竷灏旀爣閲忥紝浠ヤ究鑳戒互涓嬭堪鏂瑰紡浣跨敤瀹冿細
搂 濡傛灉鏁版嵁鍊兼绘槸NULL锛屼娇鐢MYSQL_TYPE_NULL缁戝畾鍒椼
搂 濡傛灉鏁版嵁鍊兼绘槸NOT NULL锛岃缃is_null = (my_bool*) 0銆
搂 鍦ㄦ墍鏈夊叾浠栨儏鍐典笅锛屽簲灏is_null璁剧疆涓my_bool鍙橀噺鐨勫湴鍧锛屽苟鍦ㄥ悇娆℃墽琛屼箣闂存伆褰撳湴鏇存敼鍙橀噺鐨勫硷紝浠ユ寚鏄庢暟鎹兼槸NULL鎴NOT NULL銆
o my_bool is_unsigned
璇ユ垚鍛樼敤浜庢暣鏁扮被鍨嬨傦紙瀵瑰簲浜MYSQL_TYPE_TINY銆MYSQL_TYPE_SHORT銆MYSQL_TYPE_LONG銆佷互鍙MYSQL_TYPE_LONGLONG绫诲瀷鐨勪唬鐮侊級銆傚浜庢棤绗﹀彿绫诲瀷锛屽簲灏鈥is_unsigned鈥璁剧疆涓鈥鐪鈥锛屽浜庡甫绗﹀彿绫诲瀷锛屽簲灏嗗叾璁剧疆涓鈥鍋鈥銆
o my_bool error
瀵逛簬杈撳嚭锛岃鎴愬憳鐢ㄤ簬閫氭姤鏁版嵁鎴煭閿欒銆傚繀椤婚氳繃璋冪敤甯︽湁MYSQL_REPORT_DATA_TRUNCATION閫夐」鐨mysql_options()锛屽惎鐢鎴煭閫氭姤鍔熻兘銆傚厑璁歌鍔熻兘鍚庯紝mysql_stmt_fetch()杩斿洖MYSQL_DATA_TRUNCATED锛岃屼笖瀵逛簬鍑虹幇鎴煭鎯呭喌鐨勫弬鏁帮紝鍦MYSQL_BIND缁撴瀯涓紝閿欒鏍囧織涓鈥鐪鈥銆傛埅鐭寚鏄庝涪澶变簡绗﹀彿鎴栨湁鏁堜綅鏁帮紝鎴栧瓧绗︿覆杩囬暱浠ヨ嚦浜庢棤娉曞绾冲湪1鍒椾腑銆
瑕佹兂浣跨敤MYSQL_BIND缁撴瀯锛屽簲灏嗗叾鍐呭缃负0浠ヤ究鍒濆鍖栧畠锛岀劧鍚庡鍏惰繘琛岃缃紝鎭板綋鍦版弿杩板畠銆備緥濡傦紝瑕佹兂澹版槑骞跺垵濮嬪寲涓変釜MYSQL_BIND缁撴瀯鐨勬暟缁勶紝鍙娇鐢ㄤ笅杩颁唬鐮侊細
MYSQL_BIND bind[3];
memset(bind, 0, sizeof(bind));
璇ョ粨鏋勭敤浜庡皢DATE銆TIME銆DATETIME鍜TIMESTAMP鏁版嵁鐩存帴鍙戦佸埌鏈嶅姟鍣紝鎴栦粠鏈嶅姟鍣ㄧ洿鎺ユ帴鏀惰繖绫绘暟鎹傚皢MYSQL_BIND缁撴瀯鐨buffer_type鎴愬憳璁剧疆涓轰复鏃跺间箣涓锛屽苟灏buffer鎴愬憳璁剧疆涓烘寚鍚MYSQL_TIME缁撴瀯锛屽嵆鍙疄鐜拌鐐广
MYSQL_TIME缁撴瀯鍖呭惈涓嬭堪鎴愬憳锛
o unsigned int year
骞翠唤
o unsigned int month
鏈堜唤
o unsigned int day
澶
o unsigned int hour
灏忔椂
o unsigned int minute
鍒嗛挓
o unsigned int second
绉
o my_bool neg
甯冨皵鏍囧織锛岀敤浜庢寚鏄庢椂闂存槸鍚︿负璐熸暟銆
o unsigned long second_part
绉掔殑鍒嗘暟閮ㄥ垎銆傝鎴愬憳鐩墠涓嶄娇鐢ㄣ
浠呬娇鐢ㄦ柦鍔犲湪缁欏畾涓存椂绫诲瀷鍊间笂鐨MYSQL_TIME缁撴瀯鐨勯儴鍒锛氱敤浜DATE銆DATETIME鍜TIMESTAMP鐨骞淬佹湀銆佹棩閮ㄥ垎銆傜敤浜TIME銆DATETIME鍜TIMESTAMP鍊肩殑灏忔椂銆佸垎閽熴佺閮ㄥ垎銆傝鍙傝25.2.10鑺傦紝鈥滄棩鏈熷拰鏃堕棿鍊肩殑C API澶勭悊鈥銆
鍦ㄤ笅闈㈢殑琛ㄦ牸涓紝缁欏嚭浜嗗彲鍦MYSQL_BIND缁撴瀯鐨buffer_type鎴愬憳涓寚瀹氱殑鍏佽鍊笺傚湪璇ヨ〃涓紝杩樼粰鍑轰簡涓庢瘡涓buffer_type鍊兼渶鎺ヨ繎鐨勫搴SQL绫诲瀷锛屽浜庢暟鍊煎拰涓存椂绫诲瀷锛岀粰鍑轰簡瀵瑰簲鐨C绫诲瀷銆
buffer_type鍊 |
SQL绫诲瀷 |
C绫诲瀷 |
MYSQL_TYPE_TINY |
TINYINT |
char |
MYSQL_TYPE_SHORT |
SMALLINT |
short int |
MYSQL_TYPE_LONG |
INT |
int |
MYSQL_TYPE_LONGLONG |
BIGINT |
long long int |
MYSQL_TYPE_FLOAT |
FLOAT |
float |
MYSQL_TYPE_DOUBLE |
DOUBLE |
double |
MYSQL_TYPE_TIME |
TIME |
MYSQL_TIME |
MYSQL_TYPE_DATE |
DATE |
MYSQL_TIME |
MYSQL_TYPE_DATETIME |
DATETIME |
MYSQL_TIME |
MYSQL_TYPE_TIMESTAMP |
TIMESTAMP |
MYSQL_TIME |
MYSQL_TYPE_STRING |
CHAR |
|
MYSQL_TYPE_VAR_STRING |
VARCHAR |
|
MYSQL_TYPE_TINY_BLOB |
TINYBLOB/TINYTEXT |
|
MYSQL_TYPE_BLOB |
BLOB/TEXT |
|
MYSQL_TYPE_MEDIUM_BLOB |
MEDIUMBLOB/MEDIUMTEXT |
|
MYSQL_TYPE_LONG_BLOB |
LONGBLOB/LONGTEXT |
|
闅愬紡绫诲瀷杞崲鍙部涓や釜鏂瑰悜鎵ц銆
鍦ㄦ褰掔撼浜嗛澶勭悊璇彞澶勭悊鍔熻兘鍙娇鐢ㄧ殑鍑芥暟锛屽苟鍦ㄥ悗闈㈢殑绔犺妭涓缁嗕粙缁嶄簡瀹冦傝鍙傝25.2.7鑺傦紝鈥淐 API棰勫鐞嗚鍙ュ嚱鏁版弿杩扳銆
鍑芥暟 |
鎻忚堪 |
mysql_stmt_affected_rows() |
杩斿洖鐢遍澶勭悊璇彞UPDATE銆DELETE鎴INSERT鍙樻洿銆佸垹闄ゆ垨鎻掑叆鐨勮鏁扮洰銆 |
mysql_stmt_attr_get() |
鑾峰彇棰勫鐞嗚鍙ュ睘鎬х殑鍊笺 |
mysql_stmt_attr_set() |
璁剧疆棰勫鐞嗚鍙ョ殑灞炴с |
mysql_stmt_bind_param() |
灏嗗簲鐢ㄧ▼搴忔暟鎹紦鍐蹭笌棰勫鐞SQL璇彞涓殑鍙傛暟鏍囪绗﹀叧鑱旇捣鏉ャ |
mysql_stmt_bind_result() |
灏嗗簲鐢ㄧ▼搴忔暟鎹紦鍐蹭笌缁撴灉闆嗕腑鐨勫垪鍏宠仈璧锋潵銆 |
mysql_stmt_close() |
閲婃斁棰勫鐞嗚鍙ヤ娇鐢ㄧ殑鍐呭瓨銆 |
mysql_stmt_data_seek() |
瀵绘壘璇彞缁撴灉闆嗕腑鐨勪换鎰忚缂栧彿銆 |
mysql_stmt_errno() |
杩斿洖涓婃璇彞鎵ц鐨勯敊璇紪鍙枫 |
mysql_stmt_error() |
杩斿洖涓婃璇彞鎵ц鐨勯敊璇秷鎭 |
mysql_stmt_execute() |
鎵ц棰勫鐞嗚鍙ャ |
mysql_stmt_fetch() |
浠庣粨鏋滈泦鑾峰彇鏁版嵁鐨勪笅涓琛岋紝骞惰繑鍥炴墍鏈夌粦瀹氬垪鐨勬暟鎹 |
mysql_stmt_fetch_column() |
鑾峰彇缁撴灉闆嗗綋鍓嶈涓煇鍒楃殑鏁版嵁銆 |
mysql_stmt_field_count() |
瀵逛簬鏈杩戠殑璇彞锛岃繑鍥炵粨鏋滆鐨勬暟鐩 |
mysql_stmt_free_result() |
閲婃斁鍒嗛厤缁欒鍙ュ彞鏌勭殑璧勬簮銆 |
mysql_stmt_init() |
涓MYSQL_STMT缁撴瀯鍒嗛厤鍐呭瓨骞跺垵濮嬪寲瀹銆 |
mysql_stmt_insert_id() |
瀵逛簬棰勫鐞嗚鍙ョ殑AUTO_INCREMENT鍒楋紝杩斿洖鐢熸垚鐨ID銆 |
mysql_stmt_num_rows() |
浠庤鍙ョ紦鍐茬粨鏋滈泦杩斿洖鎬昏鏁般 |
mysql_stmt_param_count() |
杩斿洖棰勫鐞SQL璇彞涓殑鍙傛暟鏁扮洰銆 |
mysql_stmt_param_metadata() |
杩斿洖缁撴灉闆嗙殑鍙傛暟鍏冩暟鎹 |
mysql_stmt_prepare() |
涓烘墽琛屾搷浣滃噯澶SQL瀛楃涓层 |
mysql_stmt_reset() |
澶嶄綅鏈嶅姟鍣ㄤ腑鐨勮鍙ョ紦鍐插尯銆 |
mysql_stmt_result_metadata() |
浠ョ粨鏋滈泦褰㈠紡杩斿洖棰勫鐞嗚鍙ュ厓鏁版嵁銆 |
mysql_stmt_row_seek() |
浣跨敤浠mysql_stmt_row_tell()杩斿洖鐨勫硷紝鏌ユ壘璇彞缁撴灉闆嗕腑鐨勮鍋忕Щ銆 |
mysql_stmt_row_tell() |
杩斿洖璇彞琛屽厜鏍囦綅缃 |
mysql_stmt_send_long_data() |
灏嗙▼搴忓潡涓殑闀挎暟鎹彂閫佸埌鏈嶅姟鍣ㄣ |
mysql_stmt_sqlstate() |
杩斿洖鍏充簬涓婃璇彞鎵ц鐨SQLSTATE閿欒浠g爜銆 |
mysql_stmt_store_result() |
灏嗗畬鏁寸殑缁撴灉闆嗘绱㈠埌瀹㈡埛绔 |
璋冪敤mysql_stmt_init()浠ュ垱寤鸿鍙ュ彞鏌勶紝鐒跺悗璋冪敤mysql_stmt_prepare鍑嗗璇彞锛岃皟鐢mysql_stmt_bind_param()鎻愪緵鍙傛暟鏁版嵁锛屽苟璋冪敤mysql_stmt_execute()鎵ц璇彞銆傞氳繃鏇存敼mysql_stmt_bind_param()鎻愪緵鐨勭浉搴旂紦鍐插尯涓殑鍙傛暟鍊硷紝鍙噸澶嶆墽琛mysql_stmt_execute()銆
濡傛灉璇彞鏄SELECT鎴栦换浣曞叾浠栬兘鐢熸垚缁撴灉闆嗙殑璇彞锛mysql_stmt_prepare()涔熶細閫氳繃mysql_stmt_result_metadata()浠MYSQL_RES缁撴灉闆嗙殑褰㈠紡杩斿洖缁撴灉闆嗗厓鏁版嵁淇℃伅銆
浣犲彲浠ヤ娇鐢mysql_stmt_bind_result()鎻愪緵缁撴灉缂撳啿锛屼互渚mysql_stmt_fetch()鑳借嚜鍔ㄥ皢鏁版嵁杩斿洖缁欒繖浜涚紦鍐层杩欐槸涓绉嶆寜琛岃幏鍙栨柟寮忋
姝ゅ锛屼綘涔熻兘浣跨敤mysql_stmt_send_long_data()灏嗙▼搴忓潡涓殑鏂囨湰鎴栦簩杩涘埗鏁版嵁鍙戦佸埌鏈嶅姟鍣ㄣ璇峰弬瑙25.2.7.25鑺傦紝鈥渕ysql_stmt_send_long_data()鈥銆
瀹屾垚璇彞鎵ц鍚庯紝蹇呴』浣跨敤mysql_stmt_close()鍏抽棴璇彞鍙ユ焺锛屼互渚夸笌涔嬬浉鍏崇殑鎵鏈夎祫婧愬潎鑳借閲婃斁銆
濡傛灉閫氳繃璋冪敤mysql_stmt_result_metadata()鑾峰緱浜SELECT璇彞鐨勭粨鏋滈泦鍏冩暟鎹紝涔熷簲浣跨敤mysql_free_result()閲婃斁鍏冩暟鎹
鎵ц姝ラ
瑕佹兂鍑嗗鍜屾墽琛岃鍙ワ紝搴旂敤绋嬪簭蹇呴』閲囧彇涓嬭堪姝ラ锛
1. 鐢msyql_stmt_init()鍒涘缓棰勫鐞嗚鍙ュ彞鏌勩傝鎯冲湪鏈嶅姟鍣ㄤ笂鍑嗗棰勫鐞嗚鍙ワ紝鍙皟鐢mysql_stmt_prepare()锛屽苟涓哄叾浼犻掑寘鍚SQL璇彞鐨勫瓧绗︿覆銆
2. 濡傛灉璇彞鐢熸垚浜嗙粨鏋滈泦锛岃皟鐢mysql_stmt_result_metadata()浠ヨ幏寰楃粨鏋滈泦鍏冩暟鎹傝櫧鐒朵笌鍖呭惈鏌ヨ杩斿洖鍒楃殑缁撴灉闆嗕笉鍚岋紝璇ュ厓鏁版嵁鏈韩涔熼噰鐢ㄤ簡缁撴灉闆嗙殑褰㈠紡銆傚厓鏁版嵁缁撴灉闆嗘寚鏄庝簡缁撴灉涓寘鍚灏戝垪锛屽苟鍖呭惈姣忎竴鍒楃殑淇℃伅銆
3. 浣跨敤mysql_stmt_bind_param()璁剧疆浠讳綍鍙傛暟鐨勫笺傚繀椤昏缃墍鏈夊弬鏁般傚惁鍒欙紝璇彞鎵ц灏嗚繑鍥為敊璇紝鎴栫敓鎴愭棤娉曢鏂欑殑缁撴灉銆
4. 璋冪敤mysql_stmt_execute()鎵ц璇彞銆
5. 濡傛灉璇彞鐢熸垚浜嗙粨鏋滈泦锛屾崋缁戞暟鎹紦鍐诧紝閫氳繃璋冪敤mysql_stmt_bind_result()锛屾绱㈣鍊笺
6. 閫氳繃閲嶅璋冪敤mysql_stmt_fetch()锛屾寜琛屽皢鏁版嵁鎻愬彇鍒扮紦鍐插尯锛岀洿鑷虫湭鍙戠幇鏇村琛屼负姝€
7. 閫氳繃鏇存敼鍙傛暟鍊煎苟鍐嶆鎵ц璇彞锛岄噸澶嶆楠3鍒版楠6銆
璋冪敤mysql_stmt_prepare()鏃讹紝MySQL瀹㈡埛绔紡鏈嶅姟鍣ㄥ崗璁皢鎵ц涓嬭堪鍔ㄤ綔锛
路 鏈嶅姟鍣ㄨВ鏋愯鍙ワ紝骞堕氳繃璧嬪艰鍙ID灏OK鐘舵佸彂鍥炲鎴风銆傛澶栵紝濡傛灉瀹冩槸闈㈠悜缁撴灉闆嗙殑璇彞锛岃繕灏嗗彂閫佹荤殑鍙傛暟鏁扮洰锛屽垪璁℃暟鍜屽厓鏁版嵁銆傚湪姝よ皟鐢ㄨ繃绋嬩腑锛屾湇鍔″櫒灏嗘鏌ヨ鍙ョ殑鎵鏈夎娉曞拰璇箟銆
路 瀹㈡埛绔噰鐢ㄨ璇彞ID鐢ㄤ簬杩涗竴姝ユ搷浣滐紝浠ヤ究鏈嶅姟鍣ㄨ兘浠庡叾璇彞姹犱腑璇嗗埆璇彞銆
璋冪敤mysql_stmt_execute()鏃讹紝MySQL瀹㈡埛绔紡鏈嶅姟鍣ㄥ崗璁皢鎵ц涓嬭堪鍔ㄤ綔锛
路 瀹㈡埛绔娇鐢ㄨ鍙ュ彞鏌勶紝骞跺皢鍙傛暟鏁版嵁鍙戦佸埌鏈嶅姟鍣ㄣ
路 鏈嶅姟鍣ㄤ娇鐢ㄧ敱瀹㈡埛绔彁渚涚殑ID鏉ヨ瘑鍒鍙ワ紝鐢ㄦ柊鎻愪緵鐨勬暟鎹浛鎹㈠弬鏁版爣璁扮锛屽苟鎵ц璇彞銆傚鏋滆鍙ョ敓鎴愪簡缁撴灉闆嗭紝鏈嶅姟鍣ㄥ皢鏁版嵁鍙戝洖瀹㈡埛绔傚惁鍒欙紝鏈嶅姟鍣ㄤ細灏嗗彂閫OK鐘舵侊紝浠ュ強鎬荤殑鍙樻洿銆佸垹闄ゅ拰鎻掑叆琛屾暟銆
璋冪敤mysql_stmt_fetch()鏃讹紝MySQL瀹㈡埛绔紡鏈嶅姟鍣ㄥ崗璁皢鎵ц涓嬭堪鍔ㄤ綔锛
路 瀹㈡埛绔寜琛屼粠淇℃伅鍖呰鍙栨暟鎹紝骞堕氳繃鎵ц蹇呰鐨勮浆鎹㈡搷浣滃皢鍏舵斁鍏ュ簲鐢ㄧ▼搴忔暟鎹紦鍐蹭腑銆傚鏋滃簲鐢ㄧ▼搴忕殑缂撳啿绫诲瀷涓庢湇鍔″櫒杩斿洖鐨勫瓧娈电被鍨嬬浉鍚岋紝杞崲鍗佸垎绠鏄庛
濡傛灉鍑虹幇浜嗛敊璇紝鍙垎鍒娇鐢mysql_stmt_errno()銆mysql_stmt_error()鍜mysql_stmt_sqlstate()鑾峰彇璇彞閿欒浠g爜銆侀敊璇秷鎭拰SQLSTATE鍊笺
棰勫鐞嗚鍙ユ棩蹇楀姛鑳
瀵逛簬涓mysql_stmt_prepare()鍜mysql_stmt_execute() C API鍑芥暟涓璧锋墽琛岀殑棰勫鐞嗚鍙ワ紝鏈嶅姟鍣ㄤ細灏嗏滃噯澶団濆拰鈥滄墽琛屸濊鍐欏叆涓鑸煡璇㈡棩蹇楋紝浠ヤ究浣犺兘浜嗚В璇彞鏄湪浣曟椂鍑嗗鍜屾墽琛岀殑銆
鍋囧畾鎸変笅杩版柟寮忓噯澶囧拰鎵ц浜嗚鍙ワ細
1. 璋冪敤mysql_stmt_prepare()浠ュ噯澶璇彞瀛楃涓"SELECT ?"銆
2. 璋冪敤mysql_stmt_bind_param()灏嗗鈥3鈥缁戝畾鍒伴澶勭悊璇彞涓殑鍙傛暟銆
3. 璋冪敤mysql_stmt_execute()锛屾墽琛岄澶勭悊璇彞銆
涓婅堪璋冪敤鐨勭粨鏋滄槸锛屾湇鍔″櫒灏嗕笅杩拌鍐欏叆涓鑸煡璇㈡棩蹇楋細
Prepare [1] SELECT ?
Execute [1] SELECT 3
鏃ュ織涓殑姣忎釜鈥滃噯澶団濆拰鈥滄墽琛屸濊鍧囧叿鏈[n]璇彞ID鏍囪瘑锛岃繖鏍凤紝浣犲氨鑳借窡韪凡璁板綍鐨勯澶勭悊璇彞銆N鏄鏁存暟銆傚浜庡鎴风锛屽鏋滃悓鏃舵湁澶氫釜娲诲姩鐨勯澶勭悊璇彞锛n鍙兘浼氬ぇ浜1銆傛浛鎹簡鈥?鈥濆弬鏁扮殑鏁版嵁鍊煎悗锛屾瘡涓滄墽琛屸濊灏嗘樉绀轰竴鏉¢澶勭悊璇彞銆
鐗堟湰璇存槑锛氬湪MySQL 4.1.10涔嬪墠锛屾樉绀虹殑鈥滃噯澶団濊鏃[n]鏍囪瘑銆傚湪MySQL 4.1.10涔嬪墠锛屼笉鏄剧ず鈥滄墽琛屸濊銆
涓轰簡鍑嗗鍜屾墽琛屾煡璇紝璇蜂娇鐢ㄤ笅杩伴儴鍒嗚缁嗕粙缁嶇殑鍑芥暟銆
娉ㄦ剰锛屼笌MYSQL_STMT缁撴瀯涓璧蜂娇鐢ㄧ殑鎵鏈夊嚱鏁板潎浠ュ墠缂mysql_stmt_寮濮嬨
瑕佹兂鍒涘缓MYSQL_STMT鍙ユ焺锛岃浣跨敤mysql_stmt_init()鍑芥暟銆
my_ulonglong mysql_stmt_affected_rows(MYSQL_STMT *stmt)
鎻忚堪
杩斿洖涓婃鎵ц璇彞鏇存敼銆佸垹闄ゆ垨鎻掑叆鐨勬昏鏁般傚浜UPDATE銆DELETE鎴INSERT璇彞锛屽彲鍦mysql_stmt_execute()涔嬪悗绔嬪埢璋冪敤瀹冧滑銆瀵逛簬SELECT璇彞锛mysql_stmt_affected_rows()鐨勫伐浣滄柟寮忕被浼间簬mysql_num_rows()銆
杩斿洖鍊
澶т簬0鐨勬暣鏁版寚鏄庝簡鍙楀奖鍝嶆垨妫绱㈢殑琛屾暟銆傚浜UPDATE璇彞锛屸0鈥濊〃鏄庢湭鏇存柊浠讳綍璁板綍锛屽湪鏌ヨ涓病鏈変笌WHERE瀛愬彞鍖归厤鐨勮锛屾垨灏氭湭鎵ц浠讳綍鏌ヨ銆鈥-1鈥濊〃鏄庤繑鍥炰簡閿欒锛屾垨瀵SELECT鏌ヨ锛屽湪璋冪敤mysql_stmt_store_result()涔嬪墠璋冪敤浜mysql_stmt_affected_rows()銆傜敱浜mysql_stmt_affected_rows()杩斿洖鏃犵鍙峰硷紝鍙氳繃姣旇緝杩斿洖鍊煎拰鈥(my_ulonglong)-1鈥锛堟垨绛夋晥鐨鈥(my_ulonglong)~0鈥锛锛屾鏌モ-1鈥濄
鍏充簬杩斿洖鍊肩殑棰濆淇℃伅锛岃鍙傝25.2.3.1鑺傦紝鈥渕ysql_affected_rows()鈥銆
閿欒
鏃犮
绀轰緥锛
鍏充簬mysql_stmt_affected_rows()鐨勭敤娉曪紝璇峰弬闃25.2.7.10鑺傦紝鈥渕ysql_stmt_execute()鈥涓粰鍑虹殑绀轰緥銆
int mysql_stmt_attr_get(MYSQL_STMT *stmt, enum enum_stmt_attr_type option, void *arg)
鎻忚堪
鍙敤浜庤幏寰楄鍙ュ睘鎬х殑褰撳墠鍊笺
鈥option鈥鍙傞噺鏄笇鏈涜幏鍙栫殑閫夐」锛鈥渁rg鈥搴旀寚鍚戝寘鍚夐」鍊肩殑鍙橀噺銆傚鏋鈥option鈥濇槸鏁存暟锛岄偅涔堚arg鈥搴旀寚鍚鏁存暟鐨勫笺
鍏充簬閫夐」鍜岄夐」绫诲瀷鐨勬竻鍗曪紝璇峰弬瑙25.2.7.3鑺傦紝鈥渕ysql_stmt_attr_set()鈥銆
杩斿洖鍊
濡傛灉OK锛岃繑鍥0銆濡傛灉閫夐」鏈煡锛岃繑鍥為潪0鍊笺
閿欒
鏃犮
int mysql_stmt_attr_set(MYSQL_STMT *stmt, enum enum_stmt_attr_type option, const void *arg)
鎻忚堪
鍙敤浜庡奖鍝嶉澶勭悊璇彞鐨勮涓恒傚彲澶氭璋冪敤璇ュ嚱鏁版潵璁剧疆澶氫釜閫夐」銆
鈥option鈥鍙傞噺鏄笇鏈涜缃殑閫夐」锛鈥arg鈥鍙傞噺鏄夐」鐨勫笺濡傛灉鈥option鈥濇槸鏁存暟锛岄偅涔堚arg鈥搴旀寚鍚鏁存暟鐨勫笺
鍙兘鐨勯夐」鍊硷細
閫夐」 |
鍙傞噺绫诲瀷 |
鍔熻兘 |
STMT_ATTR_UPDATE_MAX_LENGTH |
my_bool * |
濡傛灉璁句负1锛氭洿鏂mysql_stmt_store_result()涓殑鍏冩暟鎹MYSQL_FIELD->max_length銆 |
STMT_ATTR_CURSOR_TYPE |
unsigned long * |
璋冪敤mysql_stmt_execute()鏃讹紝璇彞灏嗘墦寮鐨勫厜鏍囩被鍨嬨*arg鍙互鏄CURSOR_TYPE_NO_CURSOR锛堥粯璁ゅ硷級鎴CURSOR_TYPE_READ_ONLY銆 |
STMT_ATTR_PREFETCH_ROWS |
unsigned long * |
浣跨敤鍏夋爣鏃讹紝涓娆′粠鏈嶅姟鍣ㄨ幏鍙栫殑琛屾暟銆*arg鐨勮寖鍥翠粠1鍒unsigned long鐨勬渶澶у笺傞粯璁ゅ间负1銆 |
濡傛灉涓CURSOR_TYPE_READ_ONLY涓璧蜂娇鐢ㄤ簡STMT_ATTR_CURSOR_TYPE閫夐」锛屽綋璋冪敤浜mysql_stmt_execute()鏃讹紝灏嗕负璇彞鎵撳紑鍏夋爣銆傚鏋滃瓨鍦ㄧ敱鍓嶄竴涓mysql_stmt_execute()璋冪敤鎵撳紑鐨勫厜鏍囷紝鍦ㄦ墦寮鏂扮殑鍏夋爣鍓嶏紝灏嗗叧闂鍏夋爣銆傛澶栵紝涓哄啀鎵ц鑰屽噯澶囪鍙ヤ箣鍓嶏紝mysql_stmt_reset()杩樺皢鍏抽棴浠讳綍鎵撳紑鐨勫厜鏍囥mysql_stmt_free_result()灏嗗叧闂换浣曟墦寮鐨勫厜鏍囥
濡傛灉涓洪澶勭悊璇彞鎵撳紑浜嗗厜鏍囷紝娌″繀瑕佽皟鐢mysql_stmt_store_result()锛岃繖鏄洜涓猴紝璇ュ嚱鏁颁細瀵艰嚧鍦ㄥ鎴风涓渚у缁撴灉闆嗚繘琛岀紦鍐插鐞嗐
鍦MySQL 5.0.2涓鍔犱簡STMT_ATTR_CURSOR_TYPE閫夐」銆傚湪MySQL 5.0.6涓紝澧炲姞浜STMT_ATTR_PREFETCH_ROWS閫夐」銆
杩斿洖鍊
濡傛灉OK锛岃繑鍥0銆濡傛灉閫夐」鏈煡锛岃繑鍥為潪0鍊笺
閿欒
鏃犮
绀轰緥锛
鍦ㄤ笅杩扮ず渚嬩腑锛屼负棰勫鐞嗚鍙ユ墦寮浜1涓厜鏍囷紝骞跺皢姣忔鑾峰彇鐨勮鏁拌涓5锛
MYSQL_STMT *stmt;
int rc;
unsigned long type;
unsigned long prefetch_rows = 5;
stmt = mysql_stmt_init(mysql);
type = (unsigned long) CURSOR_TYPE_READ_ONLY;
rc = mysql_stmt_attr_set(stmt, STMT_ATTR_CURSOR_TYPE, (void*) &type);
/* ... check return value ... */
rc = mysql_stmt_attr_set(stmt, STMT_ATTR_PREFETCH_ROWS,
(void*) &prefetch_rows);
/* ... check return value ... */
my_bool mysql_stmt_bind_param(MYSQL_STMT *stmt, MYSQL_BIND *bind)
鎻忚堪
mysql_stmt_bind_param()鐢ㄤ簬涓SQL璇彞涓殑鍙傛暟鏍囪绗︾粦瀹氭暟鎹紝浠ヤ紶閫掔粰mysql_stmt_prepare()銆傚畠浣跨敤MYSQL_BIND缁撴瀯鏉ユ彁渚涙暟鎹鈥bind鈥鏄MYSQL_BIND缁撴瀯鐨勬煇涓鏁扮粍鐨勫湴鍧銆傛寜鐓у鎴风搴撶殑棰勬湡锛屽浜庢煡璇腑鍑虹幇鐨勬瘡涓鈥?鈥鍙傛暟鏍囪绗︼紝鏁扮粍涓潎鍖呭惈1涓厓绱犮
鍋囧畾浣犲噯澶囦簡涓嬭堪璇彞锛
INSERT INTO mytbl VALUES(?,?,?)
缁戝畾鍙傛暟鏃讹紝MYSQL_BIND缁撴瀯鐨勬暟缁勫寘鍚3涓厓绱狅紝骞惰兘澹版槑濡備笅锛
MYSQL_BIND bind[3];
鍦25.2.5鑺傦紝鈥淐 API棰勫鐞嗚鍙ョ殑鏁版嵁绫诲瀷鈥涓紝浠嬬粛浜嗗簲璁剧疆鐨勬瘡涓MYSQL_BIND鍏冪礌鐨勬垚鍛樸
杩斿洖鍊
濡傛灉缁戝畾鎴愬姛锛岃繑鍥0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
路 CR_INVALID_BUFFER_USE
鎸囨槑鈥bind鈥濓紙缁戝畾锛夋槸鍚﹀皢鎻愪緵绋嬪簭鍧椾腑鐨勯暱鏁版嵁锛屼互鍙婄紦鍐茬被鍨嬫槸鍚︿负闈炲瓧绗︿覆鎴栦簩杩涘埗绫诲瀷銆
路 CR_UNSUPPORTED_PARAM_TYPE
涓嶆敮鎸佽杞崲銆傛垨璁buffer_type鍊兼槸闈炴硶鐨勶紝鎴栦笉鏄墍鏀寔鐨勭被鍨嬩箣涓銆
路 CR_OUT_OF_MEMORY
鍐呭瓨婧㈠嚭銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
绀轰緥锛
鍏充簬mysql_stmt_bind_param()鐨勭敤娉曪紝璇峰弬瑙25.2.7.10鑺傦紝鈥渕ysql_stmt_execute()鈥缁欏嚭鐨勭ず渚嬨
my_bool mysql_stmt_bind_result(MYSQL_STMT *stmt, MYSQL_BIND *bind)
鎻忚堪
mysql_stmt_bind_result()鐢ㄤ簬灏嗙粨鏋滈泦涓殑鍒椾笌鏁版嵁缂撳啿鍜岄暱搴︾紦鍐插叧鑱旓紙缁戝畾锛夎捣鏉ャ傚綋璋冪敤mysql_stmt_fetch()浠ヨ幏鍙栨暟鎹椂锛MySQL瀹㈡埛绔紡鏈嶅姟鍣ㄥ崗璁細灏嗙粦瀹氬垪鐨勬暟鎹疆浜庢寚瀹氱殑缂撳啿鍖哄唴銆
璋冪敤mysql_stmt_fetch()涔嬪墠锛屽繀椤灏嗘墍鏈夊垪缁戝畾鍒扮紦鍐层鈥渂ind鈥鏄MYSQL_BIND缁撴瀯鏌愪竴鏁扮粍鐨勫湴鍧銆鎸夌収瀹㈡埛绔簱鐨勯鏈燂紝瀵逛簬缁撴灉闆嗕腑鐨勬瘡涓鍒楋紝鏁扮粍搴斿寘鍚浉搴旂殑鍏冪礌銆傚鏋滄湭灏嗗垪缁戝畾鍒MYSQL_BIND缁撴瀯锛mysql_stmt_fetch()灏嗙畝鍗曞湴蹇界暐鏁版嵁鑾峰彇鎿嶄綔銆傜紦鍐插尯搴旇冻澶熷ぇ锛岃冻浠ュ绾虫暟鎹硷紝杩欐槸鍥犱负鍗忚涓嶈繑鍥炴垚鍧楃殑鏁版嵁鍊笺
鍙互鍦ㄤ换浣曟椂鍊欑粦瀹氭垨鍐嶇粦瀹氬垪锛屽嵆浣垮凡閮ㄥ垎妫绱簡缁撴灉闆嗗悗涔熷悓鏍枫傛柊鐨勭粦瀹氬皢鍦ㄤ笅涓娆¤皟鐢mysql_stmt_fetch()鏃惰捣浣滅敤銆傚亣瀹氭煇涓搴旂敤绋嬪簭缁戝畾浜嗙粨鏋滈泦涓殑鍒楋紝骞惰皟鐢ㄤ簡mysql_stmt_fetch()銆傚鎴风锛忔湇鍔″櫒鍗忚灏嗚繑鍥炵粦瀹氱紦鍐插尯涓殑鏁版嵁銆傛帴涓嬫潵锛屽亣瀹氬簲鐢ㄧ▼搴忓皢澶氫釜鍒楃粦瀹氬埌涓嶅悓鐨勭紦鍐层傝鍗忚涓嶄細灏嗘暟鎹疆浜庢柊缁戝畾鐨勭紦鍐插尯锛岀洿鑷涓嬫璋冪敤mysql_stmt_fetch()涓烘銆
瑕佹兂缁戝畾鍒楋紝搴旂敤绋嬪簭灏嗚皟鐢mysql_stmt_bind_result()锛屽苟浼犻掔被鍨嬨佸湴鍧銆佷互鍙婇暱搴︾紦鍐茬殑鍦板潃銆傚湪25.2.5鑺傦紝鈥淐 API棰勫鐞嗚鍙ョ殑鏁版嵁绫诲瀷鈥涓紝浠嬬粛浜嗗簲璁剧疆鐨勫悇MYSQL_BIND鍏冪礌鐨勬垚鍛樸
杩斿洖鍊
濡傛灉缁戝畾鎴愬姛锛岃繑鍥0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
路 CR_UNSUPPORTED_PARAM_TYPE
涓嶆敮鎸佽杞崲銆傛垨璁buffer_type鍊兼槸闈炴硶鐨勶紝鎴栦笉鏄墍鏀寔鐨勭被鍨嬩箣涓銆
路 CR_OUT_OF_MEMORY
鍐呭瓨婧㈠嚭銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
绀轰緥锛
鍏充簬mysql_stmt_bind_result()鐨勭敤娉曪紝璇峰弬瑙25.2.7.11鑺傦紝鈥渕ysql_stmt_fetch()鈥涓粰鍑虹殑绀轰緥銆
my_bool mysql_stmt_close(MYSQL_STMT *)
鎻忚堪
鍏抽棴棰勫鐞嗚鍙ャ傛澶栵紝mysql_stmt_close()杩樹細鍙栨秷鐢鈥渟tmt鈥鎸囧悜鐨勮鍙ュ彞鏌勫垎閰嶃
濡傛灉褰撳墠璇彞宸叉寕璧锋垨鏈鍙栫粨鏋滐紝璇ュ嚱鏁板皢鍙栨秷瀹冧滑锛屼互渚胯兘鎵ц涓嬩竴涓煡璇紝
杩斿洖鍊
濡傛灉鎴愬姛閲婃斁浜嗚鍙ワ紝杩斿洖0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
绀轰緥锛
鍏充簬mysql_stmt_close()鐨勭敤娉曪紝璇峰弬瑙25.2.7.10鑺傦紝鈥渕ysql_stmt_execute()鈥涓粰鍑虹殑绀轰緥銆
void mysql_stmt_data_seek(MYSQL_STMT *stmt, my_ulonglong offset)
鎻忚堪
鏌ユ壘璇彞缁撴灉闆嗕腑鐨勪换鎰忚銆傚亸绉婚噺涓鸿缂栧彿锛屽簲浣嶄簬浠0鍒mysql_stmt_num_rows(stmt)-1鐨勮寖鍥村唴銆
璇ュ嚱鏁拌姹傝鍙ョ粨鏋滈泦缁撴瀯鍖呭惈涓婃鎵ц鏌ヨ鐨勫叏閮ㄧ粨鏋滐紝杩欐牱锛mysql_stmt_data_seek()灏辫兘涓mysql_stmt_store_result()涓璧蜂娇鐢ㄣ
杩斿洖鍊
鏃犮
閿欒
鏃犮
unsigned int mysql_stmt_errno(MYSQL_STMT *stmt)
鎻忚堪
瀵逛簬鐢stmt鎸囧畾鐨勮鍙ワ紝mysql_stmt_errno()灏嗚繑鍥炴渶杩戣皟鐢ㄧ殑璇彞API鍑芥暟鐨勯敊璇唬鐮侊紝璇ュ嚱鏁版垨鎴愬姛鎴栧け璐ャ鈥0鈥濊繑鍥炲艰〃绀烘湭鍑虹幇閿欒銆傚湪MySQL errmsg.h澶存枃浠朵腑鍒楀嚭浜嗗鎴风閿欒娑堟伅缂栧彿銆傚湪mysqld_error.h涓紝鍒楀嚭浜嗘湇鍔″櫒閿欒娑堟伅銆姝ゅ锛屽湪闄勫綍B锛閿欒浠g爜鍜屾秷鎭涓紝涔熷垪鍑轰簡閿欒娑堟伅銆
杩斿洖鍊
閿欒浠g爜鍊笺傚鏋滄湭鍑虹幇閿欒锛岃繑鍥0銆
閿欒
鏃犮
const char *mysql_stmt_error(MYSQL_STMT *stmt)
鎻忚堪
瀵逛簬鐢stmt鎸囧畾鐨勮鍙ワ紝mysql_stmt_error()杩斿洖鐢Null缁堢粨鐨勫瓧绗︿覆锛岃瀛楃涓插寘鍚渶杩戣皟鐢ㄧ殑璇彞API鍑芥暟鐨勯敊璇秷鎭紝璇ュ嚱鏁版垨鎴愬姛鎴栧け璐ャ傚鏋滄湭鍑虹幇閿欒锛岃繑鍥绌哄瓧绗︿覆("")銆傝繖鎰忓懗鐫涓嬭堪涓や釜娴嬭瘯鏄瓑鏁堢殑锛
if (mysql_stmt_errno(stmt))
{
// an error occurred
}
if (mysql_stmt_error(stmt)[0])
{
// an error occurred
}
閫氳繃閲嶆柊缂栬瘧MySQL瀹㈡埛绔簱锛屽彲鏇存敼瀹㈡埛绔敊璇秷鎭殑璇█銆傜洰鍓嶏紝鑳藉閫夋嫨鏁扮璇█涔嬩竴鏄剧ず閿欒娑堟伅銆
杩斿洖鍊
鎻忚堪浜嗛敊璇殑瀛楃涓层傚鏋滄湭鍑虹幇閿欒锛岃繑鍥炵┖瀛楃涓层
閿欒
鏃犮
int mysql_stmt_execute(MYSQL_STMT *stmt)
鎻忚堪
mysql_stmt_execute()鎵ц涓庤鍙ュ彞鏌勭浉鍏崇殑棰勫鐞嗘煡璇€傚湪璇ヨ皟鐢ㄦ湡闂达紝灏嗗綋鍓嶇粦瀹氱殑鍙傛暟鏍囪绗︾殑鍊煎彂閫佸埌鏈嶅姟鍣紝鏈嶅姟鍣ㄧ敤鏂版彁渚涚殑鏁版嵁鏇挎崲鏍囪绗︺
濡傛灉璇彞鏄UPDATE銆DELETE鎴INSERT锛岄氳繃璋冪敤mysql_stmt_affected_rows()锛屽彲鍙戠幇鏇存敼銆佸垹闄ゆ垨鎻掑叆鐨勬昏鏁般傚鏋滆繖鏄濡SELECT绛夎兘鐢熸垚缁撴灉闆嗙殑璇彞锛岃皟鐢ㄤ换浣曞叾浠栬兘瀵艰嚧鏌ヨ澶勭悊鐨勫嚱鏁颁箣鍓嶏紝蹇呴』璋冪敤mysql_stmt_fetch()鏉ヨ幏鍙栨暟鎹傚叧浜庡浣曡幏鍙栫粨鏋滅殑鏇村淇℃伅锛岃鍙傝25.2.7.11鑺傦紝鈥渕ysql_stmt_fetch()鈥銆
瀵逛簬鐢熸垚缁撴灉闆嗙殑璇彞锛屾墽琛岃鍙ヤ箣鍓嶏紝鍙氳繃璋冪敤mysql_stmt_attr_set()锛璇锋眰mysql_stmt_execute()涓鸿鍙ユ墦寮鍏夋爣銆濡傛灉澶氭鎵ц鏌愪竴璇彞锛屽湪鎵撳紑鏂扮殑鍏夋爣鍓嶏紝mysql_stmt_execute()灏嗗叧闂换浣曞凡鎵撳紑鐨勫厜鏍囥
杩斿洖鍊
濡傛灉鎵ц鎴愬姛锛岃繑鍥0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_OUT_OF_MEMORY
鍐呭瓨婧㈠嚭銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
绀轰緥锛
鍦ㄤ笅闈㈢殑绀轰緥涓紝浠嬬粛浜嗕娇鐢mysql_stmt_init()銆mysql_stmt_prepare()銆mysql_stmt_param_count()銆mysql_stmt_bind_param()銆mysql_stmt_execute()銆佷互鍙mysql_stmt_affected_rows()鍒涘缓鍜屽~鍏呰〃鐨勬柟娉曘傚亣瀹mysql鍙橀噺鍏锋湁鏈夋晥鐨勮繛鎺ュ彞鏌勩
#define STRING_SIZE 50
#define DROP_SAMPLE_TABLE "DROP TABLE IF EXISTS test_table"
#define CREATE_SAMPLE_TABLE "CREATE TABLE test_table(col1 INT,\
col2 VARCHAR(40),\
col3 SMALLINT,\
col4 TIMESTAMP)"
#define INSERT_SAMPLE "INSERT INTO test_table(col1,col2,col3) VALUES(?,?,?)"
MYSQL_STMT *stmt;
MYSQL_BIND bind[3];
my_ulonglong affected_rows;
int param_count;
short small_data;
int int_data;
char str_data[STRING_SIZE];
unsigned long str_length;
my_bool is_null;
if (mysql_query(mysql, DROP_SAMPLE_TABLE))
{
fprintf(stderr, " DROP TABLE failed\n");
fprintf(stderr, " %s\n", mysql_error(mysql));
exit(0);
}
if (mysql_query(mysql, CREATE_SAMPLE_TABLE))
{
fprintf(stderr, " CREATE TABLE failed\n");
fprintf(stderr, " %s\n", mysql_error(mysql));
exit(0);
}
/* Prepare an INSERT query with 3 parameters */
/* (the TIMESTAMP column is not named; the server */
/* sets it to the current date and time) */
stmt = mysql_stmt_init(mysql);
if (!stmt)
{
fprintf(stderr, " mysql_stmt_init(), out of memory\n");
exit(0);
}
if (mysql_stmt_prepare(stmt, INSERT_SAMPLE, strlen(INSERT_SAMPLE)))
{
fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
fprintf(stdout, " prepare, INSERT successful\n");
/* Get the parameter count from the statement */
param_count= mysql_stmt_param_count(stmt);
fprintf(stdout, " total parameters in INSERT: %d\n", param_count);
if (param_count != 3) /* validate parameter count */
{
fprintf(stderr, " invalid parameter count returned by MySQL\n");
exit(0);
}
/* Bind the data for all 3 parameters */
memset(bind, 0, sizeof(bind));
/* INTEGER PARAM */
/* This is a number type, so there is no need to specify buffer_length */
bind[0].buffer_type= MYSQL_TYPE_LONG;
bind[0].buffer= (char *)&int_data;
bind[0].is_null= 0;
bind[0].length= 0;
/* STRING PARAM */
bind[1].buffer_type= MYSQL_TYPE_STRING;
bind[1].buffer= (char *)str_data;
bind[1].buffer_length= STRING_SIZE;
bind[1].is_null= 0;
bind[1].length= &str_length;
/* SMALLINT PARAM */
bind[2].buffer_type= MYSQL_TYPE_SHORT;
bind[2].buffer= (char *)&small_data;
bind[2].is_null= &is_null;
bind[2].length= 0;
/* Bind the buffers */
if (mysql_stmt_bind_param(stmt, bind))
{
fprintf(stderr, " mysql_stmt_bind_param() failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
/* Specify the data values for the first row */
int_data= 10; /* integer */
strncpy(str_data, "MySQL", STRING_SIZE); /* string */
str_length= strlen(str_data);
/* INSERT SMALLINT data as NULL */
is_null= 1;
/* Execute the INSERT statement - 1*/
if (mysql_stmt_execute(stmt))
{
fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
/* Get the total number of affected rows */
affected_rows= mysql_stmt_affected_rows(stmt);
fprintf(stdout, " total affected rows(insert 1): %lu\n",
(unsigned long) affected_rows);
if (affected_rows != 1) /* validate affected rows */
{
fprintf(stderr, " invalid affected rows by MySQL\n");
exit(0);
}
/* Specify data values for second row, then re-execute the statement */
int_data= 1000;
strncpy(str_data, "The most popular Open Source database", STRING_SIZE);
str_length= strlen(str_data);
small_data= 1000; /* smallint */
is_null= 0; /* reset */
/* Execute the INSERT statement - 2*/
if (mysql_stmt_execute(stmt))
{
fprintf(stderr, " mysql_stmt_execute, 2 failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
/* Get the total rows affected */
affected_rows= mysql_stmt_affected_rows(stmt);
fprintf(stdout, " total affected rows(insert 2): %lu\n",
(unsigned long) affected_rows);
if (affected_rows != 1) /* validate affected rows */
{
fprintf(stderr, " invalid affected rows by MySQL\n");
exit(0);
}
/* Close the statement */
if (mysql_stmt_close(stmt))
{
fprintf(stderr, " failed while closing the statement\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
娉ㄩ噴锛氬叧浜庝娇鐢ㄩ澶勭悊璇彞鍑芥暟鐨勫畬鏁寸ず渚嬶紝璇峰弬瑙佹枃浠tests/mysql_client_test.c銆傝鏂囦欢鍙粠MySQL婧愮爜鍒嗗彂鐗堣幏寰楋紝鎴栦粠BitKeeper婧愮爜浠撳簱鑾峰緱銆
int mysql_stmt_fetch(MYSQL_STMT *stmt)
鎻忚堪
mysql_stmt_fetch()杩斿洖缁撴灉闆嗕腑鐨勪笅涓琛屻備粎鑳藉綋缁撴灉闆嗗瓨鍦ㄦ椂璋冪敤瀹冿紝涔熷氨鏄锛岃皟鐢ㄤ簡鑳藉垱寤虹粨鏋滈泦鐨mysql_stmt_execute()涔嬪悗锛鎴栧綋mysql_stmt_execute()瀵规暣涓粨鏋滈泦鍗宠缂撳啿澶勭悊鍚璋冪敤浜mysql_stmt_store_result()銆
浣跨敤mysql_stmt_bind_result()缁戝畾鐨勭紦鍐诧紝mysql_stmt_fetch()杩斿洖琛屾暟鎹傚浜庡綋鍓嶅垪闆嗗悎涓殑鎵鏈夊垪锛屽畠灏嗚繑鍥炵紦鍐插唴鐨勬暟鎹紝骞跺皢闀垮害杩斿洖鍒伴暱搴︽寚閽堛
璋冪敤mysql_stmt_fetch()涔嬪墠锛屽簲鐢ㄧ▼搴忓繀椤荤粦瀹氭墍鏈夊垪銆
濡傛灉鑾峰彇鐨勬暟鎹兼槸NULL鍊硷紝瀵瑰簲MYSQL_BIND缁撴瀯鐨*is_null鍊煎皢鍖呭惈TRUE (1)銆傚惁鍒欙紝灏嗘牴鎹簲鐢ㄧ▼搴忔寚瀹氱殑缂撳啿绫诲瀷锛屽湪*buffer鍜*length鍐呰繑鍥炴暟鎹強鍏堕暱搴︺傛瘡涓暟鍊肩被鍨嬪拰涓存椂绫诲瀷閮芥湁鍥哄畾鐨勯暱搴︼紝璇峰弬瑙佷笅闈㈢殑琛ㄦ牸銆傚瓧绗︿覆绫诲瀷鐨勯暱搴﹀彇鍐充簬鐢data_length鎸囨槑鐨瀹為檯鏁版嵁鍊肩殑闀垮害銆
绫诲瀷 |
闀垮害 |
MYSQL_TYPE_TINY |
1 |
MYSQL_TYPE_SHORT |
2 |
MYSQL_TYPE_LONG |
4 |
MYSQL_TYPE_LONGLONG |
8 |
MYSQL_TYPE_FLOAT |
4 |
MYSQL_TYPE_DOUBLE |
8 |
MYSQL_TYPE_TIME |
sizeof(MYSQL_TIME) |
MYSQL_TYPE_DATE |
sizeof(MYSQL_TIME) |
MYSQL_TYPE_DATETIME |
sizeof(MYSQL_TIME) |
MYSQL_TYPE_STRING |
data length |
MYSQL_TYPE_BLOB |
data_length |
杩斿洖鍊
杩斿洖鍊 |
鎻忚堪 |
0 |
鎴愬姛锛屾暟鎹鎻愬彇鍒板簲鐢ㄧ▼搴忔暟鎹紦鍐插尯銆 |
1 |
鍑虹幇閿欒銆傞氳繃璋冪敤mysql_stmt_errno()鍜mysql_stmt_error()锛屽彲鑾峰彇閿欒浠g爜鍜岄敊璇秷鎭 |
MYSQL_NO_DATA |
涓嶅瓨鍦ㄨ锛忔暟鎹 |
MYSQL_DATA_TRUNCATED |
鍑虹幇鏁版嵁鎴煭銆 |
涓嶈繑鍥MYSQL_DATA_TRUNCATED锛岄櫎闈炵敤mysql_options()鍚敤浜嗘埅鐭氭姤鍔熻兘銆傝繑鍥炶鍊兼椂锛屼负浜嗙‘瀹氭埅鐭殑鍙傛暟鏄摢涓紝鍙鏌MYSQL_BIND鍙傛暟缁撴瀯鐨勯敊璇垚鍛樸
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_OUT_OF_MEMORY
鍐呭瓨婧㈠嚭銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
路 CR_UNSUPPORTED_PARAM_TYPE
缂撳啿绫诲瀷涓MYSQL_TYPE_DATE銆MYSQL_TYPE_TIME銆MYSQL_TYPE_DATETIME銆佹垨MYSQL_TYPE_TIMESTAMP锛屼絾鏁版嵁绫诲瀷涓嶆槸DATE銆TIME銆DATETIME銆佹垨TIMESTAMP銆
路 浠mysql_stmt_bind_result()杩斿洖鎵鏈夊叾浠栦笉鏀寔鐨勮浆鎹㈤敊璇
绀轰緥锛
鍦ㄤ笅闈㈢殑绀轰緥涓紝浠嬬粛浜嗕娇鐢mysql_stmt_result_metadata()銆mysql_stmt_bind_result()鍜mysql_stmt_fetch()浠庤〃涓幏鍙栨暟鎹殑鏂规硶銆傦紙鍦ㄦ湰绀轰緥涓紝灏嗘绱㈠湪25.2.7.10鑺傦紝鈥渕ysql_stmt_execute()鈥涓鑺傜殑绀轰緥涓彃鍏ョ殑涓よ鍐呭锛銆傚亣瀹mysql鍙橀噺鍏锋湁鏈夋晥鐨勮繛鎺ュ彞鏌勩
#define STRING_SIZE 50
#define SELECT_SAMPLE "SELECT col1, col2, col3, col4 FROM test_table"
MYSQL_STMT *stmt;
MYSQL_BIND bind[4];
MYSQL_RES *prepare_meta_result;
MYSQL_TIME ts;
unsigned long length[4];
int param_count, column_count, row_count;
short small_data;
int int_data;
char str_data[STRING_SIZE];
my_bool is_null[4];
/* Prepare a SELECT query to fetch data from test_table */
stmt = mysql_stmt_init(mysql);
if (!stmt)
{
fprintf(stderr, " mysql_stmt_init(), out of memory\n");
exit(0);
}
if (mysql_stmt_prepare(stmt, SELECT_SAMPLE, strlen(SELECT_SAMPLE)))
{
fprintf(stderr, " mysql_stmt_prepare(), SELECT failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
fprintf(stdout, " prepare, SELECT successful\n");
/* Get the parameter count from the statement */
param_count= mysql_stmt_param_count(stmt);
fprintf(stdout, " total parameters in SELECT: %d\n", param_count);
if (param_count != 0) /* validate parameter count */
{
fprintf(stderr, " invalid parameter count returned by MySQL\n");
exit(0);
}
/* Fetch result set meta information */
prepare_meta_result = mysql_stmt_result_metadata(stmt);
if (!prepare_meta_result)
{
fprintf(stderr,
" mysql_stmt_result_metadata(), returned no meta information\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
/* Get total columns in the query */
column_count= mysql_num_fields(prepare_meta_result);
fprintf(stdout, " total columns in SELECT statement: %d\n", column_count);
if (column_count != 4) /* validate column count */
{
fprintf(stderr, " invalid column count returned by MySQL\n");
exit(0);
}
/* Execute the SELECT query */
if (mysql_stmt_execute(stmt))
{
fprintf(stderr, " mysql_stmt_execute(), failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
/* Bind the result buffers for all 4 columns before fetching them */
memset(bind, 0, sizeof(bind));
/* INTEGER COLUMN */
bind[0].buffer_type= MYSQL_TYPE_LONG;
bind[0].buffer= (char *)&int_data;
bind[0].is_null= &is_null[0];
bind[0].length= &length[0];
/* STRING COLUMN */
bind[1].buffer_type= MYSQL_TYPE_STRING;
bind[1].buffer= (char *)str_data;
bind[1].buffer_length= STRING_SIZE;
bind[1].is_null= &is_null[1];
bind[1].length= &length[1];
/* SMALLINT COLUMN */
bind[2].buffer_type= MYSQL_TYPE_SHORT;
bind[2].buffer= (char *)&small_data;
bind[2].is_null= &is_null[2];
bind[2].length= &length[2];
/* TIMESTAMP COLUMN */
bind[3].buffer_type= MYSQL_TYPE_TIMESTAMP;
bind[3].buffer= (char *)&ts;
bind[3].is_null= &is_null[3];
bind[3].length= &length[3];
/* Bind the result buffers */
if (mysql_stmt_bind_result(stmt, bind))
{
fprintf(stderr, " mysql_stmt_bind_result() failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
/* Now buffer all results to client */
if (mysql_stmt_store_result(stmt))
{
fprintf(stderr, " mysql_stmt_store_result() failed\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
/* Fetch all rows */
row_count= 0;
fprintf(stdout, "Fetching results ...\n");
while (!mysql_stmt_fetch(stmt))
{
row_count++;
fprintf(stdout, " row %d\n", row_count);
/* column 1 */
fprintf(stdout, " column1 (integer) : ");
if (is_null[0])
fprintf(stdout, " NULL\n");
else
fprintf(stdout, " %d(%ld)\n", int_data, length[0]);
/* column 2 */
fprintf(stdout, " column2 (string) : ");
if (is_null[1])
fprintf(stdout, " NULL\n");
else
fprintf(stdout, " %s(%ld)\n", str_data, length[1]);
/* column 3 */
fprintf(stdout, " column3 (smallint) : ");
if (is_null[2])
fprintf(stdout, " NULL\n");
else
fprintf(stdout, " %d(%ld)\n", small_data, length[2]);
/* column 4 */
fprintf(stdout, " column4 (timestamp): ");
if (is_null[3])
fprintf(stdout, " NULL\n");
else
fprintf(stdout, " %04d-%02d-%02d %02d:%02d:%02d (%ld)\n",
ts.year, ts.month, ts.day,
ts.hour, ts.minute, ts.second,
length[3]);
fprintf(stdout, "\n");
}
/* Validate rows fetched */
fprintf(stdout, " total rows fetched: %d\n", row_count);
if (row_count != 2)
{
fprintf(stderr, " MySQL failed to return all rows\n");
exit(0);
}
/* Free the prepared result metadata */
mysql_free_result(prepare_meta_result);
/* Close the statement */
if (mysql_stmt_close(stmt))
{
fprintf(stderr, " failed while closing the statement\n");
fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
exit(0);
}
int mysql_stmt_fetch_column(MYSQL_STMT *stmt, MYSQL_BIND *bind, unsigned int column, unsigned long offset)
鎻忚堪
浠庡綋鍓嶇粨鏋滈泦琛岃幏鍙1鍒椼傗bind鈥鎻愪緵浜嗗簲灏嗘暟鎹疆浜庡叾涓殑缂撳啿銆傚叾璁剧疆鏂规硶搴斾笌璁剧疆mysql_stmt_bind_result()鐨勭浉鍚屻鈥column鈥鎸囨槑浜嗗皢鑾峰彇鍝釜鍒椼傜1鍒楃紪鍙蜂负0銆鈥offset鈥鏄暟鎹煎唴鐨勫亸绉婚噺锛屽皢浠庤澶勫紑濮嬫绱㈡暟鎹傚彲灏嗗叾鐢ㄤ簬鑾峰彇纰庣墖褰㈠紡鐨勬暟鎹笺鍊煎紑濮嬮儴鍒嗙殑鍋忕Щ閲忎负0銆
杩斿洖鍊
濡傛灉鎴愬姛鑾峰彇浜嗗硷紝杩斿洖0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
路 CR_INVALID_PARAMETER_NO
Invalid column number.
路 CR_NO_DATA
宸叉姷杈剧粨鏋滈泦鐨勬湯灏俱
unsigned int mysql_stmt_field_count(MYSQL_STMT *stmt)
鎻忚堪
涓鸿鍙ュ鐞嗙▼搴忚繑鍥炲叧浜庢渶杩戣鍙ョ殑琛屾暟銆傚浜庤濡INSERT鎴DELETE绛変笉鐢熸垚缁撴灉闆嗙殑璇彞锛岃鍊间负0銆
閫氳繃璋冪敤mysql_stmt_prepare()鍑嗗濂戒簡璇彞鍚庯紝鍙皟鐢mysql_stmt_field_count()銆
杩斿洖鍊
琛ㄧず缁撴灉闆嗕腑琛屾暟鐨勬棤绗﹀彿鏁存暟銆
閿欒
鏃犮
my_bool mysql_stmt_free_result(MYSQL_STMT *stmt)
鎻忚堪
閲婃斁涓庢墽琛岄澶勭悊璇彞鐢熸垚鐨勭粨鏋滈泦鏈夊叧鐨勫唴瀛樸傚浜庤璇彞锛屽鏋滃瓨鍦ㄦ墦寮鐨勫厜鏍囷紝mysql_stmt_free_result()灏嗗叧闂畠銆
杩斿洖鍊
濡傛灉鎴愬姛閲婃斁浜嗙粨鏋滈泦锛岃繑鍥0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
MYSQL_STMT *mysql_stmt_init(MYSQL *mysql)
鎻忚堪
鍒涘缓MYSQL_STMT鍙ユ焺銆瀵逛簬璇ュ彞鏌勶紝搴斾娇鐢mysql_stmt_close(MYSQL_STMT *)閲婃斁銆
杩斿洖鍊
鎴愬姛鏃讹紝杩斿洖鎸囧悜MYSQL_STMT缁撴瀯鐨勬寚閽銆傚鏋滃唴瀛樻孩鍑猴紝杩斿洖NULL銆
閿欒
路 CR_OUT_OF_MEMORY
鍐呭瓨婧㈠嚭銆
my_ulonglong mysql_stmt_insert_id(MYSQL_STMT *stmt)
鎻忚堪
杩斿洖棰勫鐞INSERT鎴UPDATE璇彞涓AUTO_INCREMENT鍒楃敓鎴愮殑鍊笺傚湪鍖呭惈AUTO_INCREMENT瀛楁鐨勮〃涓婃墽琛屼簡棰勫鐞INSERT璇彞鍚庯紝浣跨敤璇ュ嚱鏁般
鏇村淇℃伅锛岃鍙傝25.2.3.36鑺傦紝鈥渕ysql_insert_id()鈥銆
杩斿洖鍊
涓哄湪鎵ц棰勫鐞嗚鍙ユ湡闂磋嚜鍔ㄧ敓鎴愭垨鏄庣‘璁剧疆鐨AUTO_INCREMENT鍒楄繑鍥炲硷紝鎴鐢LAST_INSERT_ID(expr)鍑芥暟鐢熸垚鐨勫笺傚鏋滆鍙ユ湭璁剧疆AUTO_INCREMENT鍊硷紝杩斿洖鍊间笉纭畾銆
閿欒
鏃犮
my_ulonglong mysql_stmt_num_rows(MYSQL_STMT *stmt)
鎻忚堪
杩斿洖缁撴灉闆嗕腑鐨勮鏁般
mysql_stmt_num_rows()鐨勭敤娉曞彇鍐充簬鏄惁浣跨敤浜mysql_stmt_store_result()鏉ュ璇彞鍙ユ焺涓殑鍏ㄩ儴缁撴灉闆嗚繘琛屼簡缂撳啿澶勭悊銆
濡傛灉浣跨敤浜mysql_stmt_store_result()锛屽彲绔嬪埢璋冪敤mysql_stmt_num_rows()銆
杩斿洖鍊
缁撴灉闆嗕腑鐨勮鏁般
閿欒
鏃犮
unsigned long mysql_stmt_param_count(MYSQL_STMT *stmt)
鎻忚堪
杩斿洖棰勫鐞嗚鍙ヤ腑鍙傛暟鏍囪绗︾殑鏁扮洰銆
杩斿洖鍊
琛ㄧず璇彞涓弬鏁版暟鐩殑鏃犵鍙烽暱鏁存暟銆
閿欒
鏃犮
绀轰緥锛
鍏充簬mysql_stmt_param_count()鐨勭敤娉曪紝璇峰弬瑙25.2.7.10鑺傦紝鈥渕ysql_stmt_execute()鈥涓粰鍑虹殑绀轰緥銆
int mysql_stmt_prepare(MYSQL_STMT *stmt, const char *query, unsigned long length)
鎻忚堪
缁欏畾mysql_stmt_init()杩斿洖鐨勮鍙ュ彞鏌勶紝鍑嗗瀛楃涓叉煡璇㈡寚鍚戠殑SQL璇彞锛屽苟杩斿洖鐘舵佸笺傚瓧绗︿覆闀垮害搴旂敱鈥length鈥濆弬閲忕粰鍑恒傚瓧绗︿覆蹇呴』鍖呭惈1鏉SQL璇彞銆備笉搴斾负璇彞娣诲姞缁堢粨鐢ㄥ垎鍙(鈥;鈥)鎴\g銆
閫氳繃灏嗛棶鍙峰瓧绗︹?鈥濆祵鍏ュ埌SQL瀛楃涓茬殑鎭板綋浣嶇疆锛屽簲鐢ㄧ▼搴忓彲鍖呭惈SQL璇彞涓殑涓涓垨澶氫釜鍙傛暟鏍囪绗︺
鏍囪绗︿粎鍦SQL璇彞涓殑鐗瑰畾浣嶇疆鏃舵墠鏄悎娉曠殑銆備緥濡傦紝瀹冨彲浠ュ湪INSERT璇彞鐨VALUES()鍒楄〃涓锛堜负琛屾寚瀹氬垪鍊硷級锛屾垨涓WHERE瀛愬彞涓煇鍒楃殑姣旇緝閮ㄥ垎锛堢敤浠ユ寚瀹氭瘮杈冨硷級銆備絾鏄紝瀵逛簬ID锛堜緥濡傝〃鍚嶆垨鍒楀悕锛夛紝涓嶅厑璁镐娇鐢ㄥ畠浠紝涓嶅厑璁告寚瀹氫簩杩涘埗鎿嶄綔绗︼紙濡傜瓑浜庡彿鈥=鈥濓級鐨勬搷浣滄暟銆傚悗涓涓檺鍒舵槸鏈夊繀瑕佺殑锛屽師鍥犲湪浜庯紝鏃犳硶纭畾鍙傛暟绫诲瀷銆備竴鑸岃█锛屽弬鏁颁粎鍦DML锛堟暟鎹搷浣滆瑷锛夎鍙ヤ腑鎵嶆槸鍚堟硶鐨勶紝鍦DDL锛堟暟鎹畾涔夎瑷锛夎鍙ヤ腑涓嶅悎娉曘
鎵ц璇彞涔嬪墠锛屽繀椤讳娇鐢mysql_stmt_bind_param()锛屽皢鍙傛暟鏍囪绗︿笌搴旂敤绋嬪簭鍙橀噺缁戝畾鍦ㄤ竴璧枫
杩斿洖鍊
濡傛灉鎴愬姛澶勭悊浜嗚鍙ワ紝杩斿洖0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_OUT_OF_MEMORY
鍐呭瓨婧㈠嚭銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鏌ヨ杩囩▼涓紝涓庢湇鍔″櫒鐨勮繛鎺ヤ涪澶便
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
濡傛灉鍑嗗鎿嶄綔澶辫触锛堝嵆mysql_stmt_prepare()杩斿洖闈0鍊硷級锛屽彲閫氳繃璋冪敤mysql_stmt_error()鑾峰彇閿欒娑堟伅銆
绀轰緥锛
鍏充簬mysql_stmt_prepare()鐨勭敤娉曪紝璇峰弬瑙25.2.7.10鑺傦紝鈥渕ysql_stmt_execute()鈥涓粰鍑虹殑绀轰緥銆
my_bool mysql_stmt_reset(MYSQL_STMT *stmt)
鎻忚堪
鍦ㄥ鎴风鍜屾湇鍔″櫒涓婏紝灏嗛澶勭悊璇彞澶嶄綅涓哄畬鎴愬噯澶囧悗鐨勭姸鎬併備富瑕佺敤浜庡浣嶇敤mysql_stmt_send_long_data()鍙戝嚭鐨勬暟鎹傚浜庤鍙ワ紝浠讳綍宸叉墦寮鐨勫厜鏍囧皢琚叧闂
瑕佹兂閲嶆柊鍑嗗鐢ㄤ簬鍙︿竴鏌ヨ鐨勮鍙ワ紝鍙娇鐢mysql_stmt_prepare()銆
杩斿洖鍊
濡傛灉璇彞鎴愬姛澶嶄綅锛岃繑鍥0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鏌ヨ杩囩▼涓紝涓庢湇鍔″櫒鐨勮繛鎺ヤ涪澶便
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
MYSQL_RES *mysql_stmt_result_metadata(MYSQL_STMT *stmt)
鎻忚堪
濡傛灉浼犻掔粰mysql_stmt_prepare()鐨勮鍙ヨ兘澶熸垚鐢熺粨鏋滈泦锛mysql_stmt_result_metadata()灏嗕互鎸囬拡鐨勫舰寮忚繑鍥炵粨鏋滈泦鍏冩暟鎹紝璇ユ寚閽堟寚鍚MYSQL_RES缁撴瀯锛屽彲鐢ㄤ簬澶勭悊鍏冧俊鎭紝濡鎬荤殑瀛楁鏁颁互鍙婂崟鐙殑瀛楁淇℃伅銆傝缁撴灉闆嗘寚閽堝彲浣滀负鍙傞噺浼犻掔粰浠讳綍鍩轰簬瀛楁涓旂敤浜庡鐞嗙粨鏋滈泦鍏冩暟鎹殑API鍑芥暟锛屽锛
路 mysql_num_fields()
路 mysql_fetch_field()
路 mysql_fetch_field_direct()
路 mysql_fetch_fields()
路 mysql_field_count()
路 mysql_field_seek()
路 mysql_field_tell()
路 mysql_free_result()
瀹屾垚鎿嶄綔鍚庯紝搴旈噴鏀剧粨鏋滈泦缁撴瀯锛屽彲閫氳繃灏嗗叾浼犻掔粰mysql_free_result()瀹屾垚銆傚畠涓庨噴鏀鹃氳繃mysql_store_result()璋冪敤鑾峰緱鐨勭粨鏋滈泦鐨勬柟娉曠被浼笺
mysql_stmt_result_metadata()杩斿洖鐨勭粨鏋滈泦浠呭寘鍚厓鏁版嵁銆備笉鍚换浣曡缁撴灉銆備笌mysql_stmt_fetch()涓璧蜂娇鐢ㄨ鍙ュ彞鏌勶紝鍙幏鍙栬銆
杩斿洖鍊
MYSQL_RES缁撴灉缁撴瀯銆傚鏋滀笉瀛樺湪鍏充簬棰勫鐞嗘煡璇㈢殑浠讳綍鍏冧俊鎭紝杩斿洖NULL銆
閿欒
路 CR_OUT_OF_MEMORY
鍐呭瓨婧㈠嚭銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
绀轰緥锛
鍏充簬mysql_stmt_result_metadata()鐨勭敤娉曪紝璇峰弬瑙25.2.7.11鑺傦紝鈥渕ysql_stmt_fetch()鈥涓粰鍑虹殑绀轰緥銆
MYSQL_ROW_OFFSET mysql_stmt_row_seek(MYSQL_STMT *stmt, MYSQL_ROW_OFFSET offset)
鎻忚堪
灏嗚鍏夋爣璁剧疆鍒拌鍙ョ粨鏋滈泦涓殑浠绘剰琛屻傗offset鈥鍊兼槸琛屽亸绉荤殑鍊硷紝琛屽亸绉诲簲鏄浠mysql_stmt_row_tell()鎴mysql_stmt_row_seek()杩斿洖鐨勫笺璇ュ间笉鏄缂栧彿锛屽鏋滄墦绠楁寜缂栧彿鏌ユ壘缁撴灉闆嗕腑鐨勮锛屽彲浣跨敤mysql_stmt_data_seek()鍙栬屼唬涔嬨
璇ュ嚱鏁拌姹傜粨鏋滈泦缁撴瀯鍖呭惈鏌ヨ鐨勫叏閮ㄧ粨鏋滐紝浠ヤ究mysql_stmt_row_seek()鑳藉浠呬笌mysql_stmt_store_result()涓璧蜂娇鐢ㄣ
杩斿洖鍊
琛屽厜鏍囩殑鍓嶄竴涓笺傚彲浠ュ皢璇ュ兼崲閫掔粰鍚庣画鐨mysql_stmt_row_seek()璋冪敤銆
閿欒
鏃犮
MYSQL_ROW_OFFSET mysql_stmt_row_tell(MYSQL_STMT *stmt)
鎻忚堪
杩斿洖閽堝鍓嶄竴涓mysql_stmt_fetch()鐨勮鍏夋爣鐨勫綋鍓嶄綅缃傝鍊煎彲鐢ㄤ綔mysql_stmt_row_seek()鐨勫弬閲忋
浠呭簲鍦mysql_stmt_store_result()涔嬪悗浣跨敤mysql_stmt_row_tell()銆
杩斿洖鍊
琛屽厜鏍囩殑褰撳墠鍋忕Щ閲忋
閿欒
鏃犮
my_bool mysql_stmt_send_long_data(MYSQL_STMT *stmt, unsigned int parameter_number, const char *data, unsigned long length)
鎻忚堪
鍏佽搴旂敤绋嬪簭鍒嗘鍦帮紙鍒嗗潡锛夊皢鍙傛暟鏁版嵁鍙戦佸埌鏈嶅姟鍣ㄣ傚彲浠ュ娆¤皟鐢ㄨ鍑芥暟锛屼互渚垮彂閫佸叧浜庢煇涓鍒楃殑瀛楃鎴栦簩杩涘埗鏁版嵁鐨勪笉鍚岄儴鍒嗭紝鍒楀繀椤绘槸TEXT鎴BLOB鏁版嵁绫诲瀷涔嬩竴銆
鈥減arameter_number鈥鎸囨槑浜嗕笌鏁版嵁鍏宠仈鐨勫弬鏁般傚弬鏁颁粠0寮濮嬬紪鍙枫鈥渄ata鈥鏄寚鍚戝寘鍚皢瑕佸彂閫佺殑鏁版嵁鐨勭紦鍐插尯鐨勬寚閽堬紝鈥length鈥鎸囨槑浜嗙紦鍐插尯鍐呯殑瀛楄妭鏁般
娉ㄩ噴锛鑷笂涓涓mysql_stmt_execute()鎴mysql_stmt_reset()鍚庯紝瀵逛簬涓mysql_stmt_send_long_data()涓璧蜂娇鐢ㄧ殑鎵鏈夊弬鏁帮紝涓嬩竴涓mysql_stmt_execute()璋冪敤灏嗗拷鐣缁戝畾缂撳啿銆
濡傛灉甯屾湜澶嶄綅锛忓拷鐣ュ凡鍙戦佺殑鏁版嵁锛屽彲浣跨敤mysql_stmt_reset()銆傝鍙傝25.2.7.21鑺傦紝鈥渕ysql_stmt_reset()鈥銆
杩斿洖鍊
濡傛灉鎴愬姛鍦板皢鏁版嵁鍙戦佸埌鏈嶅姟鍣紝杩斿洖0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_OUT_OF_MEMORY
鍐呭瓨婧㈠嚭銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
绀轰緥锛
鍦ㄤ笅闈㈢殑绀轰緥涓紝浠嬬粛浜嗕互淇℃伅鍧楀舰寮忎负TEXT鍒楀彂閫佹暟鎹殑鏂规硶銆傚畠浼氬皢鏁版嵁鍊尖MySQL锛屾渶娴佽鐨勫紑鏀炬簮鐮佹暟鎹簱鈥濇彃鍏ュ埌text_column鍒椾腑銆鍋囧畾mysql鍙橀噺鍏锋湁鏈夋晥鐨勮繛鎺ュ彞鏌勩
#define INSERT_QUERY "INSERT INTO test_long_data(text_column) VALUES(?)"
MYSQL_BIND bind[1];
long length;
smtt = mysql_stmt_init(mysql);
if (!stmt)
{
fprintf(stderr, " mysql_stmt_init(), out of memory\n");
exit(0);
}
if (mysql_stmt_prepare(stmt, INSERT_QUERY, strlen(INSERT_QUERY)))
{
fprintf(stderr, "\n mysql_stmt_prepare(), INSERT failed");
fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
exit(0);
}
memset(bind, 0, sizeof(bind));
bind[0].buffer_type= MYSQL_TYPE_STRING;
bind[0].length= &length;
bind[0].is_null= 0;
/* Bind the buffers */
if (mysql_stmt_bind_param(stmt, bind))
{
fprintf(stderr, "\n param bind failed");
fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
exit(0);
}
/* Supply data in chunks to server */
if (!mysql_stmt_send_long_data(stmt,0,"MySQL",5))
{
fprintf(stderr, "\n send_long_data failed");
fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
exit(0);
}
/* Supply the next piece of data */
if (mysql_stmt_send_long_data(stmt,0," - The most popular Open Source database",40))
{
fprintf(stderr, "\n send_long_data failed");
fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
exit(0);
}
/* Now, execute the query */
if (mysql_stmt_execute(stmt))
{
fprintf(stderr, "\n mysql_stmt_execute failed");
fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
exit(0);
}
const char *mysql_stmt_sqlstate(MYSQL_STMT *stmt)
鎻忚堪
瀵逛簬鐢stmt鎸囧畾鐨勮鍙ワ紝mysql_stmt_sqlstate()杩斿洖鐢Null缁堢粨鐨勫瓧绗︿覆锛岃瀛楃涓插寘鍚拡瀵规渶杩戣皟鐢ㄩ澶勭悊璇彞API鍑芥暟鐨SQLSTATE閿欒浠g爜锛岃鍑芥暟鎴栨垚鍔熸垨澶辫触銆傞敊璇唬鐮佺敱5涓瓧绗︽瀯鎴愩"00000"琛ㄧず鈥滄棤閿欒鈥濄傝繖浜涘肩敱ANSI SQL鍜ODBC鎸囧畾銆傚叧浜庡彲鑳藉肩殑鍒楄〃锛岃鍙傝闄勫綍B锛氶敊璇唬鐮佸拰娑堟伅銆
娉ㄦ剰锛屽苟闈炴墍鏈夌殑MySQL閿欒鍧囦細琚槧灏勫埌SQLSTATE浠g爜銆傚"HY000"锛堜竴鑸敊璇級鐢ㄤ簬鏈槧灏勭殑閿欒銆
杩斿洖鍊
鍖呭惈SQLSTATE閿欒浠g爜銆佺敱Null缁堢粨鐨勫瓧绗︿覆銆
int mysql_stmt_store_result(MYSQL_STMT *stmt)
鎻忚堪
瀵逛簬鎴愬姛鐢熸垚缁撴灉闆嗙殑鎵鏈夎鍙ワ紙SELECT銆SHOW銆DESCRIBE銆EXPLAIN锛夛紝鑰屼笖浠呭綋浣犳墦绠楀瀹㈡埛绔殑鍏ㄩ儴缁撴灉闆嗚繘琛岀紦鍐插鐞嗘椂锛屽繀椤昏皟鐢mysql_stmt_store_result()锛屼互渚垮悗缁殑mysql_stmt_fetch()璋冪敤鑳借繑鍥炵紦鍐叉暟鎹
瀵逛簬鍏朵粬璇彞锛屾病鏈夊繀瑕佽皟鐢mysql_stmt_store_result()锛屼絾濡傛灉璋冪敤浜嗗畠锛涔熶笉浼氶犳垚浠讳綍浼ゅ鎴栧鑷翠换浣曟ц兘闂銆傞氳繃妫鏌mysql_stmt_result_metadata()鏄惁杩斿洖NULL锛屽彲妫娴嬭鍙ユ槸鍚︾敓鎴愪簡缁撴灉闆嗐鏇村淇℃伅锛岃鍙傝25.2.7.22鑺傦紝鈥渕ysql_stmt_result_metadata()鈥銆
娉ㄩ噴锛榛樿鎯呭喌涓嬶紝瀵逛簬mysql_stmt_store_result()涓殑鎵鏈夊垪锛MySQL涓嶈绠MYSQL_FIELD->max_length锛岃繖鏄洜涓猴紝璁$畻瀹冧細鏄捐憲闄嶄綆mysql_stmt_store_result()鐨勬ц兘锛岃屼笖澶у鏁板簲鐢ㄧ▼搴涓嶉渶瑕max_length銆傚鏋滄墦绠楁洿鏂max_length锛鍙氳繃璋冪敤mysql_stmt_attr_set(MYSQL_STMT, STMT_ATTR_UPDATE_MAX_LENGTH, &flag)鍚敤瀹冦傝鍙傝25.2.7.3鑺傦紝鈥渕ysql_stmt_attr_set()鈥銆
杩斿洖鍊
濡傛灉鎴愬姛瀹屾垚浜嗗缁撴灉鐨勭紦鍐插鐞嗭紝杩斿洖0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
閿欒
路 CR_COMMANDS_OUT_OF_SYNC
浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆
路 CR_OUT_OF_MEMORY
鍐呭瓨婧㈠嚭銆
路 CR_SERVER_GONE_ERROR
MySQL鏈嶅姟鍣ㄤ笉鍙敤銆
路 CR_SERVER_LOST
鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆
路 CR_UNKNOWN_ERROR
鍑虹幇鏈煡閿欒銆
涓嬮潰鍒楀嚭浜嗕竴浜涚洰鍓嶅凡鐭ョ殑涓庨澶勭悊璇彞鏈夊叧鐨勯棶棰橈細
路 TIME銆TIMESTAMP鍜DATETIME涓嶆敮鎸佺閮ㄥ垎锛屼緥濡傛潵鑷DATE_FORMAT()鐨勭閮ㄥ垎銆
路 灏嗘暣鏁拌浆鎹负瀛楃涓叉椂锛屽湪鏌愪簺鎯呭喌涓嬶紝褰MySQL涓嶆墦鍗板墠瀵0鏃讹紝鍙笌棰勫鐞嗚鍙ヤ竴璧蜂娇鐢ZEROFILL銆備緥濡傦紝涓MIN(number-with-zerofill)涓璧枫
路 灏嗘诞鐐规暟杞崲涓哄鎴风涓殑瀛楃涓叉椂锛岃杞崲鍊兼渶鍙充晶鐨勪綅鍙兘浼氫笌鍘熷鍊肩殑鏈夋墍涓嶅悓銆
路 棰勫鐞嗚鍙ヤ笉浣跨敤鏌ヨ楂橀熺紦鍐诧紝鍗充娇褰撴煡璇笉鍚换浣曞崰浣嶇鏃朵篃鍚屾牱銆傘璇峰弬瑙5.13.1鑺傦紝鈥滄煡璇㈤珮閫熺紦鍐插浣曞伐浣溾銆
鍦ㄩ粯璁ゆ儏鍐典笅锛mysql_query()鍜mysql_real_query()浠呰繑鍥炵1涓煡璇㈢殑鐘舵侊紝骞惰兘浣跨敤mysql_more_results()鍜mysql_next_result()瀵瑰悗缁煡璇㈢殑鐘舵佽繘琛屽鐞嗐
/* Connect to server with option CLIENT_MULTI_STATEMENTS */
mysql_real_connect(..., CLIENT_MULTI_STATEMENTS);
/* Now execute multiple queries */
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");
do
{
/* Process all results */
...
printf("total affected rows: %lld", mysql_affected_rows(mysql));
...
if (!(result= mysql_store_result(mysql)))
{
printf(stderr, "Got fatal error processing query\n");
exit(1);
}
process_result_set(result); /* client function */
mysql_free_result(result);
} while (!mysql_next_result(mysql));
澶氳鍙ュ姛鑳藉彲涓mysql_query()鎴mysql_real_query()涓璧蜂娇鐢ㄣ傚畠涓嶈兘涓庨澶勭悊璇彞鎺ュ彛涓璧蜂娇鐢ㄣ傛寜鐓у畾涔夛紝棰勫鐞嗚鍙ヤ粎鑳戒笌鍖呭惈鍗曚釜璇彞鐨勫瓧绗︿覆涓璧蜂娇鐢ㄣ
浜岃繘鍒跺崗璁厑璁镐綘浣跨敤MYSQL_TIME缁撴瀯鍙戦佸拰鎺ュ彈鏃ユ湡鍜屾椂闂村硷紙DATE銆TIME銆DATETIME鍜TIMESTAMP锛夈鍦25.2.5鑺傦紝鈥淐 API棰勫鐞嗚鍙ョ殑鏁版嵁绫诲瀷鈥涓紝浠嬬粛浜嗚缁撴瀯鐨勬垚鍛樸
瑕佹兂鍙戦佷复鏃舵暟鎹硷紝鍙娇鐢mysql_stmt_prepare()鍒涘缓棰勫鐞嗚鍙ャ傜劧鍚庯紝鍦ㄨ皟鐢mysql_stmt_execute()鎵ц璇彞涔嬪墠锛屽彲閲囩敤涓嬭堪姝ラ璁剧疆姣忎釜涓存椂鍙傛暟锛
1. 鍦ㄤ笌鏁版嵁鍊肩浉鍏崇殑MYSQL_BIND缁撴瀯涓紝灏buffer_type鎴愬憳璁剧疆涓虹浉搴旂殑绫诲瀷锛岃绫诲瀷鎸囨槑浜嗗彂閫佺殑涓存椂鍊肩被鍨嬨瀵逛簬DATE銆TIME銆DATETIME鎴TIMESTAMP鍊硷紝灏buffer_type鍒嗗埆璁剧疆涓MYSQL_TYPE_DATE銆MYSQL_TYPE_TIME銆MYSQL_TYPE_DATETIME鎴MYSQL_TYPE_TIMESTAMP銆
2. 灏MYSQL_BIND缁撴瀯鐨缂撳啿鎴愬憳璁剧疆涓虹敤浜庝紶閫掍复鏃跺肩殑MYSQL_TIME缁撴瀯鐨勫湴鍧銆
3. 濉厖MYSQL_TIME缁撴瀯鐨勬垚鍛橈紝浣夸箣涓庢墦绠椾紶閫掔殑涓存椂鏀殑绫诲瀷鐩哥銆
浣跨敤mysql_stmt_bind_param()灏嗗弬鏁版暟鎹粦瀹氬埌璇彞銆傜劧鍚庡彲璋冪敤mysql_stmt_execute()銆
瑕佹兂妫绱复鏃跺硷紝鍙噰鐢ㄧ被浼肩殑姝ラ锛屼絾搴斿皢buffer_type鎴愬憳璁剧疆涓烘墦绠楁帴鍙楃殑鍊肩殑绫诲瀷锛屽苟灏缂撳啿鎴愬憳璁句负搴斿皢杩斿洖鍊肩疆浜庡叾涓殑MYSQL_TIME缁撴瀯鐨勫湴鍧銆傝皟鐢mysql_stmt_execute()涔嬪悗锛屽苟鍦ㄨ幏鍙栫粨鏋滀箣鍓嶏紝浣跨敤mysql_bind_results()灏嗙紦鍐茬粦瀹氬埌璇彞涓娿
涓嬮潰缁欏嚭浜嗕竴涓彃鍏DATE銆TIME鍜TIMESTAMP鏁版嵁鐨勭畝鍗曠ず渚銆傚亣瀹mysql鍙橀噺鍏锋湁鏈夋晥鐨勮繛鎺ュ彞鏌勩
MYSQL_TIME ts; MYSQL_BIND bind[3]; MYSQL_STMT *stmt; strmov(query, "INSERT INTO test_table(date_field, time_field, timestamp_field) VALUES(?,?,?"); stmt = mysql_stmt_init(mysql); if (!stmt) { fprintf(stderr, " mysql_stmt_init(), out of memory\n"); exit(0); } if (mysql_stmt_prepare(mysql, query, strlen(query))) { fprintf(stderr, "\n mysql_stmt_prepare(), INSERT failed"); fprintf(stderr, "\n %s", mysql_stmt_error(stmt)); exit(0); } /* set up input buffers for all 3 parameters */ bind[0].buffer_type= MYSQL_TYPE_DATE; bind[0].buffer= (char *)&ts; bind[0].is_null= 0; bind[0].length= 0; ... bind[1]= bind[2]= bind[0]; ... mysql_stmt_bind_param(stmt, bind); /* supply the data to be sent in the ts structure */ ts.year= 2002; ts.month= 02; ts.day= 03; ts.hour= 10; ts.minute= 45; ts.second= 20; mysql_stmt_execute(stmt); ..
褰撲綘鎵撶畻鍒涘缓绾跨▼瀹㈡埛绔椂锛岄渶瑕佷娇鐢ㄤ笅杩板嚱鏁般傝鍙傝25.2.15鑺傦紝鈥滃浣曠敓鎴愮嚎绋嬪紡瀹㈡埛绔銆
void my_init(void)
鎻忚堪
璋冪敤浠讳綍MySQL鍑芥暟涔嬪墠锛岄渶瑕佸湪绋嬪簭涓皟鐢ㄨ鍑芥暟銆傚畠灏嗗垵濮嬪寲MySQL鎵闇鐨勬煇浜涘叏灞鍙橀噺銆傚鏋滀綘姝e湪浣跨敤绾跨▼瀹夊叏瀹㈡埛绔簱锛屽畠杩樿兘涓鸿绾跨▼璋冪敤mysql_thread_init()銆
閫氳繃mysql_init()銆mysql_library_init()銆mysql_server_init()鍜mysql_connect()锛屽彲鑷姩璋冪敤璇ュ嚱鏁般
杩斿洖鍊
鏃犮
my_bool mysql_thread_init(void)
鎻忚堪
瀵逛簬姣忎釜鍒涘缓鐨勭嚎绋嬶紝闇瑕佽皟鐢ㄨ鍑芥暟鏉ュ垵濮嬪寲涓庣嚎绋嬬浉鍏崇殑鍙橀噺銆
瀹冨彲鐢my_init()鍜mysql_connect()鑷姩璋冪敤銆
杩斿洖鍊
濡傛灉鎴愬姛锛岃繑鍥0锛屽鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺
濡傛灉甯屾湜鍏佽搴旂敤绋嬪簭閾炬帴鍒板祵鍏ュ紡MySQL鏈嶅姟鍣ㄥ簱锛屽繀椤讳娇鐢mysql_server_init()鍜mysql_server_end()鍑芥暟銆傝鍙傝25.1鑺傦紝鈥渓ibmysqld锛屽祵鍏ュ紡MySQL鏈嶅姟鍣ㄥ簱鈥銆
浣嗘槸锛岃鎯虫彁渚涙敼杩涚殑鍐呭瓨绠$悊锛屽嵆浣挎槸瀵逛笌鈥-lmysqlclient鈥濊屼笉鏄笌鈥-lmysqld鈥濋摼鎺ョ殑绋嬪簭锛屼篃搴斿寘鍚惎鐢ㄥ拰缁撴潫搴撲娇鐢ㄧ殑璋冪敤銆mysql_library_init()鍜mysql_library_end()鍑芥暟鍙敤浜庤鐩殑銆傚畠浠疄闄呬笂鏄娇鍏剁瓑鏁堜簬mysql_server_init()鍜mysql_server_end()鐨#define绗﹀彿锛屼絾瀹冧滑鐨勫悕绉版洿娓呮鍦版寚鏄庯紝鏃犺搴旂敤绋嬪簭浣跨敤鐨勬槸libmysqlclient鎴libmysqld锛屽紑濮嬩娇鐢ㄦ垨缁撴潫MySQL C API搴撶殑浣跨敤鏃讹紝搴旇皟鐢ㄥ畠浠傚叧浜庢洿澶氫俊鎭紝璇峰弬瑙25.2.2鑺傦紝鈥淐 API鍑芥暟姒傝堪鈥銆
int mysql_server_init(int argc, char **argv, char **groups)
鎻忚堪
璋冪敤浠讳綍鍏朵粬MySQL鍑芥暟涔嬪墠锛屽繀椤诲湪浣跨敤宓屽叆寮忔湇鍔″櫒鐨勭▼搴忎腑璋冪敤璇ュ嚱鏁般傚畠灏嗗惎鍔ㄦ湇鍔″櫒锛屽苟鍒濆鍖栨湇鍔″櫒浣跨敤鐨勪换浣曞瓙绯荤粺锛mysys銆InnoDB绛夛級銆傚鏋滄湭璋冪敤璇ュ嚱鏁帮紝瀵mysql_init()鐨勪笅涓娆¤皟鐢ㄥ皢鎵цmysql_server_init()銆傚鏋滀綘姝e湪浣跨敤涓MySQL涓璧锋彁渚涚殑DBUG杞欢鍖咃紝搴斿湪璋冪敤浜my_init()涔嬪悗璋冪敤瀹冦
瀵逛簬main()鐨勫弬閲忥紝argc鍜argv鏄被浼肩殑鍙傞噺銆argv鐨勭1涓厓绱犲皢琚拷鐣锛堝吀鍨嬫儏鍐典笅锛屽畠鍖呭惈绋嬪簭鍚嶏級銆備负浜嗘柟渚胯捣瑙侊紝濡傛灉娌℃湁閽堝鏈嶅姟鍣ㄧ殑鍛戒护琛屽弬閲忥紝argc鍙互鏄0銆mysql_server_init()灏嗗鍒跺弬閲忥紝浠ヤ究鑳藉鍦ㄨ皟鐢ㄤ箣鍚庡畨鍏ㄥ湴鎽ф瘉argv鎴groups銆
濡傛灉鎵撶畻杩炴帴鍒板閮ㄦ湇鍔″櫒鑰屼笉鍚姩宓屽叆寮忔湇鍔″櫒锛屽簲涓argc鎸囧畾璐熷笺
鈥groups鈥濅腑浠Null缁堢粨鐨勫瓧绗︿覆鍒楄〃閫夋嫨浜嗛夐」鏂囦欢涓殑娲诲姩鈥groups鈥濄傝鍙傝4.3.2鑺傦紝鈥滀娇鐢ㄩ夐」鏂囦欢鈥銆備负浜嗘柟渚胯捣瑙侊紝groups鍙互鏄NULL锛屽湪璇ユ儏鍐典笅锛[server]鍜[embedded]缁勬槸娲诲姩鐨勩
绀轰緥锛
#include <mysql.h>
#include <stdlib.h>
static char *server_args[] = {
"this_program", /* this string is not used */
"--datadir=.",
"--key_buffer_size=32M"
};
static char *server_groups[] = {
"embedded",
"server",
"this_program_SERVER",
(char *)NULL
};
int main(void) {
if (mysql_server_init(sizeof(server_args) / sizeof(char *),
server_args, server_groups))
exit(1);
/* Use any MySQL API functions here */
mysql_server_end();
return EXIT_SUCCESS;
}
杩斿洖鍊
濡傛灉OK锛岃繑鍥0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖1銆
鎴愬姛璋冪敤mysql_query()鍚庯紝mysql_store_result()鑳藉杩斿洖NULL銆傚嚭鐜拌鎯呭喌鏃讹紝琛ㄦ槑鍑虹幇浜嗕笅杩版潯浠朵箣涓锛
路 鍑虹幇浜malloc()鏁呴殰锛堜緥濡傦紝濡傛灉缁撴灉闆嗚繃澶锛夈
路 鏃犳硶璇诲彇鏁版嵁锛堝湪杩炴帴涓婂嚭鐜颁簡閿欒锛夈
路 鏌ヨ鏈繑鍥炴暟鎹紙渚嬪锛屽畠鏄INSERT銆UPDATE鎴DELETE锛夈
閫氳繃璋冪敤mysql_field_count()锛濮嬬粓鑳芥鏌ヨ鍙ユ槸鍚﹀簲鐢熸垚闈炵┖缁撴灉銆傚鏋mysql_field_count()杩斿洖0锛岀粨鏋滀负绌猴紝鑰屼笖涓婁竴涓煡璇鏄湭杩斿洖鍊肩殑璇彞锛堜緥濡INSERT鎴DELETE锛夈傚鏋mysql_field_count()杩斿洖闈0鍊硷紝璇彞搴旂敓鎴愰潪绌虹粨鏋銆傚叧浜庤繖鏂归潰鐨勭ず渚嬶紝璇峰弬瑙mysql_field_count()鍑芥暟浠嬬粛銆
閫氳繃璋冪敤mysql_error()鎴mysql_errno()锛鍙祴璇曟槸鍚﹀嚭鐜颁簡閿欒銆
闄や簡鏌ヨ杩斿洖鐨勭粨鏋滈泦澶栵紝杩樿兘鑾峰彇涓嬭堪淇℃伅锛
路 鎵цINSERT銆UPDATE鎴DELETE鏃讹紝mysql_affected_rows()杩斿洖涓婃鏌ヨ褰卞搷鐨勮鏁般
瀵逛簬蹇熷湪鍒涘缓锛岃浣跨敤TRUNCATE TABLE銆
路 mysql_num_rows()杩斿洖缁撴灉闆嗕腑鐨勮鏁般備娇鐢mysql_store_result()锛屼竴鏃mysql_store_result()杩斿洖锛屽氨鑳借皟鐢mysql_num_rows()銆備娇鐢mysql_use_result()锛屼粎褰撶敤mysql_fetch_row()鑾峰彇浜嗘墍鏈夎鍚庯紝鎵嶈兘璋冪敤mysql_num_rows()銆
路 mysql_insert_id()杩斿洖涓婃鏌ヨ鐢熸垚鐨ID锛岃鏌ヨ浣跨敤AUTO_INCREMENT绱㈠紩灏嗚鎻掑叆鍒拌〃鍐銆傝鍙傝25.2.3.36鑺傦紝鈥渕ysql_insert_id()鈥銆
路 鏌愪簺鏌ヨ锛LOAD DATA INFILE ...銆INSERT INTO ... SELECT ...銆UPDATE锛夊皢杩斿洖棰濆淇℃伅銆缁撴灉鐢mysql_info()杩斿洖銆傚叧浜庡畠杩斿洖鐨勫瓧绗︿覆鏍煎紡锛璇峰弬瑙佸叧浜mysql_info()鐨勪粙缁嶃傚鏋滄病鏈夐澶栦俊鎭紝mysql_info()灏嗚繑鍥NULL鎸囬拡銆
濡傛灉灏嗚褰曟彃鍏ュ寘鍚AUTO_INCREMENT鍒楃殑琛ㄤ腑锛岄氳繃璋冪敤mysql_insert_id()鍑芥暟锛屽彲鑾峰彇淇濆瓨鍦ㄨ鍒椾腑鐨勫笺
閫氳繃鎵ц涓嬭堪浠g爜锛屽彲浠C搴旂敤绋嬪簭妫鏌ユ煇涓鍊兼槸鍚︿繚瀛樺湪AUTO_INCREMENT鍒椾腑锛堝亣瀹氳璇彞宸叉垚鍔熸墽琛岋級銆傚畠鑳界‘瀹氭煡璇㈡槸鍚︽槸鍏锋湁AUTO_INCREMENT绱㈠紩鐨INSERT锛
if ((result = mysql_store_result(&mysql)) == 0 &&
mysql_field_count(&mysql) == 0 &&
mysql_insert_id(&mysql) != 0)
{
used_id = mysql_insert_id(&mysql);
}
鍏充簬鏇村淇℃伅锛岃鍙傝25.2.3.36鑺傦紝鈥渕ysql_insert_id()鈥銆
鐢熸垚鏂扮殑AUTO_INCREMENT鍊兼椂锛屼篃鑳戒笌mysql_query()涓璧烽氳繃鎵цSELECT LAST_INSERT_ID()璇彞鑾峰緱瀹冿紝骞朵粠璇ヨ鍙ヨ繑鍥炵殑缁撴灉闆嗘绱㈣鍊笺
瀵逛簬LAST_INSERT_ID()锛屾渶杩戠敓鎴愮殑ID鏄湪鏈嶅姟鍣ㄤ笂鎸夎繛鎺ョ淮鎶ょ殑銆傚畠涓嶄細琚彟涓涓鎴风鏀瑰彉銆傚嵆浣跨敤non-magic鍊硷紙鍗抽潪Null闈0鍊硷級鏇存柊浜嗗彟涓涓AUTO_INCREMENT鍒楋紝涔熶笉浼氭洿鏀瑰畠銆
濡傛灉鎵撶畻浣跨敤浠庢煇涓琛ㄧ敓鎴愮殑ID锛屽苟灏嗗叾鎻掑叆鍒扮2涓〃涓紝鍙娇鐢ㄥ涓嬫墍绀虹殑SQL璇彞锛
INSERT INTO foo (auto,text)
VALUES(NULL,'text'); # generate ID by inserting NULL
INSERT INTO foo2 (id,text)
VALUES(LAST_INSERT_ID(),'text'); # use ID in second table
娉ㄦ剰锛mysql_insert_id()杩斿洖淇濆瓨鍦AUTO_INCREMENT鍒椾腑鐨勫硷紝鏃犺璇ュ兼槸鍥犲瓨鍌NULL鎴0鑰岃嚜鍔ㄧ敓鎴愮殑锛屾垨鏄槑纭寚瀹氱殑锛屽潎濡傛銆LAST_INSERT_ID()浠呰繑鍥炶嚜鍔ㄧ敓鎴愮殑AUTO_INCREMENT鍊笺濡傛灉浣犱繚瀛樹簡闄NULL鎴0涔嬪鐨勭‘鍒囧硷紝涓嶄細褰卞搷LAST_INSERT_ID()杩斿洖鐨勫笺
gcc -g -o client test.o -L/usr/local/lib/mysql -lmysqlclient -lsocket -lnsl
Undefined first referenced
symbol in file
floor /usr/local/lib/mysql/libmysqlclient.a(password.o)
ld: fatal: Symbol referencing errors. No output written to client
濡傛灉鍦ㄤ綘鐨勭郴缁熶笂鍑虹幇浜嗚鎯呭喌锛屽繀椤诲湪缂栬瘧/閾炬帴琛岀殑鏈熬澧炲姞鈥-lm鈥濓紝閫氳繃璇ユ柟寮忓寘鍚暟瀛﹀簱銆
濡傛灉浣犵紪璇戜簡鑷繁缂栧啓鐨MySQL瀹㈡埛绔紝鎴栫紪璇戜簡浠庣涓夋柟鑾峰彇鐨MySQL瀹㈡埛绔紝蹇呴』鍦ㄩ摼鎺ュ懡浠や腑浣跨敤鈥-lmysqlclient -lz鈥濋夐」閾炬帴瀹冧滑銆備綘鎴栬杩樺簲鎸囧畾鈥-L鈥閫夐」锛岄氱煡閾炬帴绋嬪簭鍒板摢閲屾壘鍒板簱銆備緥濡傦紝濡傛灉灏嗗簱瀹夎鍒颁簡/usr/local/mysql/lib锛屽彲鍦ㄩ摼鎺ュ懡浠や腑浣跨敤sr/local/mysql/lib -lmysqlclient 鈥搇z銆
瀵逛簬浣跨敤MySQL澶存枃浠剁殑瀹㈡埛绔紝缂栬瘧瀹冧滑鏃惰繕椤绘寚瀹氣-I鈥閫夐」锛堜緥濡锛-I/usr/local/mysql/include锛夛紝浠ヤ究缂栬瘧鍣ㄨ兘鎵惧埌澶存枃浠躲
涓轰簡浣垮湪Unix骞冲彴涓婄紪璇MySQL绋嬪簭鍙樺緱绠鍗曪紝鎻愪緵浜mysql_config鑴氭湰銆傝鍙傝25.9.2鑺傦紝鈥渕ysql_config锛氳幏鍙栫紪璇戝鎴风鐨勭紪璇戦夐」鈥銆
浣犱篃鍙互浣跨敤瀹冩潵缂栬瘧MySQL瀹㈡埛绔紝濡備笅鎵杩帮細
CFG=/usr/local/mysql/bin/mysql_config
sh -c "gcc -o progname `$CFG --cflags` progname.c `$CFG --libs`"
闇瑕佷娇鐢ㄢsh 鈥揷鈥锛屼娇寰shell涓嶅皢mysql_config鐨勮緭鍑哄綋浣1涓瘝瀵瑰緟銆
瀹㈡埛绔簱鎬绘槸绾跨▼瀹夊叏鐨勩傛渶澶х殑闂鍦ㄤ簬浠庡鎺ュ瓧璇诲彇鐨net.c涓殑瀛愮▼搴忓苟涓嶆槸涓柇瀹夊叏鐨勩傛垨璁镐綘鍙兘甯屾湜鐢ㄨ嚜宸辩殑鍛婅涓柇瀵规湇鍔″櫒鐨勯暱鏃堕棿璇诲彇锛屼互姝ゆ潵瑙e喅闂銆傚鏋滀负SIGPIPE涓柇瀹夎浜嗕腑鏂鐞嗙▼搴忥紝濂楁帴瀛楀鐞嗗姛鑳藉簲鏄嚎绋嬪畨鍏ㄧ殑銆
涓轰簡閬垮厤杩炴帴涓柇鏃舵斁寮冪▼搴忥紝MySQL灏嗗湪棣栨璋冪敤mysql_server_init()銆mysql_init()鎴mysql_connect()鏃跺睆钄SIGPIPE銆傚鏋滀綘鎵撶畻浣跨敤鑷繁鐨SIGPIPE澶勭悊绋嬪簭锛棣栧厛搴旇皟鐢mysql_server_init()锛岀劧鍚庡畨瑁呬綘鐨勫鐞嗙▼搴忥紝
鍦ㄨ緝鏃х殑鍙戝竷鍦ㄦ垜鏂圭綉绔欎笂锛http://www.mysql.com/锛夌殑浜岃繘鍒剁増鏈腑锛屾湭鐢ㄧ嚎绋嬪畨鍏ㄧ殑閫夐」瀵瑰鎴风搴撹繘琛屾甯哥紪璇戯紙榛樿鎯呭喌涓嬶紝缂栬瘧鐨Windows浜岃繘鍒剁増鏈槸绾跨▼瀹夊叏鐨勶級銆傝緝鏂扮殑浜岃繘鍒跺垎鍙戠増搴旀槸姝e父鐨勫拰绾跨▼瀹夊叏鐨勫鎴风搴撱
涓轰簡鑾峰緱鑳戒粠鍏朵粬绾跨▼涓柇瀹㈡埛绔殑绾跨▼寮忓鎴风锛屽苟鍦ㄤ笌MySQL鏈嶅姟鍣ㄩ氫俊鏃惰缃秴鏃讹紝搴斾娇鐢ㄢ-lmysys鈥濄佲-lmystrings鈥鍜鈥-ldbug鈥搴擄紝浠ュ強鏈嶅姟鍣ㄤ娇鐢ㄧ殑net_serv.o浠g爜銆
濡傛灉浣犱笉闇瑕佷娇鐢ㄤ腑鏂垨瓒呮椂锛屽彲缂栬瘧绾跨▼瀹夊叏瀹㈡埛绔簱锛mysqlclient_r锛夊苟浣跨敤瀹冦傘傝鍙傝25.2鑺傦紝鈥淢ySQL C API鈥銆傚湪璇ユ儏鍐典笅锛屼笉蹇呮媴蹇net_serv.o瀵硅薄鏂囦欢鎴栧叾浠MySQL搴撱
浣跨敤绾跨▼寮忓鎴风骞舵墦绠椾娇鐢ㄨ秴鏃舵垨涓柇鏃讹紝鍙洿濂藉湴鍒╃敤thr_alarm.c鏂囦欢涓殑瀛愮▼搴忋傚鏋滀綘姝e湪浣跨敤鏉ヨ嚜mysys搴撶殑瀛愮▼搴忥紝鍞竴闇瑕佽浣忕殑浜嬫槸棣栧厛璋冪敤my_init()锛佽鍙傝25.2.11鑺傦紝鈥淐 API绾跨▼鍑芥暟浠嬬粛鈥銆
瀵逛簬闄mysql_real_connect()澶栫殑鎵鏈夊嚱鏁帮紝鍦ㄩ粯璁ゆ儏鍐典笅瀹冧滑鍧囨槸绾跨▼瀹夊叏鐨勩傚湪涓嬮潰鐨勮鏄庝腑锛屼粙缁嶄簡缂栬瘧绾跨▼瀹夊叏瀹㈡埛绔簱鐨勬柟娉曪紝浠ュ強浠ョ嚎绋嬪畨鍏ㄦ柟寮忎娇鐢ㄥ畠鐨勬柟娉曘傦紙涓嬮潰鍏充簬mysql_real_connect()鐨勮鏄庡疄闄呬笂涔熼傜敤浜mysql_connect()锛屼絾鐢变簬mysql_connect()宸蹭笉鍐嶈閲嶈锛鎬诲簲灏介噺浣跨敤mysql_real_connect()锛夈
瑕佹兂浣mysql_real_connect()鎴愪负绾跨▼瀹夊叏鐨勶紝蹇呴』鐢ㄤ笅杩板懡浠ゅ啀娆$紪璇瀹㈡埛绔簱锛
shell> ./configure --enable-thread-safe-client
瀹冨垱寤轰簡绾跨▼瀹夊叏瀹㈡埛绔簱libmysqlclient_r銆傦紙鍋囧畾浣犵殑鎿嶄綔绯荤粺鏈夌嚎绋嬪畨鍏ㄧ殑gethostbyname_r()鍑芥暟锛夈傛寜鐓ц繛鎺ワ紝璇ュ簱鏄嚎绋嬪畨鍏ㄧ殑銆傚彲閬靛惊涓嬭堪璀﹀憡锛屼娇涓や釜绾跨▼鍏变韩鐩稿悓鐨勮繛鎺ワ細
路 鍦ㄧ浉鍚岀殑杩炴帴涓婏紝涓や釜绾跨▼涓嶈兘鍚屾椂灏嗘煡璇㈠彂閫佸埌MySQL鏈嶅姟鍣ㄣ傚挨鍏舵槸锛屽繀椤荤‘淇濆湪mysql_query()鍜mysql_store_result()涔嬮棿锛屾病鏈変娇鐢ㄧ浉鍚岃繛鎺ョ殑鍏朵粬绾跨▼銆
路 寰堝绾跨▼鍧囪兘璁块棶鐢mysql_store_result()妫绱㈢殑涓嶅悓缁撴灉闆嗐
路 濡傛灉浣跨敤浜mysql_use_result锛屽姟蹇呯‘淇濇棤鍏朵粬绾跨▼姝e湪浣跨敤鐩稿悓鐨勮繛鎺ワ紝鐩磋嚦鍏抽棴浜嗙粨鏋滈泦涓烘銆傜劧鑰岋紝瀵逛簬绾跨▼寮忓鎴风锛屾渶濂芥槸鍏变韩鐩稿悓鐨勮繛鎺ヤ互浣跨敤mysql_store_result()銆
路 濡傛灉鎵撶畻鍦ㄧ浉鍚岀殑杩炴帴涓婁娇鐢ㄥ涓嚎绋嬶紝蹇呴』鍦mysql_query()鍜mysql_store_result()璋冪敤缁勫悎涓婃嫢鏈変簰鏂ラ攣銆涓鏃mysql_store_result()鍑嗗灏辩华锛屽彲閲婃斁閿佸畾锛屽叾浠栫嚎绋嬪彲鍦ㄧ浉鍚岀殑杩炴帴涓婃墽琛屾煡璇€
路 濡傛灉浣跨敤POSIX绾跨▼杩涜缂栫▼锛屽彲浣跨敤pthread_mutex_lock()鍜pthread_mutex_unlock()鏉ュ缓绔嬪苟閲婃斁浜掓枼閿併
濡傛灉浣犳湁1涓皟鐢MySQL鍑芥暟鐨勭嚎绋嬶紝鑰岃鍑芥暟鏈垱寤轰笌MySQL鏁版嵁搴撶殑杩炴帴锛屽氨闇浜嗚В涓嬭堪浜嬪疁锛
璋冪敤mysql_init()鎴mysql_connect()鏃讹紝MySQL浼氫负璋冭瘯搴撲娇鐢ㄧ殑绾跨▼鍒涘缓涓庣嚎绋嬬浉鍏崇殑鍙橀噺锛堝挨鍏讹級銆
鍦ㄧ嚎绋嬭皟鐢mysql_init()鎴mysql_connect()涔嬪墠锛濡傛灉璋冪敤浜MySQL鍑芥暟锛岃绾跨▼灏嗘病鏈夋墍闇鐨勭嚎绋嬬被鍙橀噺锛岃屼笖浣犲緢鍙兘鎴栨棭鎴栨櫄缁撴潫浜庡唴鏍歌浆鍌ㄣ
瑕佹兂浣胯繖浜涙搷浣滃钩绋冲伐浣滐紝闇瑕侀噰鍙栦笅杩版帾鏂斤細
1. 濡傛灉绋嬪簭鍦ㄨ皟鐢mysql_real_connect()涔嬪墠闇瑕佽皟鐢ㄤ换浣曞叾浠MySQL鍑芥暟锛岃鍦ㄥ惎鍔ㄧ▼搴忔椂璋冪敤my_init()銆
2. 璋冪敤浠讳綍MySQL鍑芥暟涔嬪墠锛屽湪绾跨▼澶勭悊绋嬪簭涓皟鐢mysql_thread_init()銆
3. 鍦ㄧ嚎绋嬩腑锛岃皟鐢pthread_exit()涔嬪墠璇疯皟鐢mysql_thread_end()銆傝繖鏍凤紝灏辫兘閲婃斁MySQL绾跨▼绫诲彉閲忎娇鐢ㄧ殑鍐呭瓨銆
灏嗗鎴风閾炬帴鍒libmysqlclient_r鏃讹紝濡傛灉瀛樺湪鏈畾涔夌殑绗﹀彿锛屽彲鑳戒細鍑洪敊銆傚湪澶у鏁版儏鍐典笅锛屽叾鍘熷洜鍦ㄤ簬锛屾湭灏嗙嚎绋嬪簱鍖呭惈鍦link/compile琛屼笂銆
PHP鏄竴绉嶆湇鍔″櫒绔HTML宓屽叆寮忚剼鏈鐞嗚瑷锛屽彲浣跨敤璇ヨ瑷鍒涘缓鍔ㄦ佺綉椤点傚畠鍙敤浜庡ぇ澶氭暟鎿嶄綔绯荤粺鍜Web鏈嶅姟鍣紝涔熻兘璁块棶澶у鏁板父瑙佹暟鎹簱锛屽寘鎷MySQL銆PHP鍙互浣滀负鍗曠嫭绋嬪簭杩愯锛屼篃鑳界紪璇戜负妯″潡锛岀敤浜Apache Web鏈嶅姟鍣ㄣ
PHP瀹為檯涓婃彁渚涗簡涓ょ涓嶅悓鐨MySQL API鎵╁睍锛
路 mysql锛氶傜敤浜PHP鐗堟湰4鍜5锛岃鎵╁睍鐢ㄤ簬MySQL 4.1涔嬪墠鐨MySQL鐗堟湰銆傝鎵╁睍涓嶆敮鎸MySQL 5.1涓噰鐢ㄧ殑銆佹敼杩涚殑閴村畾鍗忚锛屼篃涓嶆敮鎸佷笌棰勫鐞嗚鍙ユ垨澶氳鍙ャ傚鏋滄墦绠椾笌MySQL 5.1涓璧蜂娇鐢ㄨ鎵╁睍锛屽簲閰嶇疆MySQL鏈嶅姟鍣紝浠ヤ娇鐢ㄢ--old-passwords鈥濋夐」锛堣鍙傝A.2.3鑺傦紝鈥滃鎴风涓嶆敮鎸侀壌瀹氬崗璁锛夈傚湪PHP缃戠珯鐨勬枃妗d腑璁板綍浜嗚璇ユ墿灞http://php.net/mysql銆
路 mysqli鏄MySQL, Improved鈥濈殑缂╁啓锛岃鎵╁睍浠呴傜敤浜PHP 5銆傚畠鑳界敤浜MySQL 4.1.1鍜屾洿楂樼増鏈傝鎵╁睍瀹屽叏鏀寔MySQL 5.1涓噰鐢ㄧ殑閴村畾鍗忚锛屼篃鏀寔棰勫鐞嗚鍙ュ拰澶氳鍙API銆傛澶栵紝璇ユ墿灞曡繕鎻愪緵浜嗗厛杩涚殑銆侀潰鍚戝璞$殑缂栫▼鎺ュ彛銆傚湪http://php.net/mysqli涓婏紝鍙壘鍒板叧浜mysqli鎵╁睍鐨勬枃妗c傚湪http://www.zend.com/php5/articles/php5-mysqli.php澶勶紝缁欏嚭浜嗕竴绡囨湁鐢ㄧ殑鏂囩珷銆
PHP鍒嗗彂鐗堝拰鏂囨。鍧囪兘浠PHP缃戠珯鑾峰緱銆
路 閿欒锛氳秴鍑轰簡鏈澶ф墽琛屾椂闂达紝杩欐槸涓绉PHP闄愬埗锛屽鏋滈渶瑕侊紝杩涘叆鏂囦欢php.ini锛屽苟璁剧疆鏈澶ф墽琛屾椂闂达紙寮濮嬩负30绉掞級銆傛澶栵紝杩樺彲浠ュ皢姣忚剼鏈厑璁镐娇鐢ㄧ殑RAM澧炲姞涓鍊嶏紝浠8MB鍙樹负16MB锛岃繖涔熸槸涓笉閿欑殑涓绘剰銆
路 鑷村懡閿欒锛氬湪鈥涓皟鐢ㄤ簡涓嶆敮鎸佹垨鏈畾涔夌殑mysql_connect()鍑芥暟锛岃繖鎰忓懗鐫锛屼綘鐨PHP鐗堟湰涓嶆敮鎸MySQL銆備綘鍙互缂栬瘧鍔ㄦMySQL妯″潡骞跺皢鍏跺姞杞藉埌PHP锛屾垨浣跨敤鍐呯疆鐨MySQL鏀寔閲嶆柊缂栬瘧PHP銆傚湪PHP鎵嬪唽涓紝璇︾粏浠嬬粛浜嗚杩涚▼銆
路 閿欒锛氬'uncompress'鐨勬湭瀹氫箟寮曠敤锛杩欐剰鍛崇潃鎵缂栬瘧鐨勫鎴风搴撴敮鎸佸帇缂╁鎴风锛忔湇鍔″櫒鍗忚銆傛洿姝f柟娉曟槸锛岀敤鈥-lmysqlclient鈥濊繘琛岄摼鎺ユ椂锛屽湪鏈鍚庢坊鍔犫-lz鈥銆
路 閿欒锛氬鎴风涓嶆敮鎸侀壌瀹氬崗璁紝涓MySQL 4.1.1鍜屾洿楂樼増鏈竴璧蜂娇鐢ㄨ緝鏃х殑mysql鎵╁睍鏃跺父浼氶亣鍒拌闂銆傚彲鑳界殑瑙e喅鏂规鏄細闄嶇骇鍒MySQL 4.0锛岃浆鍚PHP 5鍜岃緝鏂扮殑mysqli鎵╁睍锛屾垨鐢鈥--old-passwords鈥閰嶇疆MySQL鏈嶅姟鍣锛堟洿澶氫俊鎭紝璇峰弬瑙A.2.3鑺傦紝鈥滃鎴风涓嶆敮鎸侀壌瀹氬崗璁锛夈
Perl DBI妯″潡涓烘暟鎹簱璁块棶鎻愪緵浜嗕竴涓氱敤鎺ュ彛銆鑳藉缂栧啓鏃犻渶鏇存敼灏辫兘涓庝笉鍚岀殑鏁版嵁搴撳紩鎿庝竴璧峰伐浣滅殑DBI鑴氭湰銆傝鎯充娇鐢DBI锛屽繀椤诲畨瑁DBI妯″潡锛屽苟涓烘墦绠楄闂殑姣忕鏈嶅姟鍣ㄥ畨瑁呮暟鎹簱椹卞姩绋嬪簭锛DBD锛夋ā鍧椼傚浜MySQL锛岃椹卞姩绋嬪簭鏄DBD::mysql妯″潡銆
Perl DBI鏄帹鑽愮殑Perl鎺ュ彛銆傚畠鍙栦唬浜嗘棫鐨勫悕涓mysqlperl鐨勬帴鍙o紝mysqlperl宸茶繃鏃躲
鍏充簬Perl DBI鏀寔鐨勫畨瑁呰鏄庯紝璇峰弬瑙2.13鑺傦紝鈥淧erl瀹夎娉ㄦ剰浜嬮」鈥銆
DBI淇℃伅鑳藉鍦ㄥ懡浠よ涓婃彁渚涳紝涔熻兘浠ュ湪绾挎柟寮忔彁渚涳紝鎴栭噰鐢ㄥ嵃鍒峰舰寮忥細
路 涓鏃﹀畨瑁呬簡DBI鍜DBD::mysql妯″潡锛屽彲浣跨敤perldoc 鍛戒护鍦ㄥ懡浠よ涓婅幏鍙栧叧浜庡畠浠殑淇℃伅锛
路 shell> perldoc DBI
路 shell> perldoc DBI::FAQ
路 shell> perldoc DBD::mysql
涔熷彲浠ヤ娇鐢pod2man銆pod2html绛灏嗚繖绫讳俊鎭浆鎹负鍏朵粬鏍煎紡銆
路 鍏充簬Perl DBI鐨勫湪绾夸俊鎭紝璇疯闂DBI缃戠珯锛http://dbi.perl.org/銆傝绔欑偣杩樻彁渚涗簡1涓竴鑸DBI閭欢鍒楄〃銆MySQL AB鎻愪緵浜1涓笓闂ㄩ拡瀵DBD::mysql 鐨勯偖浠跺垪琛紝璇峰弬瑙1.7.1.1 鈥MySQL閭欢鍒楄〃鈥銆
路 鑷充簬鍗板埛鐗堜俊鎭紝瀹樻柟鐨DBI涔︾睄鏄缂栫▼Perl DBI锛Alligator Descartes鍜Tim Bunce锛O'Reilly & Associates, 2000锛夈傚叧浜庤涔︾殑淇℃伅锛岃璁块棶DBI缃戠珯http://dbi.perl.org/銆
鍏充簬涓MySQL涓璧蜂娇鐢DBI鐨勪笓闂ㄤ俊鎭紝璇峰弬瑙閽堝Web鐨MySQL鍜Perl锛Paul DuBois, New Riders, 2001锛夈傝涔︾殑缃戠珯鏄http://www.kitebird.com/mysql-perl/銆
MySQL++鏄敤浜C++鐨MySQL API銆Warren Young璐熻矗璇ラ」鐩傝鎯充簡瑙f洿澶氫俊鎭紝璇疯闂http://www.mysql.com/products/mysql++/銆
浣跨敤Borland C++鏃剁殑1涓凡鐭ラ棶棰樻槸锛屽畠閲囩敤浜嗕笉鍚屼簬VC++鐨勭粨鏋勫榻愭柟寮忋傝繖鎰忓懗鐫锛屽鏋滀綘鎵撶畻涓Borland C++涓璧蜂娇鐢ㄩ粯璁ょ殑libmysql.dll搴擄紙瀹冩槸浣跨敤VC++缂栬瘧鐨勶級锛屽皢浼氶亣鍒伴棶棰樸備负浜嗛伩鍏嶈闂锛屼粎搴旇皟鐢ㄥ皢Null浣滀负鍙傞噺鐨mysql_init()锛岃屼笉鏄鍏堝垎閰MYSQL缁撴瀯銆
Eiffel MySQL鏄竴绉嶄笌MySQL鏁版嵁搴撴湇鍔″櫒鐨勬帴鍙o紝瀹冮噰鐢ㄧ殑鏄Eiffel缂栫▼璇█锛岀敱Michael Ravits缂栧啓銆傚彲鍦http://efsa.sourceforge.net/archive/ravits/mysql.htm涓婃壘鍒板畠銆
鏈鍒濓紝寮鍙戠殑MySQL C API寰堢被浼间负mSQL鏁版嵁搴撶郴缁熷紑鍙戠殑API銆傛鍥犱负濡傛锛岄氳繃鏇存敼C API鍑芥暟鐨勫悕绉帮紝閫氬父鑳界浉瀵瑰鏄撳湴杞崲mSQL绋嬪簭锛屼娇涔嬬敤浜MySQL銆
msql2mysql瀹炵敤宸ュ叿鐢ㄤ簬灏mSQL C API鍑芥暟璋冪敤杞崲涓哄叾MySQL瀵圭瓑鐗┿msql2mysql鑳藉杞崲浣嶄簬鎭板綋浣嶇疆杈撳叆鏂囦欢锛屽湪鎵ц鍏蜂綋杞崲涔嬪墠澶嶅埗鍘熶欢銆備緥濡傦紝鍙噰鐢ㄤ笅杩版柟寮忎娇鐢msql2mysql锛
shell> cp client-prog.c client-prog.c.orig
shell> msql2mysql client-prog.c
client-prog.c converted
鐒跺悗锛屾鏌client-prog.c锛屽苟鎵ц鍙兘闇瑕佺殑鍚庢湡杞崲淇銆
msql2mysql浣跨敤replace瀹炵敤宸ュ叿鏉ユ浛鎹㈠嚱鏁板悕銆傝鍙傝8.14鑺傦紝鈥渞eplace锛氬瓧绗︿覆鏇挎崲瀹炵敤宸ュ叿鈥銆
mysql_config鎻愪緵浜嗗叧浜庣紪璇MySQL瀹㈡埛绔互鍙婂皢鍏惰繛鎺ュ埌MySQL鐨勬湁鐢ㄤ俊鎭
mysql_config鏀寔涓嬭堪閫夐」锛
路 --cflags
缂栬瘧鍣ㄦ爣蹇楋紝鐢ㄤ簬鏌ユ壘鍖呭惈鏂囦欢锛屼互鍙婄紪璇libmysqlclient搴撴椂鎵瑕佷娇鐢ㄧ殑鍏抽敭缂栬瘧鍣ㄦ爣蹇楀拰瀹氫箟銆
路 --include
缂栬瘧鍣ㄩ夐」锛岀敤浜庢煡鎵MySQL鍖呭惈鏂囦欢锛堟敞鎰忥紝姝e父鎯呭喌涓嬪簲浣跨敤鈥鈥攃flags鈥鑰屼笉鏄閫夐」锛夈
路 --libmysqld-libs, ---embedded
涓MySQL宓屽叆寮忔湇鍔″櫒杩涜閾炬帴鎵闇鐨勫簱鍜岄夐」銆
路 --libs
涓MySQL瀹㈡埛绔簱杩涜閾炬帴鎵闇鐨勫簱鍜岄夐」銆
路 --libs_r
涓庣嚎绋嬪畨鍏MySQL瀹㈡埛绔簱杩涜閾炬帴鎵闇鐨勫簱鍜岄夐」銆
路 --port
榛樿鐨TCP/IP绔彛鍙凤紝閰嶇疆MySQL鏃跺畾涔夈
路 --socket
榛樿鐨Unix濂楁帴瀛楁枃浠讹紝閰嶇疆MySQL鏃跺畾涔夈
路 --version
鐗堟湰鍙蜂互鍙MySQL鍒嗗彂鐗堢殑鐗堟湰銆
濡傛灉鏈娇鐢ㄤ换浣曢夐」璋冪敤浜mysql_config锛屽皢鏄剧ず瀹冩墍鏀寔鐨勬墍鏈夐夐」鐨勫垪琛紝浠ュ強瀹冧滑鐨勫硷細
shell> mysql_config
Usage: /usr/local/mysql/bin/mysql_config [options]
Options:
--cflags [-I/usr/local/mysql/include/mysql -mcpu=pentiumpro]
--include [-I/usr/local/mysql/include/mysql]
--libs [-L/usr/local/mysql/lib/mysql -lmysqlclient -lz
-lcrypt -lnsl -lm -L/usr/lib -lssl -lcrypto]
--libs_r [-L/usr/local/mysql/lib/mysql -lmysqlclient_r
-lpthread -lz -lcrypt -lnsl -lm -lpthread]
--socket [/tmp/mysql.sock]
--port [3306]
--version [4.0.16]
--libmysqld-libs [-L/usr/local/mysql/lib/mysql -lmysqld -lpthread -lz
-lcrypt -lnsl -lm -lpthread -lrt]
鍙互鍦ㄥ懡浠よ涓婁娇鐢mysql_config锛屽苟鍖呭惈閽堝鐗瑰畾閫夐」鐨勫笺備緥濡傦紝瑕佹兂缂栬瘧MySQL瀹㈡埛绔▼搴忥紝鍙娇鐢mysql_config锛屽涓嬩緥鎵绀猴細
shell> CFG=/usr/local/mysql/bin/mysql_config
shell> sh -c "gcc -o progname `$CFG --cflags` progname.c `$CFG --libs`"
浠ヨ繖绉嶆柟寮忎娇鐢mysql_config鏃讹紝鍔″繀鍦ㄥ瓧绗(鈥`鈥)鍐呰皟鐢ㄥ畠銆傝繖鏍凤紝灏辫兘閫氱煡shell鎵ц瀹冿紝骞跺皢鍏惰緭鍑轰唬鍏ュ埌鐜鍛戒护涓
杩欐槸MySQL鍙傝冩墜鍐岀殑缈昏瘧鐗堟湰锛屽叧浜嶮ySQL鍙傝冩墜鍐岋紝璇疯闂dev.mysql.com銆 鍘熷鍙傝冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆