MYSQL_RES *Query::get_result(const std::string& sql) { // query, result if (odb && res) { GetDatabase().error(*this, "get_result: query busy"); } if (odb && !res) { if (execute(sql)) { res = mysql_store_result(&odb -> mysql); if (res) { MYSQL_FIELD *f = mysql_fetch_field(res); int i = 1; while (f) { if (f -> name) m_nmap[f -> name] = i; f = mysql_fetch_field(res); i++; } m_num_cols = i - 1; } } } return res; }
static Variant HHVM_FUNCTION(mysql_async_fetch_array, const Resource& result, int result_type /* = 1 */) { if ((result_type & PHP_MYSQL_BOTH) == 0) { throw_invalid_argument("result_type: %d", result_type); return false; } auto res = php_mysql_extract_result(result); if (!res) { return false; } MYSQL_RES* mysql_result = res->get(); if (!mysql_result) { raise_warning("invalid parameter to mysql_async_fetch_array"); return false; } MYSQL_ROW mysql_row = nullptr; int status = mysql_fetch_row_nonblocking(mysql_result, &mysql_row); // Last row, or no row yet available. if (status != NET_ASYNC_COMPLETE) { return false; } if (mysql_row == nullptr) { res->close(); return false; } unsigned long *mysql_row_lengths = mysql_fetch_lengths(mysql_result); if (!mysql_row_lengths) { return false; } mysql_field_seek(mysql_result, 0); Array ret; MYSQL_FIELD *mysql_field; int i; for (mysql_field = mysql_fetch_field(mysql_result), i = 0; mysql_field; mysql_field = mysql_fetch_field(mysql_result), i++) { Variant data; if (mysql_row[i]) { data = mysql_makevalue(String(mysql_row[i], mysql_row_lengths[i], CopyString), mysql_field); } if (result_type & PHP_MYSQL_NUM) { ret.set(i, data); } if (result_type & PHP_MYSQL_ASSOC) { ret.set(String(mysql_field->name, CopyString), data); } } return ret; }
DBResult::DBResult(MYSQL_RES* res) { m_handle = res; int32_t i = 0; MYSQL_FIELD* field = mysql_fetch_field(m_handle); while (field) { m_listNames[field->name] = i++; field = mysql_fetch_field(m_handle); } }
/** * do fetch */ static int Lmysql_do_fetch (lua_State *L, lua_mysql_res *my_res, int result_type) { MYSQL_RES *res = my_res->res; unsigned long *lengths; MYSQL_ROW row = mysql_fetch_row(res); if (row == NULL) { lua_pushnil(L); /* no more results */ return 1; } lengths = mysql_fetch_lengths(res); if (result_type == MYSQL_NUM) { int i; lua_newtable(L); /* result */ for (i = 0; i < my_res->numcols; i++) { luaM_pushvalue (L, row[i], lengths[i]); lua_rawseti (L, -2, i+1); } } else { int i,j; MYSQL_FIELD *field; /* Check if colnames exists */ lua_newtable(L); /* result */ mysql_field_seek(my_res->res, 0); for (field = mysql_fetch_field(my_res->res), i = 0; field; field = mysql_fetch_field(my_res->res), i++) { /* push the field name */ luaM_pushvalue (L, field->name, field->name_length); /* Actually push the value */ luaM_pushvalue (L, row[i], lengths[i]); lua_rawset (L, -3); } if (result_type == MYSQL_BOTH) { /* Copy values to numerical indices */ for (j = 0; j < my_res->numcols; j++) { lua_pushnumber(L, j+1); luaM_pushvalue (L, row[j], lengths[j]); lua_rawset (L, -3); } } } return 1; }
ResultSet* myconn_listTablesMatching(Connection *conn, char *pattern){ int i; MYSQL_FIELD *field; ResultSet *rset; rset = myrset_createDefault(conn, mysql_list_tables(conn->stream, pattern)); for(i=0,field = mysql_fetch_field(rset->res);field != NULL;i++,field = mysql_fetch_field(rset->res)){ rset->column_names[i] = estrdup(field->name); rset->column_types[i] = field->type; } return rset; }
//select * from bool doSql(MYSQL *conn,char* sql,SQLDATA& data) { MYSQL_ROW row; MYSQL_FIELD *field; MYSQL_RES *res=NULL; int index=0; if (mysql_query(conn, sql)) { mylogD("%s\n", mysql_error(conn)); exit(0); } res = mysql_store_result(conn); if(res!=NULL) { if(1!=mysql_num_rows(res)) return false; row = mysql_fetch_row(res); while((field = mysql_fetch_field(res))) { std::string key(field->name); std::string value(row[index++]); // mylog("%s %s \n",key.c_str(),value.c_str()); data.push_back(std::make_pair(key,value)); } mysql_free_result(res); return true; } return false; }
int Mysql::Obtener_nombre_campos_y_valores(wxGrid *cuadro,wxString consulta){ if(!mysql_query(sock,consulta.fn_str())){ if((resultado = mysql_store_result(sock))) { // Obtener el número de registros seleccionados: f = (int) mysql_num_rows(resultado); // Obtener el número de columnsa por fila: c = (int) mysql_num_fields(resultado); // Información sobre columnas usando mysql_fetch_field: if(c!=cuadro->GetNumberCols()){ cuadro->AppendCols(c);//agregamos el numero de columnas x resultado cuadro->AppendRows(f);//agregamos el numero de filas x resultado for(int l = 0; l < c; l++) { columna = mysql_fetch_field(resultado); cuadro->SetColLabelValue( l,wxString::FromAscii(columna->name)); cuadro->SetRowLabelValue(l,wxString::Format(wxT("%i"),l+1)); } for(int k=0; k<f;k++){ fila = mysql_fetch_row(resultado); for(int j = 0 ; j < c ; j++){ cuadro->SetCellValue(k,j,wxString::FromAscii(fila[j])); } } } } else{ return 0; } return 1; } else{ mysql_close(sock); return 0; } }
char* CMySQLController::ModelGetString(string strFieldName) { MYSQL_ROW row; MYSQL_FIELD *field; for (int i = 0; i < m_pResult->row_count; i++) { try{ row = mysql_fetch_row(m_pResult); for (int col = 0; col < mysql_num_fields(m_pResult); col++) { field = mysql_fetch_field(m_pResult); if (strFieldName.compare(field->name) == 0) { return row[col]; } } } catch (exception& ex) { string strLog; stringstream strErrorMess; strErrorMess << ex.what() << "][" << __FILE__ << "|" << __LINE__; strLog = CUtilities::FormatLog(ERROR_MSG, "CMySQLController", "Find", "exception:" + strErrorMess.str()); CUtilities::WriteErrorLog(ERROR_MSG, strLog); } } }
QueryResultMysql::QueryResultMysql(MYSQL_RES *result, uint64 rowCount, uint32 fieldCount) : QueryResult(rowCount, fieldCount), mResult(result) { mCurrentRow = new Field[mFieldCount]; assert(mCurrentRow); // mysql-5.0.22 version mysql_fetch_fields get data error, maybe mysql static lib have changed // MYSQL_FIELD *fields = mysql_fetch_fields(mResult); // // for (uint32 i = 0; i < mFieldCount; i++) // { // mCurrentRow[i].SetName(fields[i].name); // mCurrentRow[i].SetType(ConvertNativeType(fields[i].type)); // // } MYSQL_FIELD *field = NULL; for (uint32 i = 0; i < mFieldCount; i++) { field = mysql_fetch_field(mResult); mCurrentRow[i].SetName(field->name); mCurrentRow[i].SetType(ConvertNativeType(field->type)); } }
void display_header() { MYSQL_FIELD *field_ptr; printf("Column details:\n"); while ((field_ptr = mysql_fetch_field(res_ptr)) != NULL) { printf("\t Name: %s\n", field_ptr->name); printf("\t Type: "); if (IS_NUM(field_ptr->type)) { printf("Numeric field\n"); } else { switch(field_ptr->type) { case FIELD_TYPE_VAR_STRING: printf("VARCHAR\n"); break; case FIELD_TYPE_LONG: printf("LONG\n"); break; default: printf("Type is %d, check in mysql_com.h\n", field_ptr->type); } /* switch */ } /* else */ printf("\t Max width %ld\n", field_ptr->length); /* Note on versions of MySQL before 4.0 the format should be %d, rather than %ld */ if (field_ptr->flags & AUTO_INCREMENT_FLAG) printf("\t Auto increments\n"); printf("\n"); } /* while */ }
QSqlRecordInfo QMYSQLDriver::recordInfo( const QSqlQuery& query ) const { QSqlRecordInfo info; if ( !isOpen() ) return info; if ( query.isActive() && query.isSelect() && query.driver() == this ) { QMYSQLResult* result = (QMYSQLResult*)query.result(); QMYSQLResultPrivate* p = result->d; if ( !mysql_errno( p->mysql ) ) { for ( ;; ) { MYSQL_FIELD* field = mysql_fetch_field( p->result ); if ( field ) { info.append ( QSqlFieldInfo( QString( field->name ), qDecodeMYSQLType( (int)field->type, field->flags ), IS_NOT_NULL( field->flags ), (int)field->length, (int)field->decimals, QVariant(), (int)field->type ) ); } else break; } } mysql_field_seek( p->result, 0 ); } return info; }
GArray GSqlDatabase::FetchArray(void) { GArray tab; this->_result = mysql_use_result(&(this->_mysql)); MYSQL_ROW row = NULL; MYSQL_FIELD *field; unsigned int num = mysql_num_fields(this->_result); field = mysql_fetch_field(this->_result); unsigned int i = 0; unsigned int j = 0; while ((row = mysql_fetch_row(this->_result))) { unsigned long *length = mysql_fetch_lengths(this->_result); length = mysql_fetch_lengths(this->_result); i = 0; GStringMap map; while (i < num) { if (row[i]) { GString sub = GString::GetBuffer(row[i], (int)length[i]); map[field[i].name] = sub; } ++i; } ++j; tab.PushBack(map); } mysql_free_result(this->_result); return (tab); }
unsigned int get_Seconds_Behind_Master(MYSQL *conn) { MYSQL_RES *res; MYSQL_ROW row; unsigned long long int num_fields; unsigned long long int row_i=0; unsigned long long int i; unsigned int SBM=0; if(mysql_query(conn, "show slave status;") != 0) { printf("Error: can't execute SQL-query: %s\n", mysql_error(conn)); SBM = 0; } else { res = mysql_store_result(conn); if(res == NULL) printf("Error: can't get the result description\n"); num_fields = mysql_num_fields(res); MYSQL_FIELD *field; unsigned long long int SBM_i=0; unsigned long long int fields_i = 0; while((field = mysql_fetch_field(res))) { if (strstr(field->name, "Seconds_Behind_Master") != NULL) {SBM_i = fields_i;} fields_i++; } if (mysql_num_rows(res) > 0) { row = mysql_fetch_row(res); sscanf(row[SBM_i], "%u", &SBM); } } return(SBM); }
static int test_bug29306(MYSQL *mysql) { MYSQL_FIELD *field; int rc; MYSQL_RES *res; rc= mysql_query(mysql, "DROP TABLE IF EXISTS tab17557"); check_mysql_rc(rc, mysql); rc= mysql_query(mysql, "DROP VIEW IF EXISTS view17557"); check_mysql_rc(rc, mysql); rc= mysql_query(mysql, "CREATE TABLE tab17557 (dd decimal (3,1))"); check_mysql_rc(rc, mysql); rc= mysql_query(mysql, "CREATE VIEW view17557 as SELECT dd FROM tab17557"); check_mysql_rc(rc, mysql); rc= mysql_query(mysql, "INSERT INTO tab17557 VALUES (7.6)"); check_mysql_rc(rc, mysql); /* Checking the view */ res= mysql_list_fields(mysql, "view17557", NULL); while ((field= mysql_fetch_field(res))) { FAIL_UNLESS(field->decimals == 1, "field->decimals != 1"); } mysql_free_result(res); rc= mysql_query(mysql, "DROP TABLE tab17557"); check_mysql_rc(rc, mysql); rc= mysql_query(mysql, "DROP VIEW view17557"); check_mysql_rc(rc, mysql); return OK; }
TL_Mysql::TL_MysqlRes::TL_MysqlRes(MYSQL * mysql) { //if(_result) _result = mysql_store_result(mysql); if (_result == NULL) { if (mysql_field_count(mysql) == 0) { //insert or update //not select _rows_num = mysql_affected_rows(mysql); //_fields_num = 0; _is_select = false; } else { std::ostringstream os; os << "TL_MysqlRes::TL_MysqlRes Error:" << mysql_errno(mysql) << " " << mysql_error(mysql) << std::endl; throw TL_Exp(os.str()); } } else { _fields_num = mysql_num_fields(_result); _rows_num = mysql_num_rows(_result); MYSQL_FIELD* field = mysql_fetch_field(_result); _field_no.clear(); for (size_t i = 0; i < _fields_num; ++i) { _field_no[field[i].name] = i; if (_table_name.empty()) _table_name = field[i].table; } _is_select = true; } _counter = 1; }
void display_header() { MYSQL_FIELD *field_ptr; printf("Column details:\n"); while ((field_ptr = mysql_fetch_field(res_ptr)) != NULL) { printf("\t Name: %s\n", field_ptr->name); printf("\t Type: "); if (IS_NUM(field_ptr->type)) { printf("Numberic field\n"); } else { switch (field_ptr->type) { case FIELD_TYPE_VAR_STRING: printf("VARCHAR\n"); break; case FIELD_TYPE_LONG: printf("LONG\n"); break; default: printf("Type is %d, check in mysql_com.h\n", field_ptr->type); } } printf("\t Max width %ld\n", field_ptr->length); if (field_ptr->flags & AUTO_INCREMENT_FLAG) printf("\t Auto increments\n"); printf("\n"); } }
void print_result(MYSQL_RES *result) { MYSQL_FIELD *field; int fields; MYSQL_ROW row; int i; // 得到并输出列名 while (field = mysql_fetch_field(result)) { printf("%s\t", field->name); } putchar('\n'); fields = mysql_num_fields(result); // 得到列数 // 从结果集中一行一行取数据 while (row = mysql_fetch_row(result)) { for (i = 0; i< fields; i++) printf("%s\t", row[i]); putchar('\n'); } mysql_free_result(result); // 记得释放结果集 }
//定制化查询操作 //显示查询结果, 测试接口 bool db_connect::mysql_show_table() { char table[64][64]; //该函数内部会进行malloc,所以需要显示的释放资源 res = mysql_store_result(connect_mysql); MYSQL_FIELD *fd; MYSQL_ROW sql_row; if(res){ //获取结果行号 std::cout<<"res rows is : "<<mysql_num_rows(res)<<std::endl; int i = 0; //获取列名 for(; fd = mysql_fetch_field(res); i++){ bzero(table[i], sizeof(table[i])); strcpy(table[i], fd->name); } //获取列号 int j = mysql_num_fields(res); for( i=0; i<j; i++ ){ std::cout<<table[i]<<"\t"; //显示所有列名 } std::cout<<std::endl; //获取具体内容数据, 按行进行获取 while(sql_row = mysql_fetch_row(res)){ for(i=0; i<j; i++){ std::cout<<sql_row[i]<<"\t"; } std::cout<<std::endl; } return true; } return false; }
bool sql_connecter::exe_query(const string &sql) { if(mysql_query(_mysql_conn, sql.c_str()) != 0){ cout<<mysql_error(_mysql_conn)<<endl; return false; } //从MYSQL句柄中提取出查询结果 _res = mysql_store_result(_mysql_conn); int row_num = mysql_num_rows(_res);//查询结果集行数 cout<<"共查询到:"<<row_num<<"行。"<<endl; int field_num = mysql_num_fields(_res);//查询结果集列数 cout<<"共有:"<<field_num<<"列。"<<endl; MYSQL_FIELD *fd = NULL;//打印列名 for(; fd=mysql_fetch_field(_res);){ cout<<fd->name<<"\t"; } cout<<endl; for(int index=0; index < row_num; ++index){//外层控制打印多少行的信息 MYSQL_ROW _row = mysql_fetch_row(_res);//获取结果集的一行信息 if(_row){//如果获取的信息不为空 int start = 0; for(; start < field_num; ++start){ cout<<_row[start]<<'\t';//依次打印一行中的每一列的信息 } cout<<endl; } } cout<<endl; close_result(); return true; }
//测试用 void MySqlConn::printResult(MYSQL_RES* result) { int iFields = mysql_num_fields(result); MYSQL_FIELD* field; mysql_field_seek(result, 0); for (int i = 0; i < iFields; ++i) { field = mysql_fetch_field(result); if (i > 0) { printf("\t"); } printf("%s", field->name); } printf("\n"); MYSQL_ROW row; while ((row = mysql_fetch_row(result)) != NULL) { for (int i = 0; i < iFields; ++i) { if (i > 0) { printf("\t"); } printf("%s", row[i] != NULL ? row[i] : "NULL"); } printf("\n"); } }
std::map<std::string, std::string> DataBase::selectInTable(const std::string &table, std::string data, std::string where) { int i; std::string selectCmd; std::map<std::string, std::string> resArray; std::map<std::string, std::string>::iterator it; selectCmd = "SELECT " + data + " FROM " + table; if (where.length()) selectCmd += " WHERE " + where; if (this->query(selectCmd)) std::cerr << "Cannot perform 'select' query on DB." << std::endl; _result = mysql_store_result(&_mysql); if (!(_result)) return (std::map<std::string, std::string>()); for (i = 0; (_field = mysql_fetch_field(this->_result)); ++i) { if (std::string(_field->name) == "id") resArray[_field->name + std::string(".") + table] = ""; else resArray[_field->name] = ""; } this->_row = mysql_fetch_row(_result); if (_row) { for (it = resArray.begin(); it != resArray.end(); ++it) it->second = _row[i]; mysql_free_result(_result); } return (resArray); }
struct field_metadatas *db_get_table_metadata(DB_HANDLE conn,const char *table) { MYSQL_RES *res_set; res_set = mysql_list_fields(conn,table,NULL); if(res_set) { struct field_metadatas* metadatas = malloc(sizeof(struct field_metadatas)); MYSQL_FIELD **field; unsigned int i; mysql_field_seek(res_set, 0); metadatas->size = mysql_num_fields(res_set); metadatas->_field_metadata = malloc(sizeof(struct field_metadata)*metadatas->size); field = malloc(sizeof(MYSQL_FIELD*)*metadatas->size); for (i = 0; i < metadatas->size; i++) { field[i] = mysql_fetch_field (res_set); strncpy(metadatas->_field_metadata[i].name,field[i]->name,NAME_CHAR_LEN); metadatas->_field_metadata[i].type = field[i]->type; metadatas->_field_metadata[i].max_length = field[i]->length; metadatas->_field_metadata[i].default_value = make_defaule_value(field[i]); } free(field); mysql_free_result (res_set); return metadatas; } print_error(conn, "Could not execute statement"); return NULL; }
MySQLFieldInfo *MySQLResult::fetchFieldInfo() { if (!m_localized) { mysql_fetch_field(m_res); } if (m_current_field < getFieldCount()) m_current_field++; return getFieldInfo(m_current_field); }
CMysqlRow* CMysqlQuery::fetch() { if( m_bEnd ) return 0; if( !m_row.m_row ) { m_row.m_row = mysql_fetch_row(m_pResult); m_row.m_lengths = mysql_fetch_lengths(m_pResult); m_row.m_fields = mysql_num_fields(m_pResult); mysql_field_seek(m_pResult, 0); unsigned int i; for( i=0; i<m_row.m_fields; i++ ) { MYSQL_FIELD* field = mysql_fetch_field(m_pResult); m_row.m_types[i] = field->type; m_field_index.insert(std::pair<std::string, unsigned int>(field->name, i)); } } else { m_row.m_row = mysql_fetch_row(m_pResult); //m_row.m_lengths = mysql_fetch_lengths(m_pResult); } if( !m_row.m_row ) { clear(); m_bEnd = true; return 0; } return &m_row; }
// 执行sql查询语句 void query_sql(char * sql, FILE * filterFile) { MYSQL my_connection; MYSQL_RES *res_ptr; MYSQL_FIELD *field; MYSQL_ROW result_row; int row,column; int res; int i,j; mysql_init(&my_connection); if (mysql_real_connect(&my_connection,HOST,USERNAME,PASSWORD,DATABASE,PORT,NULL,CLIENT_FOUND_ROWS)) { // 连接成功 printf("connection success!\n"); mysql_query(&my_connection, "set names utf8"); res = mysql_query(&my_connection, sql); if (res){ printf("Error: mysql_query!\n"); mysql_close(&my_connection); } else{ // 查询结果集 res_ptr = mysql_store_result(&my_connection); if (res_ptr){ column = mysql_num_fields(res_ptr); row = mysql_num_rows(res_ptr)+1; printf("查询到 %lu 行\n", row); // 输出查询结果字段名 for (i=0; field = mysql_fetch_field(res_ptr); i++){ printf("%s\t", field->name); //fprintf(filterFile,"%s\t", field->name); } printf("\n"); //fprintf(filterFile,"\n"); // 按行输出结果 for (i=1; i<row; i++){ result_row = mysql_fetch_row(res_ptr); for (j=0; j<column-1; j++){ printf("%s\t", result_row[j]); fprintf(filterFile,"%s\t",result_row[j]); } printf("%s",result_row[column-1]); fprintf(filterFile,"%s",result_row[column-1]); printf("\n"); fprintf(filterFile,"\n"); } } mysql_close(&my_connection); } } }
void GScr_mysql_fetch_field(int entityIndex) { MYSQL_RES *res = (MYSQL_RES*)Scr_GetInt(0); MYSQL_FIELD *f = mysql_fetch_field(res); if(!f) Scr_AddUndefined(); else Scr_AddString(f->name); }
static JSVAL fetch_field(JSARGS args) { HandleScope scope; MYSQL_RES *res = (MYSQL_RES *) args[0]->IntegerValue(); MYSQL_FIELD *f = mysql_fetch_field(res); JSOBJ o = Object::New(); MakeField(f, o); return scope.Close(o); }
int main(int argc, char **argv) { MYSQL *con = mysql_init(NULL); if (con == NULL) { fprintf(stderr, "mysql_init() failed\n"); exit(1); } if (mysql_real_connect(con, "localhost", "root", "123456", "db2", 0, NULL, 0) == NULL) { finish_with_error(con); } if (mysql_query(con, "SELECT * FROM Cars LIMIT 3")) { finish_with_error(con); } MYSQL_RES *result = mysql_store_result(con); if (result == NULL) { finish_with_error(con); } int num_fields = mysql_num_fields(result); MYSQL_ROW row; MYSQL_FIELD *field; while ((row = mysql_fetch_row(result))) { for(int i = 0; i < num_fields; i++) { if (i == 0) { while(field = mysql_fetch_field(result)) { printf("%s ", field->name); } printf("\n"); } printf("%s ", row[i] ? row[i] : "NULL"); } } printf("\n"); mysql_free_result(result); mysql_close(con); exit(0); }
static void my_print_result_metadata(MYSQL_RES *result) { MYSQL_FIELD *field; unsigned int i; size_t j; unsigned int field_count; mysql_field_seek(result, 0); if (!opt_silent) { fputc('\n', stdout); fputc('\n', stdout); } field_count= mysql_num_fields(result); for(i= 0; i< field_count; i++) { field= mysql_fetch_field(result); j= strlen(field->name); if (j < field->max_length) j= field->max_length; if (j < 4 && !IS_NOT_NULL(field->flags)) j= 4; field->max_length= j; } if (!opt_silent) { my_print_dashes(result); fputc('\t', stdout); fputc('|', stdout); } mysql_field_seek(result, 0); for(i= 0; i< field_count; i++) { field= mysql_fetch_field(result); if (!opt_silent) fprintf(stdout, " %-*s |", (int) field->max_length, field->name); } if (!opt_silent) { fputc('\n', stdout); my_print_dashes(result); } }
MYSQL_FIELD *sql_query_c::fetch_field() { if ( sql_connection && odb && res ) { return mysql_fetch_field( res ); } return 0; }