C语言写数据库(一)

/***
connect.c
***/
#include<stdio.h>
#include<stdlib.h>
#include"mysql.h"

int main()
{
    MYSQL mysql;
    int t,r;
    mysql_init(&mysql);
    if(!mysql_real_connect(&mysql,"localhost","root","mysql123","csql",0,NULL,0))
    {
        printf("Error connecting to database:%s\n",mysql_error(&mysql));
    }
    else
    {
        printf("Connect MYSQL successfully!\n");
    }
    mysql_close(&mysql);
    return 0;
}

gcc编译:gcc connect.c -o connect -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient

/***
query
***/
#include<stdio.h>
#include<stdlib.h>
#include"mysql.h"

int main()
{
    MYSQL* conn_ptr;
    MYSQL_RES* res_ptr;
    MYSQL_ROW sqlrow;
    char taskID[256];
    char taskName[256];
    int res,i,j;
    
    conn_ptr = mysql_init(NULL);
    if(!conn_ptr)
    {
        return EXIT_FAILURE;
    }
    conn_ptr = mysql_real_connect(conn_ptr,"localhost","root","mysql123","csql",0,NULL,0);
    if(conn_ptr)
    {
        res = mysql_query(conn_ptr,"select * from task where taskState=4 order by taskLastEditTime desc");
        if(res)
        {
            printf("SELECT error:%s\n",mysql_error(conn_ptr));
        }
        else
        {
            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)))
                {
                    for(i = 0 ; i  < j; i++)
                    {
                        printf("%s\t",sqlrow[i]);
                    }
                    printf("\n");
                    break;
                }
                if(mysql_errno(conn_ptr))
                {
                    fprintf(stderr,"Receive error:%s",mysql_error(conn_ptr));
                }
            }
            mysql_free_result(res_ptr);
        }
        if(res)
        {
            printf("mysql_insert error:%s\n",mysql_error(conn_ptr));
        }
    }
    else
    {
        printf("Connection failed\n");
    }
    mysql_close(conn_ptr);
    return EXIT_SUCCESS;
}

gcc编译:gcc query.c -o query -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient

/**
insert
***/
#include<stdio.h>
#include<stdlib.h>
#include"mysql.h"

int main(){
    MYSQL *conn_ptr;
    int res;
    
    conn_ptr = mysql_init(NULL);
    if(!conn_ptr){
        printf("mysql_init failed!\n");
        return EXIT_FAILURE;
    }
    conn_ptr = mysql_real_connect(conn_ptr,"localhost","root","shenhaoinfo","csql",0,NULL,0);
    if(conn_ptr){
        res = mysql_query(conn_ptr,"insert into children values(null,'jesffsui',5)");
        //
        if(!res){
            printf("Insert %lu rows\n",(unsigned long)mysql_affected_rows(conn_ptr));
        }else{
            fprintf(stderr,"insert error %d: %s\n",mysql_errno(conn_ptr),mysql_error(conn_ptr));
        }
    }else{
        printf("Connection failed\n");
    }
    mysql_close(conn_ptr);
    return EXIT_SUCCESS;
}

gcc编译:gcc insert.c -o insert -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient

/***
delete
***/
#include<stdio.h>
#include<stdlib.h>
#include"mysql.h"

int main(){
    MYSQL *conn_ptr;
    int res;
    
    conn_ptr = mysql_init(NULL);
    if(!conn_ptr){
        printf("mysql_init failed!\n");
        return EXIT_FAILURE;
    }
    conn_ptr = mysql_real_connect(conn_ptr,"localhost","root","shenhaoinfo","csql",0,NULL,0);
    if(conn_ptr){
        //res = mysql_query(conn_ptr,"insert into children values(null,'jesffsui',5)");
        res =mysql_query(conn_ptr,"delete from children where fname = 'jesffsui'");
        if(!res){
            printf("delete %lu rows\n",(unsigned long)mysql_affected_rows(conn_ptr));
        }else{
            fprintf(stderr,"delete error %d: %s\n",mysql_errno(conn_ptr),mysql_error(conn_ptr));
        }
    }else{
        printf("Connection failed\n");
    }
    mysql_close(conn_ptr);
    return EXIT_SUCCESS;
}

gcc编译:gcc delete.c -o delete -I /usr/include/mysql/ -L /usr/lib/mysql/ -lmysqlclient

分别是大写的i,表示INCLUDE,大写的L,表示LINK,小写的L,表示link 一个库

无论是windows环境下还是linux环境下,编译链接都要带上mysql的库,头文件和附属链接。如何配置网上其他网友写的都已经很详细了。

转载于:https://www.cnblogs.com/wanghao-boke/p/10979007.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值