使用c語言對mysql進行增刪查改
#include"stdafx.h"
#include<winsock2.h>//這個是必須新增的,不然就會出現102個錯誤
#include"mysql.h"
#include<my_global.h>
#include<stdio.h>
#include<stdlib.h>
staticMYSQL*conn; // 連線connection
//初始化函式,主要是進行資料庫連線等工作
void init(){
char*server= "localhost"; // 資料庫所在主機的ip
char*user = "root";
char*password= "huachao"; //root使用者的密碼
char*database= "test"; // 操作的資料庫名
//連線到mysql資料庫
conn=mysql_init(NULL);
//根據具體的配置資訊連結資料庫.
if(!mysql_real_connect(conn , server, user, password, database, 0, NULL,0)) {
printf("Conection error : %s\n",mysql_error(conn ));
exit(1);
}
}
//通過sql語句進行插入資料
bool insertBySQL(char*str){
/**//*執行插入語句*/
if(mysql_query(conn,str)){
printf("執行插入失敗");
returnfalse;
}else{
printf("插入成功,受影響行數:%lu\n",(ulong)mysql_affected_rows(conn));
returntrue;
}
}
//通過sql語句來對資料庫表進行修改資料
void updateBySQL(char*sql){
/**//*執行插入語句*/
if(mysql_query(conn,sql)){
printf("修改失敗");
}else{
printf("
}
}
//根據sql語句來對資料庫表進行查詢
voidgetBySQL(char *sql,intcolNum){
MYSQL_RES *res; //結果
MYSQL_ROW row; //結果行(一行一行)
//將查詢傳送到資料庫
if(mysql_query(conn, sql))
{
printf("MySQL query error : %s\n",mysql_error(conn));
exit(1);
}
res=mysql_use_result(conn);
while((row = mysql_fetch_row(res)) !=NULL)
{
printf("%s\t%s\t%s\n", row[0],row[1],row[2]);
}
mysql_free_result(res);
}
void deleteBySQL(char*sql){
if(mysql_query(conn,sql)){
printf("刪除失敗");
}else{
printf("刪除成功,受影響行數:%lu\n",(ulong)mysql_affected_rows(conn));
}
}
int_tmain(intargc,_TCHAR* argv[])
{
init();
//insertBySQL("insertinto test(name,password) values('hh','22')");
//getBySQL("select* from test",2);
//updateBySQL("updatetest set name='lisi' where id=2");
deleteBySQL("delete from test where id=5");
mysql_close(conn);
getchar();
return0;
}
出現如下錯誤:
error LNK2019: 無法解析的外部符號 [email protected],該符號在函式 _main 中被引用
error LNK2019: 無法解析的外部符號 [email protected],該符號在函式 _main 中被引用
error LNK2019: 無法解析的外部符號 [email protected],該符號在函式 _main 中被引用
error LNK2019: 無法解析的外部符號 [email protected],該符號在函式 _main 中被引用
如果按照之前的步驟還會出現這種錯誤,可能是因為新建的專案是win32,而作業系統和mysql軟體都市64位的,此時如下解決:
右擊專案->屬性->配置管理器,如圖:
然後在“活動解決方案平臺”點選下拉,“新建”選擇x64即可