取MySQL的排结果动态数组

编程入门 行业动态 更新时间:2024-10-27 22:31:22
本文介绍了取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的排结果动态数组

本文发布于:2023-11-22 03:04:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615731.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   动态   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!