C语言查询MySQL数据库如何将得到的数据放入结构体数组中?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言查询MySQL数据库如何将得到的数据放入结构体数组中?相关的知识,希望对你有一定的参考价值。

int mysql_select(int connfd,MSG *pbuf,MYSQL *conn_ptr)

int res, i, j, m;
char sqlstr[128];
char buf[128];
//char *cData;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;

sprintf(sqlstr, "select * from Door_Kind where Kind_Door_Name = '%s'", pbuf->name);
res = mysql_query(conn_ptr, sqlstr);

if (!res)
res_ptr = mysql_store_result(conn_ptr);
if(res_ptr)
printf("%lu Rows\n",(unsigned long)mysql_num_rows(res_ptr));
j = mysql_num_fields(res_ptr);

while((sqlrow = mysql_fetch_row(res_ptr))) //依次取出记录
//strncpy(pbuf->data, (char *)sqlrow, j);
for(i = 0; i < j; i++)
pbuf->data[i] =( (char *)&sqlrow[i] );
printf("sqlrow[%d] = %s\n", i,sqlrow[i]);


send(connfd, pbuf->data, strlen(pbuf->data), 0);

参考技术A select * from tableName limit m,nm代表从多少条开始,n代表查询多少条追问

可是我的sqlrow里面的数据是正确的,但是在放入结构体数组pbuf->data后就出问题了,试了很多方法,结果都是错误的,所以向你们求助

本回答被提问者和网友采纳

LNMP源码编译安装之Mysql

Mysql 数据库
是一种关系型数据库管理软件,关系型数据库特点是将数据库保存在不同的二维表(Excel)中,并且将这些表放入不同的数据库中,而不是把所有数据统一放在一个大仓库里,这样的设计增加了Mysql的读取速度,灵活性和可管理性也得到了很大提高。访问及管理Mysql数据库的最常用标准化语言为SQL结构化查询语言。

Mysql安装

[[email protected] ~]# useradd -s /sbin/noligin mysql -M
[[email protected] tools]# mv mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49
[[email protected] tools]# cd /application/mysql-5.5.49/
[[email protected] tools]# ln -s /application/mysql-5.5.49/ /application/mysql
[[email protected] mysql]# chown -R mysql.mysql /application/mysql/
[[email protected] mysql]# ./scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql   ?初始化

技术分享图片
修改配置文件

cd /application/mysql/bin
sed -i ‘s#/usr/local/#/application/#g‘ mysqld_safe 
sed -i ‘s#/usr/local/#/application/#g‘ support-files/mysql.server  
cp support-files/mysql.server /etc/init.d/mysqld  拷贝到启动脚本
chmod +x /etc/init.d/mysqld 赋予权限
[[email protected] bin]# less mysqld_safe

技术分享图片

指定配置文件

[[email protected] mysql]# cp support-files/my-small.cnf /etc/my.cnf 
cp:是否覆盖"/etc/my.cnf"? y
  • 启动数据库*
    [[email protected] mysql]# /application/mysql/bin/mysqld_safe --user=mysql &
    [1] 6953
    180411 21:35:31 mysqld_safe Logging to ‘/application/mysql/data/web01.err‘.
    180411 21:35:31 mysqld_safe Starting mysqld daemon with databases from /application/mysql/data
    PATH="/application/mysql/bin:$PATH"  设置环境变量
    echo "PATH="/application/mysql/bin:$PATH"" >>/etc/profile
    . /etc/profile
    [[email protected] mysql]# lsof -i :3306
    COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    mysqld  7183 mysql   10u  IPv4  61272      0t0  TCP *:mysql (LISTEN)

启动关闭测试

[[email protected] mysql]# /etc/init.d/mysqld stop
Shutting down MySQL.180411 21:44:45 mysqld_safe mysqld from pid file /application/mysql/data/web01.pid ended
 SUCCESS! 
[1]+  Done                    /application/mysql/bin/mysqld_safe --user=mysql
[[email protected] mysql]# lsof -i :3306
[[email protected] mysql]# /etc/init.d/mysqld start
Starting MySQL... SUCCESS! 

Mysql数据库安装总结-脚化

useradd -s /sbin/noligin mysql -M
mkdir –p /application/
mkdir -p /home/oldboy/tools 
cd /home/oldboy/tools/
tar zxf mysql-5.5.49-linux2.6-x86_64.tar.gz
useradd -s /sbin/noligin mysql -M
mv mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49
cd /application/mysql-5.5.49/
ln -s /application/mysql-5.5.49/ /application/mysql
chown -R mysql.mysql /application/mysql/
./scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql  
cd /application/mysql/bin
sed -i ‘s#/usr/local/#/application/#g‘ mysqld_safe 
cd ..
sed -i ‘s#/usr/local/#/application/#g‘ support-files/mysql.server
cp support-files/mysql.server /etc/init.d/mysqld  
chmod +x /etc/init.d/mysqld 
\cp support-files/my-small.cnf /etc/my.cnf
/application/mysql/bin/mysqld_safe --user=mysql &
PATH="/application/mysql/bin:$PATH"
echo "PATH="/application/mysql/bin:$PATH"" >>/etc/profile
. /etc/profile
lsof -i :3306
/etc/init.d/mysqld stop
chkconfig mysqld on 

以上是关于C语言查询MySQL数据库如何将得到的数据放入结构体数组中?的主要内容,如果未能解决你的问题,请参考以下文章

Mysql

sql查询出来的结果,如何放入数组中

LNMP源码编译安装之Mysql

C 语言文件操作 ( 学生管理系统 | 插入数据 | 查询数据 | 删除数据 )

php怎样把一个数组放入另一个数组的子数组里面

C# SQL语句查询出来的字段放入数组中