您的位置:首页 > 数据库 > MySQL

MYSQL与C

2016-06-26 20:34 411 查看
codeblocks 为32位 与本机所安装64位MYSQL不兼容 所以用VS2015

VS中新建项目 建好后 项目属性--

设置解决方案平台为X64

将MYSQL  \lib下的.dll和.lib拷贝到新建项目的X64中的debug下

环境配置完成

此过程中遇到的问题:新建项目时可新建成空文件

<span style="font-size:14px;">#include <stdio.h>
#include <winsock2.h>
#include <mysql.h>
#include <stdlib.h>
#include<cstring>
#include <string>
#include <iostream>
#include <sstream>
/*数据库连接用宏*/
#define HOST "localhost"
#define USERNAME "root"
#define PASSWORD "1234"
#define DATABASE "travel"
#define FLIGHT "select * from flights157"
#define BUS "select * from bus157"
#define HOTEL "select * from hotels157"
#define RESERVATION "select * from reservation157"
//#define DATABASE "test"
//#define INSE "insert into bus157 values(null,'1002',2,'56')"
using namespace std;

char* custID = (char*)malloc(sizeof(char)*100);
int choice1;
//string numTostr(int i);
//char* reservation(string custID);
void reservation(char* custID,int choice1);
void options();
void query_sql(char* sql);
//char * stoc(string str,char* ch);

#pragma warning(disable:4996)//解决不安全报告
int main()
{    
    //printf("%s\n", reservation("1002"));
    //cout << reservation("1002") << endl;

    
    options();
    //char *query;
    //query = "insert into reservations157 values ('10','1002',3,'E')";
    //query = "insert into bus157 values('90','SHANGHAI',50,25,25)";
    //query = "select * from t2 ";
    //query = "select * from t2";
    //query_sql("insert into reservation157 values(null,'1002',2,'12')");
    system("pause");
    return 0;
}

//C与MYSQL链接函数
void query_sql(char* sql)
{
    MYSQL my_connection; /*这是一个数据库连接*/
    int res; /*执行sql語句后的返回标志*/
    MYSQL_RES *res_ptr; /*指向查询结果的指针*/
    MYSQL_FIELD *field; /*字段结构指针*/
    MYSQL_ROW result_row; /*按行返回的查询信息*/
    int row, column; /*查询返回的行数和列数*/
    int i, j;
    /*初始化mysql连接my_connection*/
    mysql_init(&my_connection);
    /*建立mysql连接*/
    if (NULL != mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD,
        DATABASE, 0, NULL, CLIENT_FOUND_ROWS))  /*连接成功*/
    {
        printf("数据库查询query_sql连接成功!\n");
        /*设置查询编码为gbk,以支持中文*/
        mysql_query(&my_connection, "set names gbk");
        res = mysql_query(&my_connection, sql);
        if (res)   /*执行失败*/
        {
            //printf("Error: mysql_query !\n");
            /*关闭连接*/
            mysql_close(&my_connection);
        }
        else     /*现在就代表执行成功了*/
        {
            /*将查询的結果给res_ptr*/
            res_ptr = mysql_store_result(&my_connection);
            /*如果结果不为空,就把结果print*/
            if (res_ptr)
            {
                /*取得結果的行数和*/
                column = mysql_num_fields(res_ptr);
                row = mysql_num_rows(res_ptr);
                printf("查询到 %d 行 \n", row);
                /*输出結果的字段名*/
                for (i = 0; field = mysql_fetch_field(res_ptr); i++)
                    printf("%10s ", field->name);
                printf("\n");
                /*按行输出結果*/
                for (i = 1; i < row + 1; i++)
                {
                    result_row = mysql_fetch_row(res_ptr);
                    for (j = 0; j < column; j++)
                        printf("%10s ", result_row[j]);
                    printf("\n");
                }
            }
            /*不要忘了关闭连接*/
            mysql_close(&my_connection);
        }
        //return 0;
    }
    else
    {
        printf("数据库连接失败");
    }
    //return 1;
}

//操作函数
</span><pre name="code" class="cpp">void options(){
//if (!query_sql("1")) {
int choice2;
//char *query;
printf("欢迎登陆机票预订系统!\n");
printf("输入你的ID:");
cin >> custID;
//scanf_s("%s", custID, 10);

do {
//printf("查询还是预订\n");
printf("1 查询 2 预订 3 退订 4 退出\n");
//scanf_s("%d", &choice1);
cin >> choice1;
if (choice1 == 1) {

printf("1.机票查询\n");
printf("2.大巴查询\n");
printf("3.宾馆查询\n");
printf("4.查询已预订信息\n");
scanf_s("%d", &choice2);
switch (choice2)
{
case 1:query_sql(FLIGHT); break;
case 2:query_sql(BUS); break;
case 3:query_sql(HOTEL); break;
case 4:query_sql(RESERVATION); break;
default:
break;
}
}
else if(choice1 == 4)
{
return;
}
else
{
reservation(custID,choice1);
}
} while (1);
//}
}

//预定函数
void reservation(char* custID,int choice1) {

    printf("0 航班 1 大巴 2 宾馆 \n");

    int choice;

    cin >> choice;

    //char t[256];

    //scanf_s("%d", &choice);

    //sprintf_s(t, 10, "%d", choice+1);

    

    printf("输入编号:\n");

    char key[20];

    cin >> key;

    //scanf_s("%s", key, 10);

    //printf("%s\n", key);

    //string s = "";

    //s = "insert into reservation157 values(null,\'" + custID + "\'," + t + ",\'" + key + "\')";

    //cout << s << endl;

    //s.c_str() = (char*)malloc(sizeof(char) * 100);

    char* ch=(char*)malloc(sizeof(char)*100) ;

    if(choice1 == 2)

        sprintf(ch, "insert into reservation157 values(null,'%s','%d','%s');", custID, choice + 1, key);

    else if (choice1 == 3)

        sprintf(ch, "delete from reservation157 where custID = '%s' and resvKey = '%s' ;", custID, key);

    //strcpy_s(ch,s.size()+1, s.c_str());

    //stoc(s, ch);

    //printf("%s\n", ch);

    //return ch;

    query_sql(ch);

    //cout << s << endl;

    

}

主要遇到的问题: 1.SQL语句为string类型 string中各部分(不同类型)的拼接

2.string类型转char*类型

3.在reservation()函数中cout正常 在main()函数中也cout正常 但不能正常预订与退订

另附一个转换函数 将int转换为string 需#include<sstream>

string numTostr(int i) {

    stringstream ss;

    ss << i;

    return ss.str();

}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: