我的C++程序使用sql查詢讀取文件並嘗試執行它。當我使用phpMyAdmin,它的工作原理,但是當我的程序執行,它與下面的錯誤最終執行查詢: 語法正確時出現C++ MySQL Sql語法錯誤
代碼:
ifstream create_file ("create.sql");
if (create_file.is_open())
{
char * create;
int length;
create_file.seekg (0, ios::end);
length = create_file.tellg();
create_file.seekg (0, ios::beg);
create = new char [length];
create_file.read (create,length);
create_file.close();
cout << "Executing query: " << endl;
cout.write (create,length);
cout << "EOF query" << endl;
if(mysql_query(mysql, "CREATE DATABASE grant_db")) {
fprintf(stderr, "Failed to create database: Error: %s\n",
mysql_error(mysql));
}
if(mysql_select_db(mysql, "grant_db")) {
fprintf(stderr, "Failed to select database: Error: %s\n",
mysql_error(mysql));
}
if(mysql_query(mysql, create)) {
fprintf(stderr, "Failed to create table: Error: %s\n",
mysql_error(mysql));
mysql_query(mysql, "DROP DATABASE grant_db");
}
delete[] create;
} else cout << "Unable to open file 'create.sql'.";
感謝您的幫助!
可能兩個驅動程序的行爲都不一樣,你可以嘗試從你的查詢中刪除註釋,看看它是否有效? – DhruvPathak 2011-03-05 15:54:59