我正在编写一个简单的图形生成器,将图形存储在MySQL数据库中。
这是输出:
连接数据库......成功!
检查未完成的顶点...最后完成n = 7
创建n8表...成功!
请注意,它没有打印"生成......"这是同一声明的一部分。它就像100%CPU一样冻结。该表实际上是在数据库中创建的。
另外,如果我从n = 1开始,它会一直运行到n7然后冻结。
这里是代码(不是整个代码,只是这一部分):
int main()
{
int n = 0;
if (initialize(n)) {
int limit = 8;
if (limit >= n) {
for (; n <= limit; n++) {
stringstream qs;
qs << "CREATE TABLE n" << n << " (idbin TEXT("
<< getLengthOfBinaryByN(n) << ") NOT NULL,vertices INT NOT NULL)";
cout << "Creating n" << n << " table...";
if (runQuery(qs.str())) {
cout << "Success!" << endl
<< "Generating all graphs on " << n << " vertices";
generateGraphs(n);
} else {
cout << "Failed!" << endl;
}
}
} else {
cout << "ERROR: Must be greater than or equal to " << n << endl;
return 1;
}
}
return 0;
}
经过一番研究后,我发现了一个“死锁”#34;可能正在发生,但我不知道如何解决这个问题。有什么想法吗?
如果有帮助,可以使用runQuery函数:
/*
runQuery: runs the sql query, assumes that database connected successfully
Parameters: query
returns a bool
*/
bool runQuery(string query)
{
try {
stmt = con->createStatement();
res = stmt->executeQuery(query);
delete stmt;
} catch (sql::SQLException &e) {
if (e.getErrorCode() != 0 && e.getErrorCode() != 1050) {
cout << "# ERR: SQLException in " << __FILE__
<< "(" << __FUNCTION__ << ") on line " << __LINE__ << endl
<< "# ERR: " << e.what()
<< " (MySQL error code: " << e.getErrorCode()
<< ", SQLState: " << e.getSQLState() << " )" << endl;
return false;
}
return true;
}
return true;
}