⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 execsql.cpp.html

📁 《Big C++ 》Third Edition电子书和代码全集-Part1
💻 HTML
字号:
<html>

<head>
	<title>execsql.cpp</title>
</head>

<body>
<pre>  1  #include &lt;iostream&gt;
  2  
  3  #include &lt;string&gt;
  4  #include &lt;mysql.h&gt;
  5  
  6  using namespace std;
  7  
  8  void execute_command(MYSQL* connection, string command)
  9  {
 10     if (mysql_query(connection, command.c_str()) != 0)
 11     {
 12        cout &lt;&lt; "Error: " &lt;&lt; mysql_error(connection) &lt;&lt; "\n";
 13        return;
 14     }
 15  
 16     MYSQL_RES* result = mysql_store_result(connection);
 17     if (result == NULL) return;
 18  
 19     int rows = mysql_num_rows(result);
 20     int fields = mysql_num_fields(result);
 21     for (int r = 1; r &lt;= rows; r++)
 22     {
 23        MYSQL_ROW row = mysql_fetch_row(result);
 24        for (int f = 0; f &lt; fields; f++)
 25        {
 26           string field(row[f]);
 27           if (f &gt; 0) cout &lt;&lt; ",";
 28           cout &lt;&lt; field;
 29        }
 30        cout &lt;&lt; "\n";
 31     }
 32  
 33     mysql_free_result(result);
 34  }
 35  
 36  int main()
 37  {
 38     MYSQL* connection = mysql_init(NULL);
 39  
 40     if(mysql_real_connect(connection, NULL, NULL, NULL,
 41           "bigcpp", 0, NULL, 0) == NULL)
 42     {
 43        cout &lt;&lt; "Error: " &lt;&lt; mysql_error(connection) &lt;&lt; "\n";
 44        return 1;
 45     }
 46  
 47     string line;
 48     bool more = true;
 49     while (getline(cin, line))
 50     {
 51        execute_command(connection, line);
 52     }
 53  
 54     mysql_close(connection);
 55     return 0;
 56  }</pre>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -