站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > MySQL 5.1参考手册中文版

Chapter聽25.聽APIs and Libraries - MySQL 5.1参考手册中文版

绗25绔狅細API鍜屽簱

鐩綍

25.1. libmysqld锛屽祵鍏ュ紡MySQL鏈嶅姟鍣ㄥ簱
25.1.1. 宓屽叆寮廙ySQL鏈嶅姟鍣ㄥ簱姒傝堪
25.1.2. 浣跨敤libmysqld缂栬瘧绋嬪簭
25.1.3. 浣跨敤宓屽叆寮廙ySQL鏈嶅姟鍣ㄦ椂鐨勯檺鍒
25.1.4. 涓庡祵鍏ュ紡鏈嶅姟鍣ㄤ竴璧蜂娇鐢ㄧ殑閫夐」
25.1.5. 宓屽叆寮忔湇鍔″櫒涓皻闇瀹屾垚鐨勪簨椤(TODO)
25.1.6. 宓屽叆寮忔湇鍔″櫒绀轰緥
25.1.7. 宓屽叆寮忔湇鍔″櫒鐨勮鍙
25.2. MySQL C API
25.2.1. C API鏁版嵁绫诲瀷
25.2.2. C API鍑芥暟姒傝堪
25.2.3. C API鍑芥暟鎻忚堪
25.2.4. C API棰勫鐞嗚鍙
25.2.5. C API棰勫鐞嗚鍙ョ殑鏁版嵁绫诲瀷
25.2.6. C API棰勫鐞嗚鍙ュ嚱鏁版杩
25.2.7. C API棰勫鐞嗚鍙ュ嚱鏁版弿杩
25.2.8. C API棰勫鐞嗚鍙ユ柟闈㈢殑闂
25.2.9. 澶氭煡璇㈡墽琛岀殑C API澶勭悊
25.2.10. 鏃ユ湡鍜屾椂闂村肩殑C API澶勭悊
25.2.11. C API绾跨▼鍑芥暟浠嬬粛
25.2.12. C API宓屽叆寮忔湇鍔″櫒鍑芥暟浠嬬粛
25.2.13. 浣跨敤C API鏃剁殑甯歌闂
25.2.14. 鍒涘缓瀹㈡埛绔▼搴
25.2.15. 濡備綍鐢熸垚绾跨▼寮忓鎴风
25.3. MySQL PHP API
25.3.1. 浣跨敤MySQL鍜孭HP鐨勫父瑙侀棶棰
25.4. MySQL Perl API
25.5. MySQL C++ API
25.5.1. Borland C++
25.6. MySQL Python API
25.7. MySQL Tcl API
25.8. MySQL Eiffel Wrapper
25.9. MySQL绋嬪簭寮鍙戝疄鐢ㄥ伐鍏
25.9.1. msql2mysql锛氳浆鎹SQL绋嬪簭浠ョ敤浜嶮ySQL
25.9.2. mysql_config锛氳幏鍙栫紪璇戝鎴风鐨勭紪璇戦夐」

鏈珷浠嬬粛浜哅ySQL鍙娇鐢ㄧ殑API锛屼粠鍝噷鑾峰緱瀹冧滑锛屼互鍙婂浣曚娇鐢ㄥ畠浠 璇︾粏浠嬬粛C API锛岃繖鏄洜涓哄畠鏄敱MySQL鍥㈤槦寮鍙戠殑锛岃屼笖瀹冧篃鏄ぇ澶氭暟鍏朵粬API鐨勫熀纭銆 鏈珷杩樹粙缁嶄簡libmysqld搴擄紙宓屽叆寮忔湇鍔″櫒锛夛紝浠ュ強瀵瑰簲鐢ㄧ▼搴忓紑鍙戜汉鍛樻湁鐢ㄧ殑涓浜涚▼搴忋

25.1. libmysqld锛屽祵鍏ュ紡MySQL鏈嶅姟鍣ㄥ簱

25.1.1.聽宓屽叆寮廙ySQL鏈嶅姟鍣ㄥ簱姒傝堪

浣跨敤宓屽叆寮廙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鈥濋夐」銆

25.1.2. 浣跨敤libmysqld缂栬瘧绋嬪簭

瑕佹兂鑾峰緱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涓紝鑰屼笉鏄崟寮曞彿鍐呫

25.1.3. 浣跨敤宓屽叆寮廙ySQL鏈嶅姟鍣ㄦ椂鐨勯檺鍒

宓屽叆寮忔湇鍔″櫒瀛樺湪涓嬭堪闄愬埗锛

         涓嶆敮鎸ISAM琛ㄣ 锛堣繖鏍峰仛鐨勪富瑕佺洰鐨勬槸涓轰簡浣垮簱鏇村皬锛夈

         娌℃湁鑷畾涔夊嚱鏁帮紙UDF锛夈

         娌℃湁瀵规牳蹇冭浆鍌ㄧ殑鍫嗘爤璺熻釜銆

         娌℃湁鍐呴儴RAID鏀寔銆 锛堢敱浜庡ぇ澶氭暟褰撳墠鎿嶄綔绯荤粺鍧囨敮鎸佸ぇ鏂囦欢锛岄氬父鎯呭喌涓嬩笉闇瑕佸畠锛夈

         涓嶈兘灏嗗叾璁剧疆涓衡滀富鈥濇垨鈥滀粠鈥濓紙鏃犲鍒讹級銆

         鍦ㄥ唴瀛樿緝浣庣殑绯荤粺涓婏紝鍙兘鏃犳硶浣跨敤寰堝ぇ鐨勭粨鏋滈泦銆

         涓嶈兘浣跨敤濂楁帴瀛楁垨TCP/IP浠庡閮ㄨ繘绋嬭繛鎺ュ埌宓屽叆寮忔湇鍔″櫒銆 浣嗘槸锛屼綘鍙互杩炴帴鍒颁腑闂村簲鐢ㄧ▼搴忥紝闅忓悗锛岃涓棿搴旂敤绋嬪簭鍙唬琛ㄨ繙绋嬪鎴风鎴栧閮ㄨ繘绋嬭繛鎺ュ埌宓屽叆寮忔湇鍔″櫒銆

閫氳繃缂栬緫鈥mysql_embed.h鈥濆寘鍚枃浠跺苟閲嶆柊缂栬瘧MySQL锛屽彲鏇存敼鏌愪簺闄愬埗銆

25.1.4.聽涓庡祵鍏ュ紡鏈嶅姟鍣ㄤ竴璧蜂娇鐢ㄧ殑閫夐」

瀵逛簬浠讳綍鑳藉涓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鑺傦紝鈥滀娇鐢ㄩ夐」鏂囦欢鈥

25.1.5. 宓屽叆寮忔湇鍔″櫒涓皻闇瀹屾垚鐨勪簨椤(TODO)

         鎴戜滑灏嗘彁渚涗竴浜涢夐」浠ョ渷鍘MySQL鐨勬煇浜涢儴鍒嗭紝浠庤屼娇搴撳彉寰楁洿灏忋

         浠嶆湁寰堝閫熷害浼樺寲宸ヤ綔闇瑕佸畬鎴愩

         閿欒灏嗚鍐欏叆stderr銆傛垜浠皢澧炲姞1涓夐」涓哄畠浠寚瀹氭枃浠跺悕銆

         浣跨敤宓屽叆寮忕増鏈椂锛岄渶瑕佹洿鏀InnoDB锛屼娇涔嬩笉鍐嶅啑闀裤傚鏋滀綘鐨勬暟鎹簱涓嶅惈InnoDB琛紝瑕佹兂鎶戝埗鐩稿叧娑堟伅锛屽彲涓虹粍[libmysqd_server]涓嬬殑閫夐」鏂囦欢澧炲姞--skip-innodb鈥閫夐」锛屾垨鍦ㄧ敤mysql_server_init()鍒濆鍖栨湇鍔″櫒鏃舵坊鍔犺閫夐」銆

25.1.6. 宓屽叆寮忔湇鍔″櫒绀轰緥

LinuxFreeBSD绯荤粺涓婏紝鏃犻渶鏇存敼灏辫兘浣跨敤涓嬮潰杩欎袱涓ず渚嬬▼搴忋傚浜庡叾浠栨搷浣滅郴缁燂紝闇瑕佽繘琛屽皬鐨勪慨鏀癸紝涓昏鏄枃浠惰矾寰勩傝璁¤繖涓や釜绀轰緥鐨勭洰鐨勫湪浜庯紝涓轰綘鎻愪緵瓒冲鐨勭粏鑺備俊鎭紝浠ヤ究鐞嗚В闂锛屽畠浠槸瀹為檯搴旂敤绋嬪簭鐨勫繀瑕佺粍鎴愰儴浠姐傜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

25.1.7. 宓屽叆寮忔湇鍔″櫒鐨勮鍙

鎴戜滑榧撳姳鎵鏈変汉鍦GPL鎴栧吋瀹硅鍙殑鏃楀笢涓嬮氳繃鍙戝竷浠g爜鏉ユ帹骞垮厤璐硅蒋浠躲傚浜庢湁鑳藉姏瀹屾垚璇ョ被浜嬮」鐨勪汉鍛橈紝涔熷彲浠ラ夋嫨浠MySQL AB璐拱MySQL鐨勫晢鐢ㄨ鍙傝鎯呰鍙傝http://www.mysql.com/company/legal/licensing/

25.2.聽MySQL C API

C API浠g爜鏄笌MySQL涓璧锋彁渚涚殑銆傚畠鍖呭惈鍦mysqlclient搴撲腑锛屽苟鍏佽C绋嬪簭璁块棶鏁版嵁搴撱

MySQL婧愮爜鍒嗗彂鐗堢殑寰堝瀹㈡埛绔槸鐢C璇█缂栧啓鐨勩傚鏋滀綘姝e湪瀵绘壘鑳芥紨绀哄浣曚娇鐢C API鐨勭ず渚嬶紝鍙弬鐪嬭繖浜涘鎴风绋嬪簭銆備綘鍙互鍦MySQL婧愮爜鍒嗗彂鐗堢殑瀹㈡埛绔洰褰曚笅鎵惧埌瀹冧滑銆

澶у鏁板叾浠栧鎴风API锛堥櫎浜Connector/JConnector/NET锛夐噰鐢mysqlclient搴撴潵涓MySQL鏈嶅姟鍣ㄨ繘琛岄氫俊銆傝繖鎰忓懗鐫锛堜緥濡傦級锛屼綘鍙互鍒╃敤寰堝鐩稿悓鐜鍙橀噺锛堜笌鍏朵粬瀹㈡埛绔▼搴忎娇鐢ㄧ殑鐜鍙橀噺鐩稿悓锛夊甫鏉ョ殑濂藉锛岃繖鏄洜涓哄畠浠槸浠庡簱涓紩鐢ㄧ殑銆傚叧浜庤繖浜涘彉閲忕殑璇︾粏娓呭崟锛岃鍙傝绗8绔狅細瀹㈡埛绔拰瀹炵敤宸ュ叿绋嬪簭

瀹㈡埛绔叿鏈夋渶澶х殑閫氫俊缂撳啿鍖哄ぇ灏忋傚垵濮嬪垎閰嶇殑缂撳啿鍖哄ぇ灏忥紙16KB锛夊皢鑷姩澧炲姞鍒版渶澶э紙鏈澶т负16MB锛夈傜敱浜庣紦鍐插尯澶у皬灏嗘寜闇澧炲姞锛岀畝鍗曞湴澧炲姞榛樿鐨勬渶澶ч檺鍒讹紝浠庡叾鏈韩鏉ヨ涓嶄細澧炲姞璧勬簮浣跨敤銆傝澶у皬妫鏌ヤ富瑕佹槸妫鏌ラ敊璇煡璇㈠拰閫氫俊淇℃伅鍖呫

閫氫俊缂撳啿鍖哄繀椤昏冻澶熷ぇ锛岃冻浠ュ寘鍚1SQL璇彞锛堢敤浜庡鎴风-鏈嶅姟鍣ㄩ氫俊锛変互鍙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鏈嶅姟鍣ㄥ簱鈥

25.2.1. C API鏁版嵁绫诲瀷

  • MYSQL

    璇ョ粨鏋勪唬琛1涓暟鎹簱杩炴帴鐨勫彞鏌勩傚嚑涔庢墍鏈夌殑MySQL鍑芥暟鍧囦娇鐢ㄥ畠銆備笉搴斿皾璇曟嫹璐MYSQL缁撴瀯銆備笉淇濊瘉杩欑被鎷疯礉缁撴灉浼氭湁鐢ㄣ

  • MYSQL_RES

    璇ョ粨鏋勪唬琛ㄨ繑鍥炶鐨勬煡璇㈢粨鏋滐紙SELECT, SHOW, DESCRIBE, EXPLAIN锛夈傚湪鏈妭鐨勫墿浣欓儴鍒嗭紝灏嗘煡璇㈣繑鍥炵殑淇℃伅绉颁负鈥滅粨鏋滈泦鈥濄

  • MYSQL_ROW

    杩欐槸1琛屾暟鎹殑鈥滅被鍨嬪畨鍏ㄢ濊〃绀恒傚畠鐩墠鏄寜鐓ц鏁板瓧鑺傚瓧绗︿覆鐨勬暟缁勫疄鏂界殑銆傦紙濡傛灉瀛楁鍊煎彲鑳藉寘鍚簩杩涘埗鏁版嵁锛屼笉鑳藉皢鍏跺綋浣鐢盢ull缁堢粨鐨勫瓧绗︿覆瀵瑰緟锛岃繖鏄洜涓鸿繖绫诲煎彲鑳戒細鍖呭惈Null瀛楄妭锛夈傝鏄氳繃璋冪敤mysql_fetch_row()鑾峰緱鐨勩

  • MYSQL_FIELD

    璇ョ粨鏋勫寘鍚叧浜庡瓧娈电殑淇℃伅锛屽瀛楁鍚嶃佺被鍨嬪拰澶у皬銆傝繖閲岃缁嗕粙缁嶄簡鍏舵垚鍛樸傞氳繃閲嶅璋冪敤mysql_fetch_field()锛屽彲涓烘瘡涓瓧娈佃幏寰MYSQL_FIELD缁撴瀯銆傚瓧娈靛间笉鏄缁撴瀯鐨勭粍鎴愰儴浠斤紝瀹冧滑鍖呭惈鍦MYSQL_ROW缁撴瀯涓

  • MYSQL_FIELD_OFFSET

    杩欐槸MySQL瀛楁鍒楄〃鍋忕Щ閲忕殑鈥滅被鍨嬪畨鍏ㄢ濊〃绀猴紙鐢mysql_field_seek()浣跨敤锛夈傚亸绉婚噺鏄鍐呯殑瀛楁缂栧彿锛屼粠0寮濮嬨

  • my_ulonglong

    鐢ㄤ簬琛屾暟浠ュ強mysql_affected_rows()mysql_num_rows()mysql_insert_id()鐨勭被鍨嬨傝绫诲瀷鎻愪緵鐨勮寖鍥翠负01.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

瀛楁鏄BLOBTEXT锛堜笉鍐嶉噸瑙嗭級

TIMESTAMP_FLAG

瀛楁鏄TIMESTAMP锛堜笉鍐嶉噸瑙嗭級

涓嶅啀閲嶈BLOB_FLAGENUM_FLAGSET_FLAGTIMESTAMP_FLAG鏍囧織锛屽師鍥犲湪浜庯紝瀹冧滑鎸囧嚭浜嗗瓧娈电殑绫诲瀷锛岃屼笉鏄被鍨嬬殑灞炴с傛洿鍙彇鐨勬柟寮忔槸浣跨敤MYSQL_TYPE_BLOBMYSQL_TYPE_ENUMMYSQL_TYPE_SETMYSQL_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)

濡傛灉璇ュ瓧娈垫槸BLOBTEXT锛屼负锛堜笉鍐嶉噸瑙锛岀敤娴嬭瘯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

DECIMALNUMERIC瀛楁

MYSQL_TYPE_NEWDECIMAL

绮惧害鏁板DECIMALNUMERIC

MYSQL_TYPE_FLOAT

FLOAT瀛楁

MYSQL_TYPE_DOUBLE

DOUBLEREAL瀛楁

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

BLOBTEXT瀛楁锛堜娇鐢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");

25.2.2. C API鍑芥暟姒傝堪

杩欓噷褰掔撼浜C API鍙娇鐢ㄧ殑鍑芥暟锛屽苟鍦ㄤ笅涓鑺傝缁嗕粙缁嶄簡瀹冧滑銆傝鍙傝25.2.3鑺傦紝鈥淐 API鍑芥暟鎻忚堪鈥

鍑芥暟

鎻忚堪

mysql_affected_rows()

杩斿洖涓婃UPDATEDELETEINSERT鏌ヨ鏇存敼锛忓垹闄わ紡鎻掑叆鐨勮鏁般

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()锛屼絾鍏跺悕绉版洿娓呮鍦版寚鏄庯紝鏃犺搴旂敤绋嬪簭浣跨敤鐨勬槸mysqlclientmysqld搴擄紝鍚姩鎴栫粨鏉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鏌ヨ锛堜緥濡INSERTUPDATEDELETE锛夛紝閫氳繃璋冪敤mysql_affected_rows()锛屽彲鍙戠幇鏈夊灏戣宸茶鏀瑰彉锛堝奖鍝嶏級銆

瀵逛簬SELECT鏌ヨ锛鑳藉妫绱綔涓虹粨鏋滈泦鐨勮銆傛敞鎰忥紝鏌愪簺璇彞鍥犲叾杩斿洖琛岋紝绫讳技涓SELECT鍖呮嫭SHOWDESCRIBEEXPLAIN銆傚簲鎸夌収瀵瑰緟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鏌ヨ涓嶈繑鍥炴暟鎹紙琛ㄦ槑瀹冩槸INSERTUPDATEDELETE绛夛級锛鑰屼笖涓嶈繑鍥炶銆傚鏋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()鍑芥暟璁块棶閿欒淇℃伅鐨勬満鍒躲傚畠浠兘杩斿洖鍏充簬鏈杩戣皟鐢ㄧ殑鍑芥暟鐨勯敊璇唬鐮佹垨閿欒娑堟伅锛屾渶杩戣皟鐢ㄧ殑鍑芥暟鍙兘鎴愬姛涔熷彲鑳藉け璐ワ紝杩欐牱锛屼綘灏辫兘鍒ゆ柇閿欒鏄湪浣曟椂鍑虹幇鐨勶紝浠ュ強閿欒鏄粈涔堛

25.2.3. C API鍑芥暟鎻忚堪

25.2.3.1. mysql_affected_rows()
25.2.3.2. mysql_autocommit()
25.2.3.3. mysql_change_user()
25.2.3.4. mysql_character_set_name()
25.2.3.5. mysql_close()
25.2.3.6. mysql_commit()
25.2.3.7. mysql_connect()
25.2.3.8. mysql_create_db()
25.2.3.9. mysql_data_seek()
25.2.3.10. mysql_debug()
25.2.3.11. mysql_drop_db()
25.2.3.12. mysql_dump_debug_info()
25.2.3.13. mysql_eof()
25.2.3.14. mysql_errno()
25.2.3.15. mysql_error()
25.2.3.16. mysql_escape_string()
25.2.3.17. mysql_fetch_field()
25.2.3.18. mysql_fetch_field_direct()
25.2.3.19. mysql_fetch_fields()
25.2.3.20. mysql_fetch_lengths()
25.2.3.21. mysql_fetch_row()
25.2.3.22. mysql_field_count()
25.2.3.23. mysql_field_seek()
25.2.3.24. mysql_field_tell()
25.2.3.25. mysql_free_result()
25.2.3.26. mysql_get_character_set_info()
25.2.3.27. mysql_get_client_info()
25.2.3.28. mysql_get_client_version()
25.2.3.29. mysql_get_host_info()
25.2.3.30. mysql_get_proto_info()
25.2.3.31. mysql_get_server_info()
25.2.3.32. mysql_get_server_version()
25.2.3.33. mysql_hex_string()
25.2.3.34. mysql_info()
25.2.3.35. mysql_init()
25.2.3.36. mysql_insert_id()
25.2.3.37. mysql_kill()
25.2.3.38. mysql_library_end()
25.2.3.39. mysql_library_init()
25.2.3.40. mysql_list_dbs()
25.2.3.41. mysql_list_fields()
25.2.3.42. mysql_list_processes()
25.2.3.43. mysql_list_tables()
25.2.3.44. mysql_more_results()
25.2.3.45. mysql_next_result()
25.2.3.46. mysql_num_fields()
25.2.3.47. mysql_num_rows()
25.2.3.48. mysql_options()
25.2.3.49. mysql_ping()
25.2.3.50. mysql_query()
25.2.3.51. mysql_real_connect()
25.2.3.52. mysql_real_escape_string()
25.2.3.53. mysql_real_query()
25.2.3.54. mysql_refresh()
25.2.3.55. mysql_reload()
25.2.3.56. mysql_rollback()
25.2.3.57. mysql_row_seek()
25.2.3.58. mysql_row_tell()
25.2.3.59. mysql_select_db()
25.2.3.60. mysql_set_character_set()
25.2.3.61. mysql_set_server_option()
25.2.3.62. mysql_shutdown()
25.2.3.63. mysql_sqlstate()
25.2.3.64. mysql_ssl_set()
25.2.3.65. mysql_stat()
25.2.3.66. mysql_store_result()
25.2.3.67. mysql_thread_id()
25.2.3.68. mysql_use_result()
25.2.3.69. mysql_warning_count()

鍦ㄦ湰鑺傛墍浣滅殑浠嬬粛涓紝鎸夌収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()锛屽彲鑾峰緱閿欒鐨勫瓧绗︿覆琛ㄧず銆

25.2.3.1.聽mysql_affected_rows()

my_ulonglong mysql_affected_rows(MYSQL *mysql)

鎻忚堪

杩斿洖涓婃UPDATE鏇存敼鐨勮鏁帮紝涓婃DELETE鍒犻櫎鐨勮鏁帮紝鎴涓婃INSERT璇彞鎻掑叆鐨勮鏁般傚浜UPDATEDELETEINSERT璇彞锛屽彲鍦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

25.2.3.2.聽mysql_autocommit()

my_bool mysql_autocommit(MYSQL *mysql, my_bool mode)

鎻忚堪

濡傛灉妯″紡涓衡1鈥濓紝鍚敤autocommit妯″紡锛涘鏋滄ā寮忎负鈥0鈥濓紝绂佹autocommit妯″紡銆

杩斿洖鍊

濡傛灉鎴愬姛锛岃繑鍥0锛屽鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺

閿欒

鏃犮

25.2.3.3.聽mysql_change_user()

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));
}

25.2.3.4.聽mysql_character_set_name()

const char *mysql_character_set_name(MYSQL *mysql)

鎻忚堪

涓哄綋鍓嶈繛鎺ヨ繑鍥為粯璁ょ殑瀛楃闆嗐

杩斿洖鍊

榛樿瀛楃闆嗐

閿欒

鏃犮

25.2.3.5.聽mysql_close()

void mysql_close(MYSQL *mysql)

鎻忚堪

鍏抽棴鍓嶉潰鎵撳紑鐨勮繛鎺ャ傚鏋滃彞鏌勬槸鐢mysql_init()mysql_connect()鑷姩鍒嗛厤鐨勶紝mysql_close()杩樺皢瑙i櫎鍒嗛厤鐢mysql鎸囧悜鐨勮繛鎺ュ彞鏌勩

杩斿洖鍊

鏃犮

閿欒

鏃犮

25.2.3.6.聽mysql_commit()

my_bool mysql_commit(MYSQL *mysql)

鎻忚堪

鎻愪氦褰撳墠浜嬪姟銆

璇ュ嚱鏁扮殑鍔ㄤ綔鍙completion_type绯荤粺鍙橀噺鐨勫兼帶鍒躲傚挨鍏舵槸锛濡傛灉completion_type鐨勫间负2锛岀粓缁撲簨鍔″苟鍏抽棴瀹㈡埛绔繛鎺ュ悗锛屾湇鍔″櫒灏嗘墽琛岄噴鏀炬搷浣溿傚鎴风绋嬪簭搴旇皟鐢mysql_close()锛屼粠瀹㈡埛绔竴渚у叧闂繛鎺ャ

杩斿洖鍊

濡傛灉鎴愬姛锛岃繑鍥0锛屽鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺

閿欒

鏃犮

25.2.3.7.聽mysql_connect()

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()鐨勭浉鍚屻

25.2.3.8.聽mysql_create_db()

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));
}

25.2.3.9.聽mysql_data_seek()

void mysql_data_seek(MYSQL_RES *result, my_ulonglong offset)

鎻忚堪

鍦ㄦ煡璇㈢粨鏋滈泦涓鎵句换鎰忚銆傚亸绉诲间负琛屽彿锛岃寖鍥翠粠0mysql_num_rows(result)-1

璇ュ嚱鏁拌姹傜粨鏋滈泦缁撴瀯鍖呭惈鏌ヨ鐨勬墍鏈夌粨鏋滐紝鍥犳锛so mysql_data_seek()浠呭簲涓mysql_store_result()鑱斿悎浣跨敤锛岃屼笉鏄mysql_use_result()

杩斿洖鍊

鏃犮

閿欒

鏃犮

25.2.3.10.聽mysql_debug()

void mysql_debug(const char *debug)

鎻忚堪

鐢ㄧ粰瀹氱殑瀛楃涓叉墽琛DBUG_PUSHmysql_debug()閲囩敤Fred Fish璋冭瘯搴撱傝鎯充娇鐢ㄨ鍑芥暟锛屽繀椤荤紪璇戝鎴风搴擄紝浣夸箣鏀寔璋冭瘯鍔熻兘銆傝鍙傝E.1鑺傦紝鈥滆皟璇昅ySQL鏈嶅姟鍣ㄢ銆傝鍙傝E.2鑺傦紝鈥滆皟璇昅ySQL瀹㈡埛绔

杩斿洖鍊

鏃犮

閿欒

鏃犮

绀轰緥锛

杩欓噷缁欏嚭鐨勮皟鐢ㄥ皢浣垮鎴风搴撳湪瀹㈡埛绔満鍣ㄧ殑/tmp/client.trace涓敓鎴1涓窡韪枃浠躲

mysql_debug("d:t:O,/tmp/client.trace");

25.2.3.11.聽mysql_drop_db()

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));

25.2.3.12.聽mysql_dump_debug_info()

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

鍑虹幇鏈煡閿欒銆

25.2.3.13.聽mysql_eof()

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));
}

25.2.3.14.聽mysql_errno()

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鈥琛ㄧず鏈嚭鐜伴敊璇

閿欒

鏃犮

25.2.3.15.聽mysql_error()

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缁堢粨鐨勫瓧绗︿覆銆傚鏋滄湭鍑虹幇閿欒锛岃繑鍥炵┖瀛楃涓层

閿欒

鏃犮

25.2.3.16.聽mysql_escape_string()

搴斾娇鐢mysql_real_escape_string()鍙栬屼唬涔嬶紒

璇ュ嚱鏁颁笌mysql_real_escape_string()绛夊悓锛mysql_real_escape_string()浼氬皢杩炴帴澶勭悊绋嬪簭浣滀负鍏剁1涓弬閲忥紝骞舵寜鐓у綋鍓嶅瓧绗﹂泦瀵瑰瓧绗︿覆杩涜杞箟澶勭悊銆mysql_escape_string()涓嶉噰鐢ㄨ繛鎺ュ弬閲忥紝鑰屼笖涓嶈冭檻褰撳墠瀛楃闆嗚缃

25.2.3.17.聽mysql_fetch_field()

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);
}

25.2.3.18.聽mysql_fetch_field_direct()

MYSQL_FIELD *mysql_fetch_field_direct(MYSQL_RES *result, unsigned int fieldnr)

鎻忚堪

缁欏畾缁撴灉闆嗗唴鏌1鍒楃殑瀛楁缂栧彿fieldnr锛屼互MYSQL_FIELD缁撴瀯褰㈠紡杩斿洖鍒楃殑瀛楁瀹氫箟銆傚彲浠ヤ娇鐢ㄨ鍑芥暟妫绱换鎰忓垪鐨勫畾涔夈Fieldnr鐨勫煎簲鍦ㄤ粠0mysql_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);
}

25.2.3.19.聽mysql_fetch_fields()

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);
}

25.2.3.20.聽mysql_fetch_lengths()

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]);
    }
}

25.2.3.21.聽mysql_fetch_row()

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");
}

25.2.3.22.聽mysql_field_count()

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()鐨勫艰繘琛屾帹鏂

25.2.3.23.聽mysql_field_seek()

MYSQL_FIELD_OFFSET mysql_field_seek(MYSQL_RES *result, MYSQL_FIELD_OFFSET offset)

鎻忚堪

灏嗗瓧娈靛厜鏍囪缃埌缁欏畾鐨勫亸绉诲銆傚mysql_fetch_field()鐨勪笅涓娆¤皟鐢ㄥ皢妫绱笌璇ュ亸绉荤浉鍏崇殑鍒楀畾涔夈

瑕佹兂鏌ユ壘琛岀殑寮濮嬶紝璇蜂紶閫掑间负0鐨勫亸绉婚噺銆

杩斿洖鍊

瀛楁鍏夋爣鐨勫墠涓涓笺

閿欒

鏃犮

25.2.3.24.聽mysql_field_tell()

MYSQL_FIELD_OFFSET mysql_field_tell(MYSQL_RES *result)

鎻忚堪

杩斿洖涓婁竴涓mysql_fetch_field()鎵浣跨敤鐨勫瓧娈靛厜鏍囩殑瀹氫箟銆傝鍊煎彲鐢ㄤ綔mysql_field_seek()鐨勫弬閲忋

杩斿洖鍊

瀛楁鍏夋爣鐨勫綋鍓嶅亸绉婚噺銆

閿欒

鏃犮

25.2.3.25.聽mysql_free_result()

void mysql_free_result(MYSQL_RES *result)

鎻忚堪

閲婃斁鐢mysql_store_result()mysql_use_result()mysql_list_dbs()绛変负缁撴灉闆嗗垎閰嶇殑鍐呭瓨銆傚畬鎴愬缁撴灉闆嗙殑鎿嶄綔鍚庯紝蹇呴』璋冪敤mysql_free_result()閲婃斁缁撴灉闆嗕娇鐢ㄧ殑鍐呭瓨銆

閲婃斁瀹屾垚鍚庯紝涓嶈灏濊瘯璁块棶缁撴灉闆嗐

杩斿洖鍊

鏃犮

閿欒

鏃犮

25.2.3.26.聽mysql_get_character_set_info()

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);
}

25.2.3.27.聽mysql_get_client_info()

char *mysql_get_client_info(void)

鎻忚堪

杩斿洖琛ㄧず瀹㈡埛绔簱鐗堟湰鐨勫瓧绗︿覆銆

杩斿洖鍊

琛ㄧずMySQL瀹㈡埛绔簱鐗堟湰鐨勫瓧绗︿覆銆

閿欒

鏃犮

25.2.3.28.聽mysql_get_client_version()

unsigned long mysql_get_client_version(void)

鎻忚堪

杩斿洖琛ㄧず瀹㈡埛绔簱鐗堟湰鐨勬暣鏁般傝鍊肩殑鏍煎紡鏄XYYZZ锛屽叾涓X鏄富鐗堟湰鍙凤紝YY鏄彂甯冪骇鍒紝ZZ鏄彂甯冪骇鍒唴鐨勭増鏈彿銆渚嬪锛屽40102琛ㄧず瀹㈡埛绔簱鐨勭増鏈槸4.1.2

杩斿洖鍊

琛ㄧずMySQL瀹㈡埛绔簱鐗堟湰鐨勬暣鏁般

閿欒

鏃犮

25.2.3.29.聽mysql_get_host_info()

char *mysql_get_host_info(MYSQL *mysql)

鎻忚堪

杩斿洖鎻忚堪浜嗘墍浣跨敤杩炴帴绫诲瀷鐨勫瓧绗︿覆锛屽寘鎷湇鍔″櫒涓绘満鍚嶃

杩斿洖鍊

浠h〃鏈嶅姟鍣ㄤ富鏈哄悕鍜岃繛鎺ョ被鍨嬬殑瀛楃涓层

閿欒

鏃犮

25.2.3.30.聽mysql_get_proto_info()

unsigned int mysql_get_proto_info(MYSQL *mysql)

鎻忚堪

杩斿洖褰撳墠杩炴帴鎵浣跨敤鐨勫崗璁増鏈

杩斿洖鍊

浠h〃褰撳墠杩炴帴鎵浣跨敤鍗忚鐗堟湰鐨勬棤绗﹀彿鏁存暟銆

閿欒

鏃犮

25.2.3.31.聽mysql_get_server_info()

char *mysql_get_server_info(MYSQL *mysql)

鎻忚堪

杩斿洖浠h〃鏈嶅姟鍣ㄧ増鏈彿鐨勫瓧绗︿覆銆

杩斿洖鍊

浠h〃鏈嶅姟鍣ㄧ増鏈彿鐨勫瓧绗︿覆銆

閿欒

鏃犮

25.2.3.32.聽mysql_get_server_version()

unsigned long mysql_get_server_version(MYSQL *mysql)

鎻忚堪

浠ユ暣鏁板舰寮忚繑鍥炴湇鍔″櫒鐨勭増鏈彿銆

杩斿洖鍊

琛ㄧずMySQL鏈嶅姟鍣ㄧ増鏈殑鏁板硷紝鏍煎紡濡備笅锛

major_version*10000 + minor_version *100 + sub_version

渚嬪锛屽浜5.0.12锛岃繑鍥500012

鍦ㄥ鎴风绋嬪簭涓紝涓轰簡蹇熺‘瀹氭煇浜涗笌鐗堟湰鐩稿叧鐨勬湇鍔″櫒鍔熻兘鏄惁瀛樺湪锛岃鍑芥暟寰堟湁鐢ㄣ

閿欒

鏃犮

25.2.3.33.聽mysql_hex_string()

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瀛楃銆

鍙噰鐢0xvalueX'value'鏍煎紡灏嗚繑鍥炲肩疆浜SQL璇彞涓備絾鏄紝杩斿洖鍊间笉鍖呮嫭0xX'...'銆傝皟鐢ㄨ呭繀椤绘彁渚涙墍甯屾湜鐨勬牸寮忔槸浣曠銆

绀轰緥锛

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瀛楃銆

閿欒

鏃犮

25.2.3.34.聽mysql_info()

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

閿欒

鏃犮

25.2.3.35.聽mysql_init()

MYSQL *mysql_init(MYSQL *mysql)

鎻忚堪

鍒嗛厤鎴栧垵濮嬪寲涓mysql_real_connect()鐩搁傚簲鐨MYSQL瀵硅薄銆濡傛灉mysqlNULL鎸囬拡锛璇ュ嚱鏁板皢鍒嗛厤銆佸垵濮嬪寲銆佸苟杩斿洖鏂板璞°傚惁鍒欙紝灏嗗垵濮嬪寲瀵硅薄锛屽苟杩斿洖瀵硅薄鐨勫湴鍧銆傚鏋mysql_init()鍒嗛厤浜嗘柊鐨勫璞★紝褰撹皟鐢mysql_close()鏉ュ叧闂繛鎺ユ椂銆傚皢閲婃斁璇ュ璞°

杩斿洖鍊

鍒濆鍖栫殑MYSQL*鍙ユ焺銆傚鏋滄棤瓒冲鍐呭瓨浠ュ垎閰嶆柊鐨勫璞★紝杩斿洖NULL

閿欒

鍦ㄥ唴瀛樹笉瓒崇殑鎯呭喌涓嬶紝杩斿洖NULL

25.2.3.36.聽mysql_insert_id()

my_ulonglong mysql_insert_id(MYSQL *mysql)

鎻忚堪

杩斿洖鐢变互鍓嶇殑INSERTUPDATE璇彞涓AUTO_INCREMENT鍒楃敓鎴愮殑鍊笺傚湪鍖呭惈AUTO_INCREMENT瀛楁鐨勮〃涓墽琛屼簡INSERT璇彞鍚庯紝搴斾娇鐢ㄨ鍑芥暟銆

鏇村噯纭湴璁诧紝灏嗗湪涓嬭堪鏉′欢涓嬫洿鏂mysql_insert_id()

         灏嗗间繚瀛樺埌AUTO_INCREMENT鍒椾腑鐨INSERT璇彞銆傛棤璁哄兼槸閫氳繃鍦ㄥ垪涓瓨鍌ㄧ壒娈婂NULL0鑷姩鐢熸垚鐨勶紝杩樻槸纭垏鐨勯潪鐗规畩鍊硷紝閮芥垚绔嬨

         鍦ㄦ湁澶氳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_INCREMENTmysql_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鍒椾腑鍑虹幇鎯呭喌鐨勬洿鍑嗙‘淇℃伅銆

杩斿洖鍊

鍦ㄥ墠闈㈢殑璁ㄨ涓簣浠ヤ簡浠嬬粛銆

閿欒

鏃犮

25.2.3.37.聽mysql_kill()

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

鍑虹幇鏈煡閿欒銆

25.2.3.38.聽mysql_library_end()

void mysql_library_end(void)

鎻忚堪

瀹冩槸mysql_server_end()鍑芥暟鐨勫悓涔夎瘝銆

鍏充簬鍏蜂綋鐨勭敤娉曪紝璇峰弬瑙25.2.2鑺傦紝鈥淐 API鍑芥暟姒傝堪鈥

25.2.3.39.聽mysql_library_init()

int mysql_library_init(int argc, char **argv, char **groups)

鎻忚堪

杩欐槸mysql_server_init()鍑芥暟鐨勫悓涔夎瘝銆

鍏充簬鍏蜂綋鐨勭敤娉曪紝璇峰弬瑙25.2.2鑺傦紝鈥淐 API鍑芥暟姒傝堪鈥

25.2.3.40.聽mysql_list_dbs()

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

鍑虹幇鏈煡閿欒銆

25.2.3.41.聽mysql_list_fields()

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

鍑虹幇鏈煡閿欒銆

25.2.3.42.聽mysql_list_processes()

MYSQL_RES *mysql_list_processes(MYSQL *mysql)

鎻忚堪

杩斿洖鎻忚堪褰撳墠鏈嶅姟鍣ㄧ嚎绋嬬殑缁撴灉闆嗐傝绫讳俊鎭笌mysqladmin processlistSHOW PROCESSLIST鏌ヨ缁欏嚭鐨勪俊鎭浉鍚屻

蹇呴』鐢mysql_free_result()閲婃斁缁撴灉闆嗐

杩斿洖鍊

濡傛灉鎴愬姛锛岃繑鍥MYSQL_RES缁撴灉闆嗐濡傛灉鍑虹幇閿欒锛岃繑鍥NULL

閿欒

         CR_COMMANDS_OUT_OF_SYNC

浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆

         CR_SERVER_GONE_ERROR

MySQL鏈嶅姟鍣ㄤ笉鍙敤銆

         CR_SERVER_LOST

鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆

         CR_UNKNOWN_ERROR

鍑虹幇鏈煡閿欒銆

25.2.3.43.聽mysql_list_tables()

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

鍑虹幇鏈煡閿欒銆

25.2.3.44.聽mysql_more_results()

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()鈥

閿欒

鏃犮

25.2.3.45.聽mysql_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

鍑虹幇鏈煡閿欒銆

25.2.3.46.聽mysql_num_fields()

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)璋冪敤銆備粎褰撳嚭閿欐椂鎵嶅簲浣跨敤瀹冦

25.2.3.47.聽mysql_num_rows()

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()涓嶈繑鍥炴纭殑鍊硷紝鐩磋嚦妫绱簡缁撴灉闆嗕腑鐨勬墍鏈夎涓烘銆

杩斿洖鍊

缁撴灉闆嗕腑鐨勮鏁般

閿欒

鏃犮

25.2.3.48.聽mysql_options()

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_FILEMYSQL_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閮ㄥ垎璇诲彇棰濆閫夐」銆

25.2.3.49.聽mysql_ping()

int mysql_ping(MYSQL *mysql)

鎻忚堪

妫鏌ヤ笌鏈嶅姟鍣ㄧ殑杩炴帴鏄惁宸ヤ綔銆傚鏋滆繛鎺ヤ涪澶憋紝灏嗚嚜鍔ㄥ皾璇曞啀杩炴帴銆

璇ュ嚱鏁板彲琚棽缃簡杈冮暱鏃堕棿鐨勫鎴风浣跨敤锛岀敤浠ユ鏌ユ湇鍔″櫒鏄惁宸插叧闂簡杩炴帴锛屽苟鍦ㄥ繀瑕佹椂鍐嶆杩炴帴銆

杩斿洖鍊

濡傛灉涓庢湇鍔″櫒鐨勮繛鎺ユ湁鏁堣繑鍥0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺傝繑鍥炵殑闈0鍊间笉琛ㄧずMySQL鏈嶅姟鍣ㄦ湰韬槸鍚﹀凡鍏抽棴锛岃繛鎺ュ彲鑳藉洜鍏朵粬鍘熷洜缁堢锛屽缃戠粶闂绛夈

閿欒

         CR_COMMANDS_OUT_OF_SYNC

浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆

         CR_SERVER_GONE_ERROR

MySQL鏈嶅姟鍣ㄤ笉鍙敤銆

         CR_UNKNOWN_ERROR

鍑虹幇鏈煡閿欒銆

25.2.3.50.聽mysql_query()

int mysql_query(MYSQL *mysql, const char *query)

鎻忚堪

鎵ц鐢扁Null缁堢粨鐨勫瓧绗︿覆鈥濇煡璇㈡寚鍚戠殑SQL鏌ヨ銆傛甯告儏鍐典笅锛屽瓧绗︿覆蹇呴』鍖呭惈1SQL璇彞锛岃屼笖涓嶅簲涓鸿鍙ユ坊鍔犵粓缁撳垎鍙凤紙鈥;鈥欙級鎴栤\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

鍑虹幇鏈煡閿欒銆

25.2.3.51.聽mysql_real_connect()

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鈥鏄暟鎹簱鍚嶇О銆傚鏋dbNULL杩炴帴浼氬皢榛樿鐨勬暟鎹簱璁句负璇ュ笺

         濡傛灉鈥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_FILEMYSQL_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閫夐」锛屽鍐嶈繛鎺ヨ涓鸿繘琛屾帶鍒躲

25.2.3.52.聽mysql_real_escape_string()

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瀛楃銆

閿欒

鏃犮

25.2.3.53.聽mysql_real_query()

int mysql_real_query(MYSQL *mysql, const char *query, unsigned long length)

鎻忚堪

鎵ц鐢扁query鈥濇寚鍚戠殑SQL鏌ヨ锛屽畠搴旀槸瀛楃涓查暱搴﹀瓧鑺傗long鈥濄傛甯告儏鍐典笅锛瀛楃涓插繀椤诲寘鍚1SQL璇彞锛岃屼笖涓嶅簲涓鸿鍙ユ坊鍔缁堢粨鍒嗗彿锛堚;鈥欙級鎴栤\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

鍑虹幇鏈煡閿欒銆

25.2.3.54.聽mysql_refresh()

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

鍑虹幇鏈煡閿欒銆

25.2.3.55.聽mysql_reload()

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

鍑虹幇鏈煡閿欒銆

25.2.3.56.聽mysql_rollback()

my_bool mysql_rollback(MYSQL *mysql)

鎻忚堪

鍥炴粴褰撳墠浜嬪姟銆

璇ュ嚱鏁扮殑鍔ㄤ綔鍙栧喅浜completion_type绯荤粺鍙橀噺鐨勫笺灏ゅ叾鏄紝濡傛灉completion_type鐨勫间负鈥2鈥锛岀粓缁撲簨鍔″悗锛屾湇鍔″櫒灏嗘墽琛岄噴鏀炬搷浣滐紝骞跺叧闂鎴风杩炴帴銆傚鎴风绋嬪簭搴旇皟鐢mysql_close()浠庡鎴风涓渚у叧闂繛鎺ャ

杩斿洖鍊

濡傛灉鎴愬姛锛岃繑鍥0锛屽鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺

閿欒

鏃犮

25.2.3.57.聽mysql_row_seek()

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()鐨勫悗缁皟鐢ㄣ

閿欒

鏃犮

25.2.3.58.聽mysql_row_tell()

MYSQL_ROW_OFFSET mysql_row_tell(MYSQL_RES *result)

鎻忚堪

瀵逛簬涓婁竴涓mysql_fetch_row()杩斿洖琛屽厜鏍囩殑褰撳墠浣嶇疆銆傝鍊煎彲鐢ㄤ綔mysql_row_seek()鐨勫弬閲忋

浠呭簲鍦mysql_store_result()涔嬪悗锛岃屼笉鏄mysql_use_result()涔嬪悗浣跨敤mysql_row_tell()

杩斿洖鍊

琛屽厜鏍囩殑褰撳墠鍋忕Щ閲忋

閿欒

鏃犮

25.2.3.59.聽mysql_select_db()

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

鍑虹幇鏈煡閿欒銆

25.2.3.60.聽mysql_set_character_set()

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));
}

25.2.3.61.聽mysql_set_server_option()

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鏃讹級锛屾垨鏈嶅姟鍣ㄤ笉鏀寔璇曞浘璁剧疆鐨勯夐」銆

 

25.2.3.62.聽mysql_shutdown()

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

鍑虹幇鏈煡閿欒銆

25.2.3.63.聽mysql_sqlstate()

const char *mysql_sqlstate(MYSQL *mysql)

鎻忚堪

杩斿洖鐢Null缁堢粨鐨勫瓧绗︿覆锛岃瀛楃涓插寘鍚叧浜庝笂娆¢敊璇殑SQLSTATE閿欒浠g爜銆傞敊璇唬鐮佸寘鍚5涓瓧绗︺'00000'琛ㄧず鏃犻敊璇傚叾鍊肩敱ANSI SQLODBC鎸囧畾銆傚叧浜庡彲鑳藉彇鍊肩殑鍒楄〃锛岃鍙傝闄勫綍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()鈥

25.2.3.64.聽mysql_ssl_set()

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()杩斿洖鐨勮繛鎺ュ鐞嗙▼搴忋鍏朵粬鍙傛暟鐨勬寚瀹氬涓嬶細

         keykey鏂囦欢鐨勮矾寰勫悕銆

         cert鏄瘉涔︽枃浠剁殑璺緞鍚嶃

         ca鏄瘉涔︽巿鏉冩枃浠剁殑璺緞鍚嶃

         capath鏄寚鍚戠洰褰曠殑璺緞鍚嶏紝璇ョ洰褰曚腑鍖呭惈浠pem鏍煎紡缁欏嚭鐨勫彈淇′换SSL CA璇佷功銆

         cipher鏄厑璁稿瘑鐮佺殑鍒楄〃锛岀敤浜SSL鍔犲瘑銆

瀵逛簬浠讳綍鏈娇鐢ㄧ殑SSL鍙傛暟锛屽彲涓哄叾缁欏畾NULL

杩斿洖鍊

璇ュ嚱鏁版昏繑鍥0銆傚鏋SSL璁剧疆涓嶆纭紝褰撲綘灏濊瘯杩炴帴鏃讹紝mysql_real_connect()灏嗚繑鍥為敊璇

25.2.3.65.聽mysql_stat()

char *mysql_stat(MYSQL *mysql)

鎻忚堪

杩斿洖鍖呭惈鐗瑰畾淇℃伅鐨勫瓧绗︿覆锛岃淇℃伅涓mysqladmin status鍛戒护鎻愪緵鐨勪俊鎭被浼笺傚寘鎷互绉掍负鍗曚綅鐨勬甯歌繍琛屾椂闂达紝浠ュ強杩愯绾跨▼鐨勬暟鐩紝闂鏁帮紝鍐嶅姞杞芥鏁帮紝浠ュ強鎵撳紑鐨勮〃鏁扮洰銆

杩斿洖鍊

鎻忚堪鏈嶅姟鍣ㄧ姸鎬佺殑瀛楃闆嗐濡傛灉鍑虹幇閿欒锛岃繑鍥NULL

閿欒

         CR_COMMANDS_OUT_OF_SYNC

浠ヤ笉鎭板綋鐨勯『搴忔墽琛屼簡鍛戒护銆

         CR_SERVER_GONE_ERROR

MySQL鏈嶅姟鍣ㄤ笉鍙敤銆

         CR_SERVER_LOST

鍦ㄦ煡璇㈣繃绋嬩腑锛屼笌鏈嶅姟鍣ㄧ殑杩炴帴涓㈠け銆

         CR_UNKNOWN_ERROR

鍑虹幇鏈煡閿欒銆

25.2.3.66.聽mysql_store_result()

MYSQL_RES *mysql_store_result(MYSQL *mysql)

鎻忚堪

瀵逛簬鎴愬姛妫绱簡鏁版嵁鐨勬瘡涓煡璇紙SELECTSHOWDESCRIBEEXPLAINCHECK 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()灏嗘煡璇㈢殑鍏ㄩ儴缁撴灉璇诲彇鍒板鎴风锛鍒嗛厤1MYSQL_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

鍑虹幇鏈煡閿欒銆

25.2.3.67.聽mysql_thread_id()

unsigned long mysql_thread_id(MYSQL *mysql)

鎻忚堪

杩斿洖褰撳墠杩炴帴鐨勭嚎绋ID銆傝鍊煎彲鐢ㄤ綔mysql_kill()鐨勫弬閲忎互鏉姝荤嚎绋嬨

濡傛灉杩炴帴涓㈠け锛屽苟浣跨敤mysql_ping()杩涜浜嗗啀杩炴帴锛绾跨▼ID灏嗘敼鍙樸傝繖鎰忓懗鐫浣犱笉搴旇幏鍙栫嚎绋ID骞朵繚瀛樺畠渚涗互鍚庝娇鐢ㄣ傚簲鍦ㄩ渶瑕佹椂鑾峰彇瀹冦

杩斿洖鍊

褰撳墠杩炴帴鐨勭嚎绋ID

閿欒

鏃犮

25.2.3.68.聽mysql_use_result()

MYSQL_RES *mysql_use_result(MYSQL *mysql)

鎻忚堪

瀵逛簬鎴愬姛妫绱㈡暟鎹殑姣忎釜鏌ヨ锛SELECTSHOWDESCRIBEEXPLAIN锛夛紝蹇呴』璋冪敤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

鍑虹幇鏈煡閿欒銆

25.2.3.69.聽mysql_warning_count()

unsigned int mysql_warning_count(MYSQL *mysql)

閿欒

杩斿洖鎵ц鍓嶄竴涓SQL璇彞鏈熼棿鐢熸垚鐨勫憡璀︽暟鐩

杩斿洖鍊

鍛婅璁℃暟銆

閿欒

鏃犮

25.2.4. C API棰勫鐞嗚鍙

MySQL瀹㈡埛绔紡鏈嶅姟鍣ㄥ崗璁彁渚涗簡棰勫鐞嗚鍙ャ傝鍔熻兘閲囩敤浜嗙敱mysql_stmt_init()鍒濆鍖栧嚱鏁拌繑鍥炵殑MYSQL_STMT璇彞澶勭悊绋嬪簭鏁版嵁缁撴瀯銆傚浜庡娆℃墽琛岀殑璇彞锛岄澶勭悊鎵ц鏄竴绉嶆湁鏁堢殑鏂瑰紡銆棣栧厛瀵硅鍙ヨ繘琛岃В鏋愶紝涓烘墽琛屼綔濂藉噯澶囥傛帴涓嬫潵锛屽湪浠ュ悗浣跨敤鍒濆鍖栧嚱鏁拌繑鍥炵殑璇彞鍙ユ焺鎵ц涓娆℃垨澶氭銆

瀵逛簬澶氭鎵ц鐨勮鍙ワ紝棰勫鐞嗘墽琛屾瘮鐩存帴鎵ц蹇紝涓昏鍘熷洜鍦ㄤ簬锛屼粎瀵规煡璇㈡墽琛屼竴娆¤В鏋愭搷浣溿傚湪鐩存帴鎵ц鐨勬儏鍐典笅锛屾瘡娆℃墽琛岃鍙ユ椂锛屽潎灏嗚繘琛屾煡璇€傛澶栵紝鐢变簬姣忔鎵ц棰勫鐞嗚鍙ユ椂浠呴渶鍙戦佸弬鏁扮殑鏁版嵁锛屼粠鑰屽噺灏戜簡缃戠粶閫氫俊閲忋

棰勫鐞嗚鍙ョ殑鍙︿竴涓紭鐐规槸锛屽畠閲囩敤浜嗕簩杩涘埗鍗忚锛屼粠鑰屼娇寰楀鎴风鍜屾湇鍔″櫒涔嬮棿鐨勬暟鎹紶杈撴洿鏈夋晥鐜囥

涓嬭堪璇彞鍙敤浣滈澶勭悊璇彞锛CREATE TABLEDELETEDOINSERTREPLACESELECTSETUPDATE銆佷互鍙婂ぇ澶氭暟SHOW璇彞銆MySQL 5.1涓紝涓嶆敮鎸佸叾浠栬鍙ャ

25.2.5. C API棰勫鐞嗚鍙ョ殑鏁版嵁绫诲瀷

棰勫鐞嗚鍙ヤ富瑕佷娇鐢MYSQL_STMTMYSQL_BIND鏁版嵁缁撴瀯銆3绉嶇粨鏋MYSQL_TIME鐢ㄤ簬浼犺緭鏆傛椂鎬ф暟鎹

         MYSQL_STMT

璇ョ粨鏋勮〃绀洪澶勭悊璇彞銆傞氳繃璋冪敤mysql_stmt_init()鍒涘缓璇彞锛岃繑鍥炶鍙ュ彞鏌勶紝鍗虫寚鍚MYSQL_STMT鐨勬寚閽堛傝鍙ユ焺鐢ㄦ埛鎵鏈夊悗缁殑涓庤鍙ユ湁鍏崇殑鍑芥暟锛岀洿鑷充娇鐢mysql_stmt_close()鍏抽棴浜嗗畠涓烘銆

MYSQL_STMT缁撴瀯娌℃湁渚涘簲鐢ㄧ▼搴忎娇鐢ㄧ殑鍙傛暟銆傛澶栵紝涓嶅簲灏濊瘯澶嶅埗MYSQL_STMT缁撴瀯銆備笉淇濊瘉杩欑被澶嶅埗鐗╀細鏈夌敤銆

澶氫釜璇彞鍙ユ焺鑳藉涓庡崟涓繛鎺ュ叧鑱旇捣鏉ャ傚鍙ユ焺鏁扮洰鐨勯檺鍒跺彇鍐充簬绯荤粺璧勬簮銆

         MYSQL_BIND

璇ョ粨鏋勭敤浜庤鍙ヨ緭鍏ワ紙鍙戦佺粰鏈嶅姟鍣ㄧ殑鏁版嵁鍊硷級鍜岃緭鍑猴紙浠庢湇鍔″櫒杩斿洖鐨勭粨鏋滃硷級銆傚浜庤緭鍏ワ紝瀹冧笌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鎴愬憳锛屾寚鏄庡彉閲忔槸signedunsigned绫诲瀷锛岃鎯呰鍙傝鏈妭鍚庨潰鐨勪粙缁嶏級銆傚浜庢棩鏈熷拰鏃堕棿鍒楃被鍨嬶紝缂撳啿搴旀寚鍚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鍙橀噺鐨勫湴鍧锛屽苟鍦ㄥ悇娆℃墽琛屼箣闂存伆褰撳湴鏇存敼鍙橀噺鐨勫硷紝浠ユ寚鏄庢暟鎹兼槸NULLNOT NULL

o        my_bool is_unsigned

璇ユ垚鍛樼敤浜庢暣鏁扮被鍨嬨傦紙瀵瑰簲浜MYSQL_TYPE_TINYMYSQL_TYPE_SHORTMYSQL_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));

         MYSQL_TIME

璇ョ粨鏋勭敤浜庡皢DATETIMEDATETIMETIMESTAMP鏁版嵁鐩存帴鍙戦佸埌鏈嶅姟鍣紝鎴栦粠鏈嶅姟鍣ㄧ洿鎺ユ帴鏀惰繖绫绘暟鎹傚皢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缁撴瀯鐨勯儴鍒锛氱敤浜DATEDATETIMETIMESTAMP骞淬佹湀銆佹棩閮ㄥ垎銆傜敤浜TIMEDATETIMETIMESTAMP鍊肩殑灏忔椂銆佸垎閽熴佺閮ㄥ垎銆傝鍙傝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.6. C API棰勫鐞嗚鍙ュ嚱鏁版杩

鍦ㄦ褰掔撼浜嗛澶勭悊璇彞澶勭悊鍔熻兘鍙娇鐢ㄧ殑鍑芥暟锛屽苟鍦ㄥ悗闈㈢殑绔犺妭涓缁嗕粙缁嶄簡瀹冦傝鍙傝25.2.7鑺傦紝鈥淐 API棰勫鐞嗚鍙ュ嚱鏁版弿杩扳

鍑芥暟

鎻忚堪

mysql_stmt_affected_rows()

杩斿洖鐢遍澶勭悊璇彞UPDATEDELETEINSERT鍙樻洿銆佸垹闄ゆ垨鎻掑叆鐨勮鏁扮洰銆

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瀹㈡埛绔紡鏈嶅姟鍣ㄥ崗璁皢鎵ц涓嬭堪鍔ㄤ綔锛

         鏈嶅姟鍣ㄨВ鏋愯鍙ワ紝骞堕氳繃璧嬪艰鍙IDOK鐘舵佸彂鍥炲鎴风銆傛澶栵紝濡傛灉瀹冩槸闈㈠悜缁撴灉闆嗙殑璇彞锛岃繕灏嗗彂閫佹荤殑鍙傛暟鏁扮洰锛屽垪璁℃暟鍜屽厓鏁版嵁銆傚湪姝よ皟鐢ㄨ繃绋嬩腑锛屾湇鍔″櫒灏嗘鏌ヨ鍙ョ殑鎵鏈夎娉曞拰璇箟銆

         瀹㈡埛绔噰鐢ㄨ璇彞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涔嬪墠锛屼笉鏄剧ず鈥滄墽琛屸濊銆

25.2.7. C API棰勫鐞嗚鍙ュ嚱鏁版弿杩

涓轰簡鍑嗗鍜屾墽琛屾煡璇紝璇蜂娇鐢ㄤ笅杩伴儴鍒嗚缁嗕粙缁嶇殑鍑芥暟銆

娉ㄦ剰锛屼笌MYSQL_STMT缁撴瀯涓璧蜂娇鐢ㄧ殑鎵鏈夊嚱鏁板潎浠ュ墠缂mysql_stmt_寮濮嬨

瑕佹兂鍒涘缓MYSQL_STMT鍙ユ焺锛岃浣跨敤mysql_stmt_init()鍑芥暟銆

25.2.7.1.聽mysql_stmt_affected_rows()

my_ulonglong mysql_stmt_affected_rows(MYSQL_STMT *stmt)

鎻忚堪

杩斿洖涓婃鎵ц璇彞鏇存敼銆佸垹闄ゆ垨鎻掑叆鐨勬昏鏁般傚浜UPDATEDELETEINSERT璇彞锛屽彲鍦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()鈥涓粰鍑虹殑绀轰緥銆

25.2.7.2.聽mysql_stmt_attr_get()

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鍊笺

閿欒

鏃犮

25.2.7.3.聽mysql_stmt_attr_set()

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鐨勮寖鍥翠粠1unsigned 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 ... */

25.2.7.4.聽mysql_stmt_bind_param()

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()鈥缁欏嚭鐨勭ず渚嬨

25.2.7.5.聽mysql_stmt_bind_result()

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()鈥涓粰鍑虹殑绀轰緥銆

25.2.7.6.聽mysql_stmt_close()

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()鈥涓粰鍑虹殑绀轰緥銆

25.2.7.7.聽mysql_stmt_data_seek()

void mysql_stmt_data_seek(MYSQL_STMT *stmt, my_ulonglong offset)

鎻忚堪

鏌ユ壘璇彞缁撴灉闆嗕腑鐨勪换鎰忚銆傚亸绉婚噺涓鸿缂栧彿锛屽簲浣嶄簬浠0mysql_stmt_num_rows(stmt)-1鐨勮寖鍥村唴銆

璇ュ嚱鏁拌姹傝鍙ョ粨鏋滈泦缁撴瀯鍖呭惈涓婃鎵ц鏌ヨ鐨勫叏閮ㄧ粨鏋滐紝杩欐牱锛mysql_stmt_data_seek()灏辫兘涓mysql_stmt_store_result()涓璧蜂娇鐢ㄣ

杩斿洖鍊

鏃犮

閿欒

鏃犮

25.2.7.8.聽mysql_stmt_errno()

unsigned int mysql_stmt_errno(MYSQL_STMT *stmt)

鎻忚堪

瀵逛簬鐢stmt鎸囧畾鐨勮鍙ワ紝mysql_stmt_errno()灏嗚繑鍥炴渶杩戣皟鐢ㄧ殑璇彞API鍑芥暟鐨勯敊璇唬鐮侊紝璇ュ嚱鏁版垨鎴愬姛鎴栧け璐ャ0鈥濊繑鍥炲艰〃绀烘湭鍑虹幇閿欒銆傚湪MySQL errmsg.h澶存枃浠朵腑鍒楀嚭浜嗗鎴风閿欒娑堟伅缂栧彿銆傚湪mysqld_error.h涓紝鍒楀嚭浜嗘湇鍔″櫒閿欒娑堟伅銆姝ゅ锛屽湪闄勫綍B锛閿欒浠g爜鍜屾秷鎭涓紝涔熷垪鍑轰簡閿欒娑堟伅銆

杩斿洖鍊

閿欒浠g爜鍊笺傚鏋滄湭鍑虹幇閿欒锛岃繑鍥0

閿欒

鏃犮

25.2.7.9.聽mysql_stmt_error()

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瀹㈡埛绔簱锛屽彲鏇存敼瀹㈡埛绔敊璇秷鎭殑璇█銆傜洰鍓嶏紝鑳藉閫夋嫨鏁扮璇█涔嬩竴鏄剧ず閿欒娑堟伅銆

杩斿洖鍊

鎻忚堪浜嗛敊璇殑瀛楃涓层傚鏋滄湭鍑虹幇閿欒锛岃繑鍥炵┖瀛楃涓层

閿欒

鏃犮

25.2.7.10.聽mysql_stmt_execute()

int mysql_stmt_execute(MYSQL_STMT *stmt)

鎻忚堪

mysql_stmt_execute()鎵ц涓庤鍙ュ彞鏌勭浉鍏崇殑棰勫鐞嗘煡璇€傚湪璇ヨ皟鐢ㄦ湡闂达紝灏嗗綋鍓嶇粦瀹氱殑鍙傛暟鏍囪绗︾殑鍊煎彂閫佸埌鏈嶅姟鍣紝鏈嶅姟鍣ㄧ敤鏂版彁渚涚殑鏁版嵁鏇挎崲鏍囪绗︺

濡傛灉璇彞鏄UPDATEDELETEINSERT锛岄氳繃璋冪敤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婧愮爜浠撳簱鑾峰緱銆

25.2.7.11.聽mysql_stmt_fetch()

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_DATEMYSQL_TYPE_TIMEMYSQL_TYPE_DATETIME銆佹垨MYSQL_TYPE_TIMESTAMP锛屼絾鏁版嵁绫诲瀷涓嶆槸DATETIMEDATETIME銆佹垨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);
}

25.2.7.12.聽mysql_stmt_fetch_column()

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鍒楃紪鍙蜂负0offset鈥鏄暟鎹煎唴鐨勫亸绉婚噺锛屽皢浠庤澶勫紑濮嬫绱㈡暟鎹傚彲灏嗗叾鐢ㄤ簬鑾峰彇纰庣墖褰㈠紡鐨勬暟鎹笺鍊煎紑濮嬮儴鍒嗙殑鍋忕Щ閲忎负0

杩斿洖鍊

濡傛灉鎴愬姛鑾峰彇浜嗗硷紝杩斿洖0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺

閿欒

         CR_INVALID_PARAMETER_NO

Invalid column number.

         CR_NO_DATA

宸叉姷杈剧粨鏋滈泦鐨勬湯灏俱

25.2.7.13.聽mysql_stmt_field_count()

unsigned int mysql_stmt_field_count(MYSQL_STMT *stmt)

鎻忚堪

涓鸿鍙ュ鐞嗙▼搴忚繑鍥炲叧浜庢渶杩戣鍙ョ殑琛屾暟銆傚浜庤濡INSERTDELETE绛変笉鐢熸垚缁撴灉闆嗙殑璇彞锛岃鍊间负0

閫氳繃璋冪敤mysql_stmt_prepare()鍑嗗濂戒簡璇彞鍚庯紝鍙皟鐢mysql_stmt_field_count()

杩斿洖鍊

琛ㄧず缁撴灉闆嗕腑琛屾暟鐨勬棤绗﹀彿鏁存暟銆

閿欒

鏃犮

25.2.7.14.聽mysql_stmt_free_result()

my_bool mysql_stmt_free_result(MYSQL_STMT *stmt)

鎻忚堪

閲婃斁涓庢墽琛岄澶勭悊璇彞鐢熸垚鐨勭粨鏋滈泦鏈夊叧鐨勫唴瀛樸傚浜庤璇彞锛屽鏋滃瓨鍦ㄦ墦寮鐨勫厜鏍囷紝mysql_stmt_free_result()灏嗗叧闂畠銆

杩斿洖鍊

濡傛灉鎴愬姛閲婃斁浜嗙粨鏋滈泦锛岃繑鍥0銆傚鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺

閿欒

25.2.7.15.聽mysql_stmt_init()

MYSQL_STMT *mysql_stmt_init(MYSQL *mysql)

鎻忚堪

鍒涘缓MYSQL_STMT鍙ユ焺銆瀵逛簬璇ュ彞鏌勶紝搴斾娇鐢mysql_stmt_close(MYSQL_STMT *)閲婃斁銆

杩斿洖鍊

鎴愬姛鏃讹紝杩斿洖鎸囧悜MYSQL_STMT缁撴瀯鐨勬寚閽銆傚鏋滃唴瀛樻孩鍑猴紝杩斿洖NULL

閿欒

         CR_OUT_OF_MEMORY

鍐呭瓨婧㈠嚭銆

25.2.7.16.聽mysql_stmt_insert_id()

my_ulonglong mysql_stmt_insert_id(MYSQL_STMT *stmt)

鎻忚堪

杩斿洖棰勫鐞INSERTUPDATE璇彞AUTO_INCREMENT鍒楃敓鎴愮殑鍊笺傚湪鍖呭惈AUTO_INCREMENT瀛楁鐨勮〃涓婃墽琛屼簡棰勫鐞INSERT璇彞鍚庯紝浣跨敤璇ュ嚱鏁般

鏇村淇℃伅锛岃鍙傝25.2.3.36鑺傦紝鈥渕ysql_insert_id()鈥

杩斿洖鍊

涓哄湪鎵ц棰勫鐞嗚鍙ユ湡闂磋嚜鍔ㄧ敓鎴愭垨鏄庣‘璁剧疆鐨AUTO_INCREMENT鍒楄繑鍥炲硷紝鎴LAST_INSERT_ID(expr)鍑芥暟鐢熸垚鐨勫笺傚鏋滆鍙ユ湭璁剧疆AUTO_INCREMENT鍊硷紝杩斿洖鍊间笉纭畾銆

閿欒

鏃犮

25.2.7.17.聽mysql_stmt_num_rows()

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()

杩斿洖鍊

缁撴灉闆嗕腑鐨勮鏁般

閿欒

鏃犮

25.2.7.18.聽mysql_stmt_param_count()

unsigned long mysql_stmt_param_count(MYSQL_STMT *stmt)

鎻忚堪

杩斿洖棰勫鐞嗚鍙ヤ腑鍙傛暟鏍囪绗︾殑鏁扮洰銆

杩斿洖鍊

琛ㄧず璇彞涓弬鏁版暟鐩殑鏃犵鍙烽暱鏁存暟銆

閿欒

鏃犮

绀轰緥锛

鍏充簬mysql_stmt_param_count()鐨勭敤娉曪紝璇峰弬瑙25.2.7.10鑺傦紝鈥渕ysql_stmt_execute()鈥涓粰鍑虹殑绀轰緥銆

25.2.7.19.聽mysql_stmt_param_metadata()

MYSQL_RES *mysql_stmt_param_metadata(MYSQL_STMT *stmt)

璇ュ嚱鏁扮洰鍓嶄笉鍋氫换浣曚簨銆

鎻忚堪

杩斿洖鍊

閿欒

25.2.7.20.聽mysql_stmt_prepare()

int mysql_stmt_prepare(MYSQL_STMT *stmt, const char *query, unsigned long length)

鎻忚堪

缁欏畾mysql_stmt_init()杩斿洖鐨勮鍙ュ彞鏌勶紝鍑嗗瀛楃涓叉煡璇㈡寚鍚戠殑SQL璇彞锛屽苟杩斿洖鐘舵佸笺傚瓧绗︿覆闀垮害搴旂敱鈥length鈥濆弬閲忕粰鍑恒傚瓧绗︿覆蹇呴』鍖呭惈1SQL璇彞銆備笉搴斾负璇彞娣诲姞缁堢粨鐢ㄥ垎鍙(;)\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()鈥涓粰鍑虹殑绀轰緥銆

25.2.7.21.聽mysql_stmt_reset()

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

鍑虹幇鏈煡閿欒銆

25.2.7.22.聽mysql_stmt_result_metadata()

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()鈥涓粰鍑虹殑绀轰緥銆

25.2.7.23.聽mysql_stmt_row_seek()

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()璋冪敤銆

閿欒

鏃犮

25.2.7.24.聽mysql_stmt_row_tell()

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()

杩斿洖鍊

琛屽厜鏍囩殑褰撳墠鍋忕Щ閲忋

閿欒

鏃犮

25.2.7.25.聽mysql_stmt_send_long_data()

my_bool mysql_stmt_send_long_data(MYSQL_STMT *stmt, unsigned int parameter_number, const char *data, unsigned long length)

鎻忚堪

鍏佽搴旂敤绋嬪簭鍒嗘鍦帮紙鍒嗗潡锛夊皢鍙傛暟鏁版嵁鍙戦佸埌鏈嶅姟鍣ㄣ傚彲浠ュ娆¤皟鐢ㄨ鍑芥暟锛屼互渚垮彂閫佸叧浜庢煇涓鍒楃殑瀛楃鎴栦簩杩涘埗鏁版嵁鐨勪笉鍚岄儴鍒嗭紝鍒楀繀椤绘槸TEXTBLOB鏁版嵁绫诲瀷涔嬩竴銆

鈥減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);
}

25.2.7.26.聽mysql_stmt_sqlstate()

const char *mysql_stmt_sqlstate(MYSQL_STMT *stmt)

鎻忚堪

瀵逛簬鐢stmt鎸囧畾鐨勮鍙ワ紝mysql_stmt_sqlstate()杩斿洖鐢Null缁堢粨鐨勫瓧绗︿覆锛岃瀛楃涓插寘鍚拡瀵规渶杩戣皟鐢ㄩ澶勭悊璇彞API鍑芥暟鐨SQLSTATE閿欒浠g爜锛岃鍑芥暟鎴栨垚鍔熸垨澶辫触銆傞敊璇唬鐮佺敱5涓瓧绗︽瀯鎴愩"00000"琛ㄧず鈥滄棤閿欒鈥濄傝繖浜涘肩敱ANSI SQLODBC鎸囧畾銆傚叧浜庡彲鑳藉肩殑鍒楄〃锛岃鍙傝闄勫綍B锛氶敊璇唬鐮佸拰娑堟伅

娉ㄦ剰锛屽苟闈炴墍鏈夌殑MySQL閿欒鍧囦細琚槧灏勫埌SQLSTATE浠g爜銆傚"HY000"锛堜竴鑸敊璇級鐢ㄤ簬鏈槧灏勭殑閿欒銆

杩斿洖鍊

鍖呭惈SQLSTATE閿欒浠g爜銆佺敱Null缁堢粨鐨勫瓧绗︿覆銆

25.2.7.27.聽mysql_stmt_store_result()

int mysql_stmt_store_result(MYSQL_STMT *stmt)

鎻忚堪

瀵逛簬鎴愬姛鐢熸垚缁撴灉闆嗙殑鎵鏈夎鍙ワ紙SELECTSHOWDESCRIBEEXPLAIN锛夛紝鑰屼笖浠呭綋浣犳墦绠楀瀹㈡埛绔殑鍏ㄩ儴缁撴灉闆嗚繘琛岀紦鍐插鐞嗘椂锛屽繀椤昏皟鐢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

鍑虹幇鏈煡閿欒銆

25.2.8. C API棰勫鐞嗚鍙ユ柟闈㈢殑闂

涓嬮潰鍒楀嚭浜嗕竴浜涚洰鍓嶅凡鐭ョ殑涓庨澶勭悊璇彞鏈夊叧鐨勯棶棰橈細

         TIMETIMESTAMPDATETIME涓嶆敮鎸佺閮ㄥ垎锛屼緥濡傛潵鑷DATE_FORMAT()鐨勭閮ㄥ垎

         灏嗘暣鏁拌浆鎹负瀛楃涓叉椂锛屽湪鏌愪簺鎯呭喌涓嬶紝褰MySQL涓嶆墦鍗板墠瀵0鏃讹紝鍙笌棰勫鐞嗚鍙ヤ竴璧蜂娇鐢ZEROFILL銆備緥濡傦紝涓MIN(number-with-zerofill)涓璧枫

         灏嗘诞鐐规暟杞崲涓哄鎴风涓殑瀛楃涓叉椂锛岃杞崲鍊兼渶鍙充晶鐨勪綅鍙兘浼氫笌鍘熷鍊肩殑鏈夋墍涓嶅悓銆

         棰勫鐞嗚鍙ヤ笉浣跨敤鏌ヨ楂橀熺紦鍐诧紝鍗充娇褰撴煡璇笉鍚换浣曞崰浣嶇鏃朵篃鍚屾牱銆傘璇峰弬瑙5.13.1鑺傦紝鈥滄煡璇㈤珮閫熺紦鍐插浣曞伐浣溾

25.2.9. 澶氭煡璇㈡墽琛岀殑C API澶勭悊

MySQL 5.1鏀寔鍦ㄥ崟涓煡璇㈠瓧绗︿覆涓寚瀹氱殑澶氳鍙ョ殑鎵ц銆傝鎯充笌缁欏畾鐨勮繛鎺ヤ竴璧蜂娇鐢ㄨ鍔熻兘锛屾墦寮杩炴帴鏃讹紝蹇呴』灏嗘爣蹇楀弬鏁颁腑鐨CLIENT_MULTI_STATEMENTS閫夐」鎸囧畾缁mysql_real_connect()涔熷彲浠ラ氳繃璋冪敤mysql_set_server_option(MYSQL_OPTION_MULTI_STATEMENTS_ON)锛屼负宸叉湁鐨勮繛鎺ヨ缃畠銆

鍦ㄩ粯璁ゆ儏鍐典笅锛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()涓璧蜂娇鐢ㄣ傚畠涓嶈兘涓庨澶勭悊璇彞鎺ュ彛涓璧蜂娇鐢ㄣ傛寜鐓у畾涔夛紝棰勫鐞嗚鍙ヤ粎鑳戒笌鍖呭惈鍗曚釜璇彞鐨勫瓧绗︿覆涓璧蜂娇鐢ㄣ

25.2.10. 鏃ユ湡鍜屾椂闂村肩殑C API澶勭悊

浜岃繘鍒跺崗璁厑璁镐綘浣跨敤MYSQL_TIME缁撴瀯鍙戦佸拰鎺ュ彈鏃ユ湡鍜屾椂闂村硷紙DATETIMEDATETIMETIMESTAMP锛夈25.2.5鑺傦紝鈥淐 API棰勫鐞嗚鍙ョ殑鏁版嵁绫诲瀷鈥涓紝浠嬬粛浜嗚缁撴瀯鐨勬垚鍛樸

瑕佹兂鍙戦佷复鏃舵暟鎹硷紝鍙娇鐢mysql_stmt_prepare()鍒涘缓棰勫鐞嗚鍙ャ傜劧鍚庯紝鍦ㄨ皟鐢mysql_stmt_execute()鎵ц璇彞涔嬪墠锛屽彲閲囩敤涓嬭堪姝ラ璁剧疆姣忎釜涓存椂鍙傛暟锛

1.    鍦ㄤ笌鏁版嵁鍊肩浉鍏崇殑MYSQL_BIND缁撴瀯涓紝灏buffer_type鎴愬憳璁剧疆涓虹浉搴旂殑绫诲瀷锛岃绫诲瀷鎸囨槑浜嗗彂閫佺殑涓存椂鍊肩被鍨嬨瀵逛簬DATETIMEDATETIMETIMESTAMP鍊硷紝buffer_type鍒嗗埆璁剧疆涓MYSQL_TYPE_DATEMYSQL_TYPE_TIMEMYSQL_TYPE_DATETIMEMYSQL_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()灏嗙紦鍐茬粦瀹氬埌璇彞涓娿

涓嬮潰缁欏嚭浜嗕竴涓彃鍏DATETIMETIMESTAMP鏁版嵁鐨勭畝鍗曠ず渚銆傚亣瀹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.11. C API绾跨▼鍑芥暟浠嬬粛

褰撲綘鎵撶畻鍒涘缓绾跨▼瀹㈡埛绔椂锛岄渶瑕佷娇鐢ㄤ笅杩板嚱鏁般傝鍙傝25.2.15鑺傦紝鈥滃浣曠敓鎴愮嚎绋嬪紡瀹㈡埛绔

25.2.11.1.聽my_init()

void my_init(void)

鎻忚堪

璋冪敤浠讳綍MySQL鍑芥暟涔嬪墠锛岄渶瑕佸湪绋嬪簭涓皟鐢ㄨ鍑芥暟銆傚畠灏嗗垵濮嬪寲MySQL鎵闇鐨勬煇浜涘叏灞鍙橀噺銆傚鏋滀綘姝e湪浣跨敤绾跨▼瀹夊叏瀹㈡埛绔簱锛屽畠杩樿兘涓鸿绾跨▼璋冪敤mysql_thread_init()

閫氳繃mysql_init()mysql_library_init()mysql_server_init()mysql_connect()锛屽彲鑷姩璋冪敤璇ュ嚱鏁般

杩斿洖鍊

鏃犮

25.2.11.2.聽mysql_thread_init()

my_bool mysql_thread_init(void)

鎻忚堪

瀵逛簬姣忎釜鍒涘缓鐨勭嚎绋嬶紝闇瑕佽皟鐢ㄨ鍑芥暟鏉ュ垵濮嬪寲涓庣嚎绋嬬浉鍏崇殑鍙橀噺銆

瀹冨彲鐢my_init()mysql_connect()鑷姩璋冪敤銆

杩斿洖鍊

濡傛灉鎴愬姛锛岃繑鍥0锛屽鏋滃嚭鐜伴敊璇紝杩斿洖闈0鍊笺

25.2.11.3.聽mysql_thread_end()

void mysql_thread_end(void)

鎻忚堪

璋冪敤pthread_exit()鏉ラ噴鏀mysql_thread_init()鍒嗛厤鐨勫唴瀛樹箣鍓嶏紝闇瑕佽皟鐢ㄨ鍑芥暟銆

娉ㄦ剰锛岃鍑芥暟涓嶄細琚鎴风搴撹嚜鍔ㄨ皟鐢ㄣ傚繀椤绘槑纭皟鐢ㄥ畠浠ラ伩鍏嶅唴瀛樻硠婕忋

杩斿洖鍊

鏃犮

25.2.11.4.聽mysql_thread_safe()

unsigned int mysql_thread_safe(void)

鎻忚堪

璇ュ嚱鏁版寚鏄庝簡瀹㈡埛绔槸鍚︾紪璇戜负绾跨▼瀹夊叏鐨勩

杩斿洖鍊

濡傛灉瀹㈡埛绔槸绾跨▼瀹夊叏鐨勶紝杩斿洖1锛屽惁鍒欒繑鍥0

25.2.12. C API宓屽叆寮忔湇鍔″櫒鍑芥暟浠嬬粛

濡傛灉甯屾湜鍏佽搴旂敤绋嬪簭閾炬帴鍒板祵鍏ュ紡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绗﹀彿锛屼絾瀹冧滑鐨勫悕绉版洿娓呮鍦版寚鏄庯紝鏃犺搴旂敤绋嬪簭浣跨敤鐨勬槸libmysqlclientlibmysqld锛屽紑濮嬩娇鐢ㄦ垨缁撴潫MySQL C API搴撶殑浣跨敤鏃讹紝搴旇皟鐢ㄥ畠浠傚叧浜庢洿澶氫俊鎭紝璇峰弬瑙25.2.2鑺傦紝鈥淐 API鍑芥暟姒傝堪鈥

25.2.12.1.聽mysql_server_init()

int mysql_server_init(int argc, char **argv, char **groups)

鎻忚堪

璋冪敤浠讳綍鍏朵粬MySQL鍑芥暟涔嬪墠锛屽繀椤诲湪浣跨敤宓屽叆寮忔湇鍔″櫒鐨勭▼搴忎腑璋冪敤璇ュ嚱鏁般傚畠灏嗗惎鍔ㄦ湇鍔″櫒锛屽苟鍒濆鍖栨湇鍔″櫒浣跨敤鐨勪换浣曞瓙绯荤粺锛mysysInnoDB绛夛級銆傚鏋滄湭璋冪敤璇ュ嚱鏁帮紝瀵mysql_init()鐨勪笅涓娆¤皟鐢ㄥ皢鎵цmysql_server_init()銆傚鏋滀綘姝e湪浣跨敤涓MySQL涓璧锋彁渚涚殑DBUG杞欢鍖咃紝搴斿湪璋冪敤浜my_init()涔嬪悗璋冪敤瀹冦

瀵逛簬main()鐨勫弬閲忥紝argcargv鏄被浼肩殑鍙傞噺銆argv鐨勭1涓厓绱犲皢琚拷鐣锛堝吀鍨嬫儏鍐典笅锛屽畠鍖呭惈绋嬪簭鍚嶏級銆備负浜嗘柟渚胯捣瑙侊紝濡傛灉娌℃湁閽堝鏈嶅姟鍣ㄧ殑鍛戒护琛屽弬閲忥紝argc鍙互鏄0mysql_server_init()灏嗗鍒跺弬閲忥紝浠ヤ究鑳藉鍦ㄨ皟鐢ㄤ箣鍚庡畨鍏ㄥ湴鎽ф瘉argvgroups

濡傛灉鎵撶畻杩炴帴鍒板閮ㄦ湇鍔″櫒鑰屼笉鍚姩宓屽叆寮忔湇鍔″櫒锛屽簲涓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

25.2.12.2.聽mysql_server_end()

void mysql_server_end(void)

鎻忚堪

鍦ㄦ墍鏈夊叾浠MySQL鍑芥暟鍚庯紝鍦ㄧ▼搴忎腑蹇呴』璋冪敤璇ュ嚱鏁颁竴娆°傚畠灏嗗叧闂祵鍏ュ紡鏈嶅姟鍣ㄣ

杩斿洖鍊

鏃犮

25.2.13. 浣跨敤C API鏃剁殑甯歌闂

25.2.13.1. 涓轰粈涔堝湪mysql_query()杩斿洖鎴愬姛鍚庯紝mysql_store_result()鏈夋椂浼氳繑鍥濶ULL

鎴愬姛璋冪敤mysql_query()鍚庯紝mysql_store_result()鑳藉杩斿洖NULL銆傚嚭鐜拌鎯呭喌鏃讹紝琛ㄦ槑鍑虹幇浜嗕笅杩版潯浠朵箣涓锛

         鍑虹幇浜malloc()鏁呴殰锛堜緥濡傦紝濡傛灉缁撴灉闆嗚繃澶锛夈

         鏃犳硶璇诲彇鏁版嵁锛堝湪杩炴帴涓婂嚭鐜颁簡閿欒锛夈

         鏌ヨ鏈繑鍥炴暟鎹紙渚嬪锛屽畠鏄INSERTUPDATEDELETE锛夈

閫氳繃璋冪敤mysql_field_count()濮嬬粓鑳芥鏌ヨ鍙ユ槸鍚﹀簲鐢熸垚闈炵┖缁撴灉銆傚鏋mysql_field_count()杩斿洖0锛岀粨鏋滀负绌猴紝鑰屼笖涓婁竴涓煡璇鏄湭杩斿洖鍊肩殑璇彞锛堜緥濡INSERTDELETE锛夈傚鏋mysql_field_count()杩斿洖闈0鍊硷紝璇彞搴旂敓鎴愰潪绌虹粨鏋銆傚叧浜庤繖鏂归潰鐨勭ず渚嬶紝璇峰弬瑙mysql_field_count()鍑芥暟浠嬬粛銆

閫氳繃璋冪敤mysql_error()mysql_errno()鍙祴璇曟槸鍚﹀嚭鐜颁簡閿欒銆

25.2.13.2.聽What Results You Can Get from a Query

闄や簡鏌ヨ杩斿洖鐨勭粨鏋滈泦澶栵紝杩樿兘鑾峰彇涓嬭堪淇℃伅锛

         鎵цINSERTUPDATEDELETE鏃讹紝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鎸囬拡銆

25.2.13.3. 濡備綍鑾峰緱涓婃鎻掑叆琛岀殑鍞竴ID

濡傛灉灏嗚褰曟彃鍏ュ寘鍚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鍊硷紙鍗抽潪Null0鍊硷級鏇存柊浜嗗彟涓涓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鍒椾腑鐨勫硷紝鏃犺璇ュ兼槸鍥犲瓨鍌NULL0鑰岃嚜鍔ㄧ敓鎴愮殑锛屾垨鏄槑纭寚瀹氱殑锛屽潎濡傛銆LAST_INSERT_ID()浠呰繑鍥炶嚜鍔ㄧ敓鎴愮殑AUTO_INCREMENT鍊笺濡傛灉浣犱繚瀛樹簡闄NULL0涔嬪鐨勭‘鍒囧硷紝涓嶄細褰卞搷LAST_INSERT_ID()杩斿洖鐨勫笺

25.2.13.4. 涓嶤 API鏈夊叧鐨勯棶棰

C API閾炬帴鏃讹紝鍦ㄦ煇浜涚郴缁熶笂鍙兘鍑虹幇涓嬭堪閿欒锛
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鈥濓紝閫氳繃璇ユ柟寮忓寘鍚暟瀛﹀簱銆

25.2.14. 鍒涘缓瀹㈡埛绔▼搴

濡傛灉浣犵紪璇戜簡鑷繁缂栧啓鐨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涓瘝瀵瑰緟銆

25.2.15. 濡備綍鐢熸垚绾跨▼寮忓鎴风

瀹㈡埛绔簱鎬绘槸绾跨▼瀹夊叏鐨勩傛渶澶х殑闂鍦ㄤ簬浠庡鎺ュ瓧璇诲彇鐨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琛屼笂銆

25.3.聽MySQL PHP API

PHP鏄竴绉嶆湇鍔″櫒绔HTML宓屽叆寮忚剼鏈鐞嗚瑷锛屽彲浣跨敤璇ヨ瑷鍒涘缓鍔ㄦ佺綉椤点傚畠鍙敤浜庡ぇ澶氭暟鎿嶄綔绯荤粺鍜Web鏈嶅姟鍣紝涔熻兘璁块棶澶у鏁板父瑙佹暟鎹簱锛屽寘鎷MySQLPHP鍙互浣滀负鍗曠嫭绋嬪簭杩愯锛屼篃鑳界紪璇戜负妯″潡锛岀敤浜Apache Web鏈嶅姟鍣ㄣ

PHP瀹為檯涓婃彁渚涗簡涓ょ涓嶅悓鐨MySQL API鎵╁睍锛

         mysql锛氶傜敤浜PHP鐗堟湰45锛岃鎵╁睍鐢ㄤ簬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缃戠珯鑾峰緱銆

25.3.1. 浣跨敤MySQL鍜孭HP鐨勫父瑙侀棶棰

         閿欒锛氳秴鍑轰簡鏈澶ф墽琛屾椂闂达紝杩欐槸涓绉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鑺傦紝鈥滃鎴风涓嶆敮鎸侀壌瀹氬崗璁锛夈

25.4.聽MySQL Perl API

Perl DBI妯″潡涓烘暟鎹簱璁块棶鎻愪緵浜嗕竴涓氱敤鎺ュ彛銆鑳藉缂栧啓鏃犻渶鏇存敼灏辫兘涓庝笉鍚岀殑鏁版嵁搴撳紩鎿庝竴璧峰伐浣滅殑DBI鑴氭湰銆傝鎯充娇鐢DBI锛屽繀椤诲畨瑁DBI妯″潡锛屽苟涓烘墦绠楄闂殑姣忕鏈嶅姟鍣ㄥ畨瑁呮暟鎹簱椹卞姩绋嬪簭锛DBD锛夋ā鍧椼傚浜MySQL锛岃椹卞姩绋嬪簭鏄DBD::mysql妯″潡銆

Perl DBI鏄帹鑽愮殑Perl鎺ュ彛銆傚畠鍙栦唬浜嗘棫鐨勫悕涓mysqlperl鐨勬帴鍙o紝mysqlperl宸茶繃鏃躲

鍏充簬Perl DBI鏀寔鐨勫畨瑁呰鏄庯紝璇峰弬瑙2.13鑺傦紝鈥淧erl瀹夎娉ㄦ剰浜嬮」鈥

DBI淇℃伅鑳藉鍦ㄥ懡浠よ涓婃彁渚涳紝涔熻兘浠ュ湪绾挎柟寮忔彁渚涳紝鎴栭噰鐢ㄥ嵃鍒峰舰寮忥細

         涓鏃﹀畨瑁呬簡DBIDBD::mysql妯″潡锛屽彲浣跨敤perldoc 鍛戒护鍦ㄥ懡浠よ涓婅幏鍙栧叧浜庡畠浠殑淇℃伅锛

                shell> perldoc DBI
                shell> perldoc DBI::FAQ
                shell> perldoc DBD::mysql

涔熷彲浠ヤ娇鐢pod2manpod2html灏嗚繖绫讳俊鎭浆鎹负鍏朵粬鏍煎紡銆

         鍏充簬Perl DBI鐨勫湪绾夸俊鎭紝璇疯闂DBI缃戠珯锛http://dbi.perl.org/銆傝绔欑偣杩樻彁渚涗簡1涓竴鑸DBI閭欢鍒楄〃銆MySQL AB鎻愪緵浜1涓笓闂ㄩ拡瀵DBD::mysql 鐨勯偖浠跺垪琛紝璇峰弬瑙1.7.1.1 MySQL閭欢鍒楄〃鈥

         鑷充簬鍗板埛鐗堜俊鎭紝瀹樻柟鐨DBI涔︾睄鏄缂栫▼Perl DBIAlligator DescartesTim BunceO'Reilly & Associates, 2000锛夈傚叧浜庤涔︾殑淇℃伅锛岃璁块棶DBI缃戠珯http://dbi.perl.org/

鍏充簬涓MySQL涓璧蜂娇鐢DBI鐨勪笓闂ㄤ俊鎭紝璇峰弬瑙閽堝WebMySQLPerlPaul DuBois, New Riders, 2001锛夈傝涔︾殑缃戠珯鏄http://www.kitebird.com/mysql-perl/

25.5.聽MySQL C++ API

MySQL++鏄敤浜C++MySQL APIWarren Young璐熻矗璇ラ」鐩傝鎯充簡瑙f洿澶氫俊鎭紝璇疯闂http://www.mysql.com/products/mysql++/

25.5.1.聽Borland C++

鍙互浣跨敤Borland C++ 5.02缂栬瘧MySQL Windows婧愮爜锛Windows婧愮爜浠呭寘鎷敤浜Microsoft VC++鐨勯」鐩紝瀵逛簬Borland C++锛屼綘灏嗕笉寰椾笉鑷繁缂栧埗椤圭洰鏂囦欢锛夈

浣跨敤Borland C++鏃剁殑1涓凡鐭ラ棶棰樻槸锛屽畠閲囩敤浜嗕笉鍚屼簬VC++鐨勭粨鏋勫榻愭柟寮忋傝繖鎰忓懗鐫锛屽鏋滀綘鎵撶畻涓Borland C++涓璧蜂娇鐢ㄩ粯璁ょ殑libmysql.dll搴擄紙瀹冩槸浣跨敤VC++缂栬瘧鐨勶級锛屽皢浼氶亣鍒伴棶棰樸備负浜嗛伩鍏嶈闂锛屼粎搴旇皟鐢ㄥ皢Null浣滀负鍙傞噺鐨mysql_init()锛岃屼笉鏄鍏堝垎閰MYSQL缁撴瀯銆

25.6.聽MySQL Python API

MySQLdbPython鎻愪緵浜MySQL鏀寔锛屽畠绗﹀悎Python DB API鐗堟湰2.0鐨勮姹傦紝鍙湪http://sourceforge.net/projects/mysql-python/涓婃壘鍒板畠銆

25.7.聽MySQL Tcl API

MySQLtcl鏄竴绉嶇畝鍗曠殑API锛岀敤浜庝粠Tcl缂栫▼璇█璁块棶MySQL鏁版嵁搴撴湇鍔″櫒銆傚彲鍦http://www.xdobry.de/mysqltcl/涓婃壘鍒板畠銆

25.8.聽MySQL Eiffel Wrapper

Eiffel MySQL鏄竴绉嶄笌MySQL鏁版嵁搴撴湇鍔″櫒鐨勬帴鍙o紝瀹冮噰鐢ㄧ殑鏄Eiffel缂栫▼璇█锛岀敱Michael Ravits缂栧啓銆傚彲鍦http://efsa.sourceforge.net/archive/ravits/mysql.htm涓婃壘鍒板畠銆

25.9. MySQL绋嬪簭寮鍙戝疄鐢ㄥ伐鍏

鍦ㄦ湰鑺備腑锛屼粙缁嶄簡寮鍙MySQL绋嬪簭鏃跺彲鑳戒細鏈夌敤鐨勪竴浜涘疄鐢ㄥ伐鍏枫

         msql2mysql

1shell鑴氭湰锛岀敤浜庡皢mSQL绋嬪簭杞崲涓MySQL绋嬪簭銆傚畠涓嶈兘澶勭悊鎵鏈夋儏鍐碉紝浣嗚兘涓鸿浆鎹㈡彁渚涜壇濂界殑寮绔

         mysql_config

1shell鑴氭湰锛岃兘鐢熸垚缂栬瘧MySQL绋嬪簭鏃舵墍闇鐨勯夐」鍊笺

25.9.1. msql2mysql锛氳浆鎹SQL绋嬪簭浠ョ敤浜嶮ySQL

鏈鍒濓紝寮鍙戠殑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锛氬瓧绗︿覆鏇挎崲瀹炵敤宸ュ叿鈥

25.9.2. mysql_config锛氳幏鍙栫紪璇戝鎴风鐨勭紪璇戦夐」

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銆 鍘熷鍙傝冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆