安裝mysql
? ? 1地熄、brew install mysql
? ? 2兰珍、mysql_secure_installation 修改密碼
? ? 3、mysql -u root -p? 登陸
編輯node-gyp binding.gyp
? ? 1、brew --prefix mysql 獲取安裝目錄
????2欣范、include_dirs 添加?
?????????????"/usr/local/Cellar/mysql/8.0.17/include"
? ? ? ? ? ? " /usr/local/Cellar/mysql/8.0.17/include/mysql"
? ? 3、libraries 添加??"/usr/local/Cellar/mysql/8.0.17/lib/libmysqlclient.a"
? ? 4令哟、node-gyp?configure
? ? 5恼琼、node-gyp?build
? ? 6、node-gyp configure -- -f? xcode
代碼
? ? ?1励饵、引入頭文件?
????????????????#include <mysql/mysql.h>
? ? 2驳癌、初始化鏈接
? ??????????MYSQL*conn;
????3、node addon init創(chuàng)建mysql connection
? ??????????std::stringserver ="localhost";
? ? ????????std::stringuser ="root";
? ????????? std::stringpassword ="****";
? ????????? std::stringdatebase ="testdb";
? ????????? conn=mysql_init(NULL);
? ? ????????if(!mysql_real_connect(conn, server.c_str(), user.c_str(), password.c_str(), datebase.c_str(),0,NULL,0)) {
? ? ? ????????????? std::cout<<"connect to database error"<
? ? ? ? ????????????exit(1);
? ????????? }
????4役听、node::AtExit關(guān)閉mysql 鏈接
? ??????????node::AtExit(atExitCallback);
? ??????????staticvoidatExitCallback(void* arg) {
? ? ????????????mysql_close(conn);
? ? ????????????std::cout<<"close conn"<
????????????}
? ? 5颓鲜、執(zhí)行查詢
? ??????????void RunCallback(const Nan::FunctionCallbackInfo<Value> &info) {
????????????? ? MYSQL_RES* res;
? ? ????????????MYSQL_ROW row;
? ? ????????????if (mysql_query(conn, "SELECT * FROM Cars")) {
? ? ? ? ????????????????std::cout<<"send SQL query"<< std:endl;
? ? ? ? ? ? ? ? ? ?}
? ? ????????????res = mysql_use_result(conn);
? ? ????????????while((row =mysql_fetch_row(res)) !=NULL) {
? ? ? ? ????????????????std::cout<< row[0] <<','<< row[1] <<','<< row[2] <
? ? ????????????}
? ????????????? mysql_free_result(res);
????????}