C executeQuery()错误显示表中的MySQL数据
发布时间:2020-12-16 06:51:31 所属栏目:百科 来源:网络整理
导读:我需要一些帮助.我有这个代码(下面),将数据添加到 MySQL表,然后返回相同的表.代码运行正常,当我运行它时,它将列添加到MySQL表但它停止,错误: SQL error. Error message: 字面上一片空白.如果我在executeQuery()中使用SELECT语句而不是INCLUDE语句,它运行时
我需要一些帮助.我有这个代码(下面),将数据添加到
MySQL表,然后返回相同的表.代码运行正常,当我运行它时,它将列添加到MySQL表但它停止,错误:
SQL error. Error message: 字面上一片空白.如果我在executeQuery()中使用SELECT语句而不是INCLUDE语句,它运行时没有问题,也没有错误消息,只显示我的表(或部分表).我错过了什么? 我正在使用Visual Studios 2015和MySQL Server. 最终目标是使用C将API与SQL表连接,以根据特定的时间跨度记录数据.这是最初的步骤之一,只是为了确保我可以正确地将MySQL与C链接起来. 很抱歉,如果这篇文章写得不好,第一次在这里,绝对不是一个有经验的程序员…另外,我调查了其他线程,但由于这是如此具体,我发现它们没有帮助. // Standard C++ includes #include <iostream> #include <cstdlib> #include <string> using namespace std; // Include the Connector/C++ headers #include "cppconn/driver.h" #include "cppconn/exception.h" #include "cppconn/resultset.h" #include "cppconn/statement.h" // Link to the Connector/C++ library #pragma comment(lib,"mysqlcppconn.lib") // Specify our connection target and credentials const string server = "127.0.0.1:3306"; const string username = "root"; const string password = "root"; const string database = "dbex"; const string table = "tbex"; int main() { sql::Driver *driver; // Create a pointer to a MySQL driver object sql::Connection *dbConn; // Create a pointer to a database connection object sql::Statement *stmt; // Create a pointer to a Statement object to hold our SQL commands sql::ResultSet *res; // Create a pointer to a ResultSet object to hold the results of any queries we run // Try to get a driver to use to connect to our DBMS try { driver = get_driver_instance(); } catch (sql::SQLException e) { cout << "Could not get a database driver. Error message: " << e.what() << endl; system("pause"); exit(1); } // Try to connect to the DBMS server try { dbConn = driver->connect(server,username,password); dbConn->setSchema(database); } catch (sql::SQLException e) { cout << "Could not connect to database. Error message: " << e.what() << endl; system("pause"); exit(1); } stmt = dbConn->createStatement(); // Try to query the database try { //stmt->execute("USE " + database); // Select which database to use. Notice that we use "execute" to perform a command. res = stmt->executeQuery("INSERT INTO "+ table +"(Brand,Model,Power,`Last Used`,`# Times Used`) VALUES('Ferrari','Modena','500','Never',0)"); res = stmt->executeQuery("SELECT * FROM cars"); // Perform a query and get the results. Notice that we use "executeQuery" to get results back } catch (sql::SQLException e) { cout << "SQL error. Error message: " << e.what() << endl; system("pause"); exit(1); } // While there are still results (i.e. rows/records) in our result set... while (res->next()) { cout << res->getString(1) << endl; } delete res; delete stmt; delete dbConn; system("pause"); return 0; } 提前致谢 解决方法
检查一下:
排队: res = stmt->executeQuery("INSERT INTO "+ table +"(Brand,0)"); 您正在进行错误的字符串连接,(加号)运算符不能以这种方式工作,该代码不会连接字符串,而是添加指针. 只需简单地替换这种方式再试一次: #define TABLE "tbex"// put this in top of cpp file ...... res = stmt->executeQuery("INSERT INTO " TABLE "(Brand,0)"); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Flex的Tree全部展开收缩,ji展开选中单个节点
- c# – XmlSerializer不使用sgen创建的XmlSerializers.dll
- c# – 是否有“public var Method()”这样的东西?
- ruby-on-rails – rails where()sql在数组上查询
- c – printf比std :: cout快5倍以上?
- npm run-script flow无效
- ruby-on-rails – 在Devise中将用户作为管理员销毁
- 如何在iOS半透明中制作PhoneGap状态栏?
- VB.NET使用LhSocket5Net创建Socket5服务器
- c# – 哪个更好用:Convert.ToX或X.Parse(…)?