通讯录第四版

62 篇文章 56 订阅
24 篇文章 4 订阅

最近在看mysql,想起以前写过通讯录,所以就想着将通讯录中的内容存储到mysql中

注意:下面出现的book是我的数据库中一张表的名字,它的字段是(name,tel,mail,addr)。根据情况自己替换
这里写图片描述

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<mysql.h>

#define SIZE 1024
MYSQL mysql;   //mysql的句柄
void Connect(const char* host,const char* user,const char* passwd,const char* db)  //进行数据库的连接
{
    mysql_init(&mysql);  //初始化mysql句柄

    if(!mysql_real_connect(&mysql,host,user,passwd,db,3306,NULL,0))    
    {
        printf("mysql_real_connect faile\n");
        exit(1);
    }
    printf("mysql connect success\n");
}

//name tel  mail addr    通讯录的结构
void Insert(const char* msg)    //向数据库里面进行插入一条信息
{
    //插入的SQL语句:insert into 表名 (列名1,列名2,列名3,列名4) values (name,tel,mail,addr)
    //或使用load data local infile '文本名' into table 表名 批量插入到数据库中
    if(msg)
    {
        char buf[SIZE]="insert into book (name,tel,mail,addr) values ";
        strcat(buf,msg);
        int len=strlen(buf);
        if(mysql_real_query(&mysql,buf,len))    //mysql_real_query失败返回非0
        {
            printf("mysql_real_query insert faile\n");
            return;
        }
    }
}

void Delete(const char* condition)    //删除数据库里面的某一条信息
{
    //删除某一行的SQL语句:DELETE FROM 表名 WHERE 某一行的一个条件          
    if(condition)
    {
        char buf[SIZE]="delete from book where ";
        strcat(buf,condition);
        int len=strlen(buf);
        if(mysql_real_query(&mysql,buf,len))
        {
            printf("mysql_real_query insert faile\n");
            return; 
        }
    }
}

void Select(const char* condition)   //查询信息
{
    //SQL语句的查询: select * from 表名 where 某一行的条件
    char buf[SIZE]={0};
    if(condition)
    {
        strcpy(buf,"select * from book where ");
        strcat(buf,condition);
    }
    else      //如果条件为假,打印全部内容
    {
        strcpy(buf,"select * from book");
    }
    int len=strlen(buf);
    if(mysql_real_query(&mysql,buf,len))
    {
        printf("mysql_real_query select faile\n");   //查询失败
        return ;
    }

    MYSQL_RES* res;   //用来指向结果集
    MYSQL_ROW row;    //用来获取某一行的值
    if(!(res=mysql_store_result(&mysql)))      //将结果集存储起来
    {
        printf("mysql_store_result faile\n");
        mysql_free_result(res);
        return;
    }
    unsigned int num=mysql_num_fields(res);    //获取结果表中的列数
    printf("%-5s\t %-10s\t %-10s\t %-20s\n","name","tel","mail","addr");
    while((row=mysql_fetch_row(res)))  //获取结果集中的每一行,失败或读完返回NULL
    {
        unsigned int i=0;
        for(i=0;i<num;i++)          //row相当于一个指针数组,每一个元素都是一个字段的值
        {
            printf("%s\t",row[i]);
        }
        printf("\n");
    }
    mysql_free_result(res);
}

void Update(const char* column,const char* condition )    //更新某一列的值
{
    //更新某一列的SQL语句:update 表名 set 列名=新的值, ... where 确定到某一行的条件 
    if(condition)
    {
        char buf[SIZE]="update book set ";
        strcat(buf,column);
        strcat(buf," where ");
        strcat(buf,condition);

        int len=strlen(buf);
        if(mysql_real_query(&mysql,buf,len))
        {
            printf("mysql_real_query update faile\n");
            return;
        }
    }
}

void menu()
{
    int i=0;
    while(1)
    {
        printf("0:exit\t 1:insert\t 2:delete\t 3:select\t 4:update\t 5:printBook\n");
        printf("choice->:");
        scanf("%d",&i);
        char buf[100]={0};
        char condition[100]={0};
        switch(i)
        {
        case 0:
            exit(1);
            break;
        case 1:
            printf("输入 (naem,tel,mail,addr) 的格式插入:\n ");
            scanf("%s",buf);
            Insert(buf);
            break;
        case 2:
            printf("输入 name=名字,tel=电话,... 的格式进行删除,至少有一个条件:\n");
            scanf("%s",buf);
            Delete(buf);
            break;
        case 3:
            printf("输入 name=名字,tel=电话,... 的格式确定要查找的行,至少有一个条件:\n");
            scanf("%s",condition);
            Select(condition);
            break;
        case 4:
            printf("输入 name=名字,tel=电话,... 的格式确定要更新的行,至少一个条件\n");
            scanf("%s",condition);
            printf("输入 name=新值,tel=新值,mail=新值,addr=新值 的格式确定要更新的列,至少更改一列:\n");
            scanf("%s",buf);
            Update(buf,condition);
            break;
        case 5:
            Select(NULL);     //打印所有信息
            break;
        default:
            break;
        }
    }
}

int main()
{
//密码是你自己的,MsgBook也是你自己的库
    Connect("localhost","root","****","MsgBook");
    menu();
    mysql_close(&mysql);
    return 0;
}

结果:
这里写图片描述

这里写图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴为您提供Ajax通讯录的完整。这个通讯录使用了Ajax技术,可以在不刷新整个页面的情况下实现添加、编辑、删除联系人等功能。下面是该应用的html、css、js代码: HTML代码: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Ajax通讯录</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <h1>Ajax通讯录</h1> <div class="form"> <h2>添加联系人</h2> <form id="add-contact-form"> <input type="text" id="name" placeholder="姓名" required> <input type="text" id="phone" placeholder="电话" required> <input type="text" id="email" placeholder="邮箱" required> <button type="submit">添加</button> </form> </div> <div class="contacts"> <h2>联系人列表</h2> <ul id="contact-list"> </ul> </div> </div> <script src="script.js"></script> </body> </html> ``` CSS代码: ```css * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: Arial, sans-serif; background-color: #eee; } .container { max-width: 800px; margin: 0 auto; padding: 20px; background-color: #fff; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); } h1 { text-align: center; margin-bottom: 20px; } .form { margin-bottom: 20px; } .form h2 { margin-bottom: 10px; } .contacts h2 { margin-bottom: 10px; } form input { display: block; width: 100%; padding: 10px; margin-bottom: 10px; border: none; border-radius: 5px; box-shadow: 0 0 5px rgba(0, 0, 0, 0.1); } form button { display: block; width: 100%; padding: 10px; margin-top: 10px; background-color: #4CAF50; color: #fff; border: none; border-radius: 5px; cursor: pointer; } ul { list-style: none; margin: 0; padding: 0; } li { padding: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: 0 0 5px rgba(0, 0, 0, 0.1); } li span { font-weight: bold; } li button { background-color: #f44336; color: #fff; border: none; border-radius: 5px; cursor: pointer; float: right; } ``` JS代码: ```js const form = document.getElementById('add-contact-form'); const nameInput = document.getElementById('name'); const phoneInput = document.getElementById('phone'); const emailInput = document.getElementById('email'); const contactList = document.getElementById('contact-list'); function getContacts() { fetch('http://localhost:3000/contacts') .then(res => res.json()) .then(data => { contactList.innerHTML = ''; data.forEach(contact => { const li = document.createElement('li'); li.innerHTML = `<span>${contact.name}</span> - ${contact.phone} - ${contact.email} <button data-id="${contact.id}">删除</button>`; contactList.appendChild(li); }); }); } getContacts(); form.addEventListener('submit', e => { e.preventDefault(); const name = nameInput.value; const phone = phoneInput.value; const email = emailInput.value; fetch('http://localhost:3000/contacts', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, phone, email }) }) .then(res => res.json()) .then(data => { getContacts(); form.reset(); }); }); contactList.addEventListener('click', e => { if (e.target.tagName === 'BUTTON') { const id = e.target.dataset.id; fetch(`http://localhost:3000/contacts/${id}`, { method: 'DELETE' }) .then(res => res.json()) .then(data => { getContacts(); }); } }); ``` 这是一个简单的Ajax通讯录应用,您可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值