如何从mysql_querry获取数据 [英] How to fetch data from mysql_querry

查看:59
本文介绍了如何从mysql_querry获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我试着用c ++语言从mysql中获取一组数据

i做了一些研究,但我只得到了如何获得一个数据的信息

for a示例



来自'mytable'表格

有2个colum



第一列是名字

第二栏是年龄



名字栏我得到了

' jessica''jessie'和'jessy



年龄是10岁,12岁,18岁



我该怎么办?将所有数据存储到字符串列表

当我从mytable调用SELECT *时



我尝试过:



hello i am try to get a group of data from mysql with c++ language
i did some research but i only got the info how to get one data
for an example

from the table of 'mytable'
there is 2 colum

one 1st column is name
2nd column is age

in the name column i got
'jessica' 'jessie' and 'jessy

age was 10, 12, 18

how do i store all of the data to a list of string
when i am calling SELECT * from mytable

What I have tried:

char* retrieveData(char* sqlStatement) {
		mysql_query(_mysqlCon, sqlStatement);
		_mysqlResult = mysql_store_result(_mysqlCon);
		if((_mysqlRows = mysql_fetch_row(_mysqlResult)) == NULL) return "No student's data was found";
		else while (1) {
			return _mysqlRows[0];
		}
		mysql_free_result(_mysqlResult);
		mysql_close(_mysqlCon);
	}



这就是我所做的。

但我只是得到了jessica



当我改为_mysqlRow [1];

i得到10



但当我尝试将_mysqlRow [2]提取到_mysqlRow [5]

结果为null ..



但是当我试试这个
$时b $ b


this is what i have done.
but i just only got jessica as a result

when i change to _mysqlRow[1];
i got 10

but when i try to fetch _mysqlRow[2] to _mysqlRow[5]
the result was null..

but when i try this

__declspec(dllexport) int dataRow(char* sqlStatement) {
		mysql_query(_mysqlCon, sqlStatement);
		_mysqlResult = mysql_store_result(_mysqlCon);
		return mysql_num_rows(_mysqlResult);
		mysql_free_result(_mysqlResult);
		mysql_close(_mysqlCon);
	}



它实际上告诉我数据行是6 ..


it actually tell me that the row of data was 6..

推荐答案

阅读已使用函数的文档,并查看一些示例代码。两者都可以在网上找到。



要迭代结果,请使用以下内容(未经测试):

Read the documentation of the used functions and have a look at some example code. Both can be found in the web.

To iterate over the results use something like this (untested):
MYSQL_RES *result = mysql_store_result(mysqlCon);
if (result != NULL) 
{
    int numFields = mysql_num_fields(result);
    MYSQL_ROW row;
    while ((row = mysql_fetch_row(result))) 
    { 
        // Row
        for (int i = 0; i < numFields; i++) 
        { 
            // Access field with row[i]
        }
    } 
    mysql_free_result(result);
}
else
{
    // Handle no matching data found
}
mysql_close(mysqlCon);



一些注意事项:



如果没有清理,请不要从函数返回。在你的代码中,结果可能没有被释放,连接可能没有关闭。



你不应该使用以下划线开头的名称和定义。这些保留供编译器实现使用。


Some notes:

Don't return from a function without cleanup. In your code the result may be not freed and the connection may be not closed.

You should not use names and defines that begin with an underscore. These are reserved for usage by the compiler implementation.


这篇关于如何从mysql_querry获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆