[問題] VC++存資料進MySQL如何使用VC++中的變數

看板C_and_CPP作者 (ChingYue)時間8年前 (2017/04/19 11:55), 8年前編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) Win7 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) VC++ 2013(WIN32 API) 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) MySQL C API 問題(Question): 各位前輩好 小弟現在寫了一隻程式 功能是先讀取資料夾內的檔案 再把檔案名子存入資料庫 可是現在問題是 我不知道該如何在mysql_query()中加入VC++內的變數 小弟嘗試了3種寫法都會出現錯誤 1: mysql_query(&myCont, "insert into ccc values (NULL, '%s');",dir); 會在dir這邊出現"Error:函式呼叫中的引數太多" 2: mysql_query(&myCont, "insert into ccc values (NULL, 'dir');"); 這樣存到資料庫內都全部都是dir而不是變數內的值 3: mysql_query(&myCont, "insert into ccc values (NULL, "dir");"); 會在dir這邊出現" Error:必須是')' " 麻煩各位前輩指導小弟 謝謝! 程式碼網址(ideone):http://ideone.com/sM42ts 程式碼圖片:http://i.imgur.com/71deIkV.jpg
程式碼(Code):(請善用置底文網頁, 記得排版) #include <windows.h> #include <stdio.h> #include <mysql.h> #include <string> #include <iostream> void main() { char InputPath[] = "D:\\tttrrr\\"; //放要讀取檔案的資料夾路徑到InputPath字串裡 char szDir[MAX_PATH]; char dir[MAX_PATH]; WIN32_FIND_DATA FileData; HANDLE hList; //------------------------------------------------------ //資料庫變數 char user[] = "root"; char pswd[] = ""; char host[] = "localhost"; char table[] = "user"; unsigned int port = 3306; MYSQL myCont; MYSQL_RES *result; MYSQL_ROW sql_row; int res; //---------------------------------------------------------- mysql_init(&myCont); sprintf_s(szDir, "%s\\*", InputPath); if ((hList = FindFirstFile(szDir, &FileData)) == INVALID_HANDLE_VALUE) printf("No files be found.\n\n"); else { while (1) { if (!FindNextFile(hList, &FileData)) { if (GetLastError() == ERROR_NO_MORE_FILES) break; } sprintf_s(dir, "%s", FileData.cFileName); if (strlen(dir) > 3){ if (mysql_real_connect(&myCont, host, user, pswd, table, port, NULL, 0)) //連接資料庫 { mysql_query(&myCont, "SET NAMES BIG5"); mysql_query(&myCont, "insert into ccc values (NULL, '%s');",dir); } printf("%s\n", dir); } } } FindClose(hList); system("pause"); } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.136.21.182 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1492574128.A.8E0.html

04/19 11:58, , 1F
關鍵字: sprintf
04/19 11:58, 1F
謝謝!!! 通了!! 完成了!! ※ 編輯: chingyue (220.136.21.182), 04/19/2017 12:06:55
文章代碼(AID): #1Ozj-mZW (C_and_CPP)