steve3d Posted September 6, 2011 Posted September 6, 2011 I need more sql support in my project, but just sqlite is not enough, So I decided to add mysql support in my plugin, some mysql functions returns string array. for example, mysql_fetch_row return MYSQL_ROW, which is typedef as char **, just array of strings. So how to use a array of string as return type, or use as argument to change in C++ api?
steve3d Posted September 6, 2011 Author Posted September 6, 2011 I've created a class MySQLResult, and MySQLDriver class, driver class has a query function returns a MySQLResult object, I tried to use this: Unigine::Variable query(const char *query) const { int result = mysql_query(m_pMySQL, query); if(result) Unigine::Log::error("%s\n", mysql_error(m_pMySQL)); else { MYSQL_RES *res = mysql_use_result(m_pMySQL); if(res) return Unigine::Variable(Unigine::TypeInfo(Unigine::TypeID<MySQLResult*>()), new MySQLResult(res), 1, 1); } return Unigine::Variable(0); } and my script is driver = new MySQLDriver("server", "user", "pass"); driver.selectDatabase("db"); MySQLResult result = driver.query("show tables"); if(result) { log.message("Field:%d", result.getFieldCount()); for(int i=0; i<result.getRowCount(); i++) log.message("%s\n", result.getColumnValue(0)); } but engine runs with error: Variable::getIntSafe(): can't convert extern class to int I've checked the Network plugin, it's almost same, why my code doesn't work? please somebody help me.
steve3d Posted September 6, 2011 Author Posted September 6, 2011 well, after some debugging, I've found that error comes with statement if(result), script engine try to convert MySQLResult object to int, that's why the code failed. then there comes another problem. how to test returned class is a null object?
Guest anet Posted September 6, 2011 Posted September 6, 2011 Try to return NULL this way: return Variable(TypeInfo(TypeID<MySQLResult *>()),NULL);
steve3d Posted September 7, 2011 Author Posted September 7, 2011 already tried with that, still same error Variable::getIntSafe(): can't convert extern class to int Unigine::Variable query(const char *query) const { int result = mysql_query(m_pMySQL, query); if(result) Unigine::Log::error("%s\n", mysql_error(m_pMySQL)); else if(mysql_field_count(m_pMySQL)) { // if field count is non-zero, then there must be some result MYSQL_RES *res = mysql_use_result(m_pMySQL); Unigine::Variable(Unigine::TypeInfo(Unigine::TypeID<MySQLResult*>()), new MySQLResult(res), 1, 1); } return Unigine::Variable(Unigine::TypeInfo(Unigine::TypeID<MySQLResult*>()), NULL); } still can not use if(result) to test the result is null or a successful result.
Guest anet Posted September 7, 2011 Posted September 7, 2011 I use this trick in Network plugin to return NULL pointers. But I also use this type of check in Unigine script: if(result != NULL) { // do something } And it works fine for me. Does it matter for you to be able to check if(result) without comparing it with NULL in Unigine script?
steve3d Posted September 7, 2011 Author Posted September 7, 2011 thank you, use if(result != 0) and if(result != NULL) both works, I've checked the unigine.h file , NULL is defined with 0 but why if(result) don't work, that's a bit odd, :mellow: anyway, thank you :)
Recommended Posts