上次我们已经共同学习了在Linux下C连接数据库,下面一起学习用C语言来操作数据库。
1,首先要打开mysql的服务
[root@bogon ~]# service mysqld status
mysqld 已停
[root@bogon ~]# service mysqld start
启动 MySQL: [确定]
[root@bogon ~]#
此时mysql服务已打开
下面我们来读取数据库的内容
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| data |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from product;
+------+-------+-------+------+
| code | name | price | num |
+------+-------+-------+------+
| 1001 | apple | 2.5 | 8 |
| 1003 | cake | 2.5 | 1 |
| 1002 | pen | 1 | 5 |
+------+-------+-------+------+
3 rows in set (0.00 sec)
mysql>
好了,下面我们用代码来测试,命名为 readdata.c
#include<stdio.h>
#include<stdlib.h>
#include "mysql.h" #define N 10
typedef struct Data
{
char code[];
char name[];
float price;
int num;
}Data;
MYSQL *conn_ptr;
int main()
{
void update_data(Data obj);
void conn_data(MYSQL *conn_ptr); printf("Reading database...\n");
read_data();
return ;
} /***********************/
/** Connect Database **/
/***********************/
void conn_data(MYSQL *conn_ptr)
{
conn_ptr=mysql_init(NULL); //连接初始化
if(!conn_ptr)
{
fprintf(stderr, "mysql_init failed\n");
exit ();
//return EXIT_FAILURE;
}
conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root","","test", , NULL, ); //建立实际连接
//参数分别为:初始化的连接句柄指针,主机名(或者IP),用户名,密码,数据库名,0,NULL,0)后面三个参数在默认安装mysql>的情况下不用改
if(conn_ptr)
{
printf("Connection success\n");
}
else
{
printf("Connection failed\n");
}
mysql_close(conn_ptr);
} /*******************/
/** read delete **/
/******************/
int read_data()
{
MYSQL *conn_ptr;
conn_ptr=mysql_init(NULL); //连接初始化
if(!conn_ptr)
{
fprintf(stderr, "mysql_init failed\n");
exit ();
return EXIT_FAILURE;
}
conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root","","test", , NULL, ); //建立实际连接
//参数分别为:初始化的连接句柄指针,主机名(或者IP),用户名,此处密码为空,数据库名,0,NULL,0)后面三个参数在默认安装mysql>的情况下不用改
if(conn_ptr)
{
printf("Connection success\n");
}
else
{
printf("Connection failed\n");
} MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
int res = mysql_query(conn_ptr, "select * from product ");
if(res)
{
printf("select error: %s\n", mysql_error(conn_ptr));
}
else {
res_ptr = mysql_store_result(conn_ptr);
if(res_ptr)
{
printf("********************\n");
printf("Retrieved %lu rows\n", (unsigned long)mysql_num_rows (res_ptr));
printf("********************\n");
printf("Code\tName\tPrice\tNumber\n");
while (sqlrow = mysql_fetch_row(res_ptr))
{
unsigned int field_count;
field_count =;
unsigned int field_num=; while(field_count < mysql_field_count(conn_ptr))
{
printf("%s\t", sqlrow[field_count]);
field_count++;
}
printf("\n");
}
if(mysql_errno(conn_ptr))
{
printf("Retrive error : %s\n", mysql_error(conn_ptr));
}
}
mysql_free_result(res_ptr);
mysql_close(conn_ptr); //关闭连接
return EXIT_SUCCESS;
}
}
那就可以运行程序了
[root@bogon ~]# gcc -I/usr/include/mysql readdata.c -lmysqlclient -L/usr/lib/mysql -o read
[root@bogon ~]# ./read
Reading database...
Connection success
********************
Retrieved 3 rows
********************
Code Name Price Number
1001 apple 2.5 8
1003 cake 2.5 1
1002 pen 1 5
[root@bogon ~]#
实验发现和从数据库中读取的完全相同,说明读取数据库已成功。
下面我们可以对其进行典型的“增删查改”了。