取MySQL的排结果动态数组 [英] Fetch MySQL row result to dynamic array

查看:171
本文介绍了取MySQL的排结果动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在从我的数据库中检索信息 fetch_row(结果)
我想从这些结果中选择,并将其存储在动态阵列
行[I] 将成为信息需要

我需要将其保存至标签识别[触发]

但字符*可以存储为char

所以我现在是标签识别[触发] = *行[I];

但是当我检查结果......这是不是我想要什么
数358713020035990需要在标签识别...

 行[I] 0x05df2090358713020035990的char *标签识别[I] -112''字符

我如何得到这个权利?

 的char *标签识别; INT触发;
标签识别=(字符*)malloc的(的sizeof(字符));
结果=了mysql_store_result(康涅狄格州); //只有整数的一列
NUM_ROWS = mysql_num_rows(结果);
而(行= mysql_fetch_row(结果))
{标签识别[触发] = *行[我];}


解决方案

如果您要复制一个字符串数据缓冲区,而不仅仅是指向该缓冲区,那么你将不得不使用一个内存拷贝操作或preferably像的strcpy 等用途做了一个标准库函数,或函数strncpy 。所以假设标签识别[触发] 指的是一个内存块是类型的数组字符,你可以做到以下几点:

 的#include<&string.h中GT;//标签识别是ROWSIZE点¯xCOLUMNSIZE的字符的一个二维阵列
焦炭**标签识别;
标签识别=的malloc(sizeof的(字符*)* COLUMNSIZE);
的for(int i = 0; I< COLUMNSIZE;我++)
{
    标签识别[I] =的malloc(sizeof的(char)的* ROWSIZE);
}//一些数据复制到阵列的行索引触发
INT触发= someValue中;
函数strncpy(标签识别[触发],行[I],ROWSIZE);//释放内存你分配你的二维数组
的for(int i = 0; I< COLUMNSIZE;我++)
{
    免费(标签识别[I]);
}
免费(标签识别);

ROWSIZE的价值将有大到足以容纳你的最大的字符串加上终止空,否则副本将使用被截断函数strncpy ,或者将数据溢出数组边界并写了别的东西,你不希望它如果你使用的strcpy

When a retrieve info from my db fetch_row(result) I want to select from these results and store them in a dynamic array row[i] will be the info a need

I'll need to store it to tagid[trigger]

but char* can be stored to char

so what i now is tagid[trigger] = *row[i];

but when i check the results... it aint what i want the number 358713020035990 needs to be in tagid...

row[i]  0x05df2090 "358713020035990"    char *

tagid[i]    -112 '' char

how do i get this right?

char *tagid;int trigger;
tagid = (char *) malloc(sizeof(char));
result = mysql_store_result(conn); // only one column of integers
num_rows = mysql_num_rows(result);
while (row = mysql_fetch_row(result))
{tagid[trigger] = *row[i];}

解决方案

If you are trying to copy a string data buffer, and not just the pointer to that buffer, then you are going to have to use a memory copy operation or preferably a standard library function made for such purposes like strcpy, or strncpy. So assuming that tagid[trigger] is referring to a block of memory that is an array of type char, you could do the following:

#include <string.h>

//tagid is a two-dimensional array of chars of ROWSIZE x COLUMNSIZE
char** tagid;
tagid = malloc(sizeof(char*) * COLUMNSIZE);
for (int i=0; i < COLUMNSIZE; i++)
{
    tagid[i] = malloc(sizeof(char) * ROWSIZE);
}

//copy some data into your array at row index "trigger"
int trigger = SOMEVALUE;
strncpy(tagid[trigger], row[i], ROWSIZE);

//free the memory you've allocated for your two dimensional array
for (int i=0; i < COLUMNSIZE; i++)
{
    free(tagid[i]);
}
free(tagid);

The value of ROWSIZE will have to be big enough to hold your largest string plus a terminating NULL otherwise the copy will be truncated using strncpy, or the data will overflow the array bounds and will write-over something else you don't want it to if you use strcpy.

这篇关于取MySQL的排结果动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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