VC6.0 向MySQL插入数据时 报错

我的代码:
#include "stdio.h"

#include "Winsock2.h"
#include "mysql.h"
#include "iostream"
#include "string"
#define HOST_IP 127.0.0.1
#define HOST_PORT 3389
struct conn_info
{
char *host;
char *user;
char *password;
char *db;
};
MYSQL *mysql_conn_setup(conn_info con)
{
MYSQL *mysql = mysql_init(NULL);
if(!mysql_real_connect(mysql,con.host,con.user,con.password,con.db,8083,NULL,0))
{
printf("Connection error :%s/n",mysql_error(mysql));
exit(1);
}

return mysql;
}

MYSQL_RES*mysql_conn_sqlQuery(MYSQL*mysql,char*sql_Query)
{
if(mysql_query(mysql,sql_Query))
{
printf("MySQL query error:%s/n",mysql_error(mysql));
exit(1);
}
return mysql_use_result(mysql);
}

#pragma comment(lib,"libmySQL.lib")
void main()
{
MYSQL*conn;
MYSQL_RES*res;
MYSQL_ROW row;
char sql_query[100];
memset(sql_query,'\0',100);
sprintf(sql_query,"show tables");
struct conn_info info;
info.host = "localhost";
info.user = "root";
info.password = "";
info.db = "mysql";
conn = mysql_conn_setup(info);
res = mysql_conn_sqlQuery(conn,sql_query);
printf("mysql all tables: \n");
while ((row = mysql_fetch_row(res))!= NULL)
{
printf("%s \n",row[0]);
}
mysql_free_result(res);

    MYSQL *mysql = mysql_init(NULL);

    char sql[1024] ;
//  sql = /'INSERT INTO smart (id,times,message) VALUES('num2',20160889,'dhasjdhsjhda')/';
    sprintf(sql,"INSERT INTO smart (id,times,message) VALUES('num7',sysdate(),'d')");

    **if(mysql_query(mysql,sql))//此处报错**
    {
        //printf("fail");
    }



  mysql_close(conn);

}

---------------------------------错误信息----------------------------------
C:\Program Files (x86)\Microsoft Visual Studio\MyProjects\rrrrrr\rrrrrr.cpp(124) : error C2664: 'mysql_query' : cannot convert parameter 1 from 'struct st_mysql ** ' to 'struct st_mysql *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
执行 cl.exe 时出错.

7个回答

问题已解决。
贴上可运行的插入语法代码,以防小伙伴们犯愁。

#include

#include

#include

#include

#include

#include

using namespace std;

int main()

{

const char user[] = "root"; //username

const char pswd[] = "root"; //password

const char host[] = "localhost"; //or"127.0.0.1"

const char table[] = "test"; //database

unsigned int port = 3306; //server port

MYSQL myCont;

MYSQL_RES *result;

MYSQL_ROW sql_row;

MYSQL_FIELD *fd;

char column[32][32];

int res;

mysql_init(&myCont);

if(mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0))//判断是否连接成功
{

printf("succeed!!!");
mysql_query(&myCont,"SET NAMES GBK");
ress = mysql_query(&myCont,"SELECT * FROM tablename");

if(!ress)
{
        res = mysql_store_result(&myCont);
        int i,j;
        printf("%ld \n",mysql_num_rows(res));//数据里的表格个数
        for(i=0; fd = mysql_fetch_field(res);i++)
        {
            strcpy(column[i],fd->name);

        }
        j = mysql_num_fields(res);
        printf(" jjj == %d \n",j);

        for(i=0;i<j;i++)
        {
            printf("%s\t",column[i]);

        }
            printf("\n");
            int p;
            p = mysql_query(&myCont,"insert into  tablename(id,data,message)values(000005,20160902,'qwert')");//插入语句

    //  printf("p=%d \n",p);
        if(!p)//判断是否插入成功
        {
            printf("succeed !!! %lu  \n",(unsigned long)mysql_affected_rows(&myCont));

        }else
        {
        //  printf("fail!! %lu",(unsigned long)mysql_affected_rows(&myCont));
        }
            while (row=mysql_fetch_row(res))//获取具体的数据
        {
            for(i=0;i<j;i++)
            {
                printf(" %s\n",row[i]);
            }
            printf("\n");
        }

}

}

把你的sql语句单独拿出来在数据库执行,看看能不能成功,
错误提示的是函数调用的参数类型不匹配

有需要可以参考:
MySQL insert用法详解 http://www.data.5helpyou.com/article421.html

执行过INSERT INTO 查询中可以插入成功 所以就是那个报错我是真不明白啥意思

错误的意思是参数错误:你定义的mysql是struct st_mysql **格式,而mysql_query(xxx,xxx)需要的是struct st_mysql *,,你更改一下定义就行

没写过c,把报错那句的mysql改成&mysql行不行?

添加地址符也会报错

mysql_query(*mysql,sql)

问题已解决。
贴上可运行的插入语法代码,以防小伙伴们犯愁。

#include

#include

#include

#include

#include

#include

using namespace std;

int main()

{

const char user[] = "root"; //username

const char pswd[] = "root"; //password

const char host[] = "localhost"; //or"127.0.0.1"

const char table[] = "test"; //database

unsigned int port = 3306; //server port

MYSQL_RES *res;
MYSQL_ROW row;

MYSQL_FIELD *fd;
char column[32][32];

MYSQL myCont;
int ress;
mysql_init(&myCont);

if(mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0))//判断是否连接成功
{
printf("succeed!!!");
mysql_query(&myCont,"SET NAMES GBK");
ress = mysql_query(&myCont,"SELECT * FROM tablename");

if(!ress)
{
res = mysql_store_result(&myCont);
int i,j;
printf("%ld \n",mysql_num_rows(res));//数据里的表格个数
for(i=0; fd = mysql_fetch_field(res);i++)
{
strcpy(column[i],fd->name);

    }
    j = mysql_num_fields(res);
    printf(" jjj == %d \n",j);

    for(i=0;i<j;i++)
    {
        printf("%s\t",column[i]);

    }
        printf("\n");
        int p;
        p = mysql_query(&myCont,"insert into  tablename(id,data,message)values(000005,20160902,'qwert')");//插入语句

//  printf("p=%d \n",p);
    if(!p)//判断是否插入成功
    {
        printf("succeed !!! %lu  \n",(unsigned long)mysql_affected_rows(&myCont));

    }else
    {
    //  printf("fail!! %lu",(unsigned long)mysql_affected_rows(&myCont));
    }
        while (row=mysql_fetch_row(res))//获取具体的数据
    {
        for(i=0;i<j;i++)
        {
            printf(" %s\n",row[i]);
        }
        printf("\n");
    }

}
if(result!=NULL) mysql_free_result(res);//释放结果资源

mysql_close(&myCont);//断开连接

return 0;

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐