我已将我的 C++ 项目连接到 MySQL 并成功创建了一个会话。我能够创建一个模式。我的问题是,当我尝试运行简单的任意查询(例如USE testSchema SHOW tables;
使用 MySQL/C++ api)时,会遇到 SQL 语法错误。当我直接在 MySQL shell 中运行该函数时,查询运行得非常好。
这是完整的代码
const char* url = (argc > 1 ? argv[1] : "mysqlx://pct@127.0.0.1");
cout << "Creating session on " << url << " ..." << endl;
Session sess(url);
{
cout << "Connected!" << endl;
// Create the Schema "testSchema"; This code creates a schema without issue
cout << "Creating Schema..." << endl;
sess.dropSchema("testSchema");
Schema mySchema = sess.createSchema("testSchema");
cout << "Schema Created!" << endl;
// Create the Table "testTable"; This code runs like normal, but the schema doesn't show
cout << "Creating Table with..." << endl;
SqlStatement sqlcomm = sess.sql("USE testSchema SHOW tables;");
sqlcomm.execute();
}
这是控制台输出:
Creating session on mysqlx://pct@127.0.0.1 ...
Connected!
Creating Schema...
Schema Created!
Creating Table with...
MYSQL ERROR: CDK Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SHOW tables' at line 1
该错误You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SHOW tables' at line 1
是 MySQL 错误,这意味着我在查询中有语法错误,但是当我仔细查看查询时,我发现它没有任何问题。
我已将代码直接从 cpp 文件复制并粘贴到 mysql shell 中,它运行良好。这告诉我,我在sql()
函数中输入查询的格式有问题。但是该sql()
函数的文档非常简洁。
这是对 sql() 函数的引用:https ://dev.mysql.com/doc/dev/connector-cpp/8.0/class_session.html#a2e625b5223acd2a3cbc5c02d653a1426
有人可以给我一些关于我哪里出错的见解吗?这里还有完整的 cpp 代码以获取更多上下文:https ://pastebin.com/3kQY8THC
Windows 10 Visual Studio 2019 MySQL 8.0 与 Connect/C++ X DevAPI