C語言操作mysql增刪改查

2021-10-01 21:22:24 字數 4326 閱讀 7451

資料庫:zhang

表:people

表字段:name(varchar)、age(int)、***(varchar)、grade(varchar)

#include #include #include int main()

/**嘗試與執行在主機上的mysql資料庫引擎建立連線

* localhost為資料庫連線的主機

* root為mysql使用者名稱

* 123456為mysql的密碼

* zhang為連線到的資料庫名稱

* 後面為預設引數

*/if (mysql_real_connect(&mysql,"localhost","root","123456","zhang",0,null,0) == null)

printf("connected mysql successful! \n");

//往表中插入資料

//sprintf(query_str, "insert into people values ('ffff', 30, 'woman', '85')");

//往表中插入資料,資料為變數的寫法

sprintf(query_str, "insert into people values ('%s', '%d', '%s', '%s')", "ffff", 30, "woman", "85");

rc = mysql_real_query(&mysql, query_str, strlen(query_str));

if (0 != rc)

sprintf(query_str, "select * from people");

rc = mysql_real_query(&mysql, query_str, strlen(query_str));

if (0 != rc)

res = mysql_store_result(&mysql);

if (null == res)

rows = mysql_num_rows(res);

printf("the total rows is: %d\n", rows);

fields = mysql_num_fields(res);

printf("the total fields is: %d\n", fields);

while ((row = mysql_fetch_row(res)))

printf("\n");

}mysql_close(&mysql);

return 0;

}

#include #include #include int main()

/**嘗試與執行在主機上的mysql資料庫引擎建立連線

* localhost為資料庫連線的主機

* root為mysql使用者名稱

* 123456為mysql的密碼

* zhang為連線到的資料庫名稱

* 後面為預設引數

*/if (mysql_real_connect(&mysql,"localhost","root","123456","zhang",0,null,0) == null)

printf("connected mysql successful! \n");

//刪除資料,刪除表中name為ffff的資料

sprintf(query_str, "delete from people where name = 'ffff'");

rc = mysql_real_query(&mysql, query_str, strlen(query_str));

if (0 != rc)

sprintf(query_str, "select * from people");

rc = mysql_real_query(&mysql, query_str, strlen(query_str));

if (0 != rc)

res = mysql_store_result(&mysql);

if (null == res)

rows = mysql_num_rows(res);

printf("the total rows is: %d\n", rows);

fields = mysql_num_fields(res);

printf("the total fields is: %d\n", fields);

while ((row = mysql_fetch_row(res)))

printf("\n");

}mysql_close(&mysql);

return 0;

}

#include #include #include int main()

/**嘗試與執行在主機上的mysql資料庫引擎建立連線

* localhost為資料庫連線的主機

* root為mysql使用者名稱

* 123456為mysql的密碼

* zhang為連線到的資料庫名稱

* 後面為預設引數

*/if (mysql_real_connect(&mysql,"localhost","root","123456","zhang",0,null,0) == null)

printf("connected mysql successful! \n");

//修改資料,將表中name為ffff的那條資料中age修改為20

sprintf(query_str, "update people set age = 20 where name = 'aaaa'");

rc = mysql_real_query(&mysql, query_str, strlen(query_str));

if (0 != rc)

sprintf(query_str, "select * from people");

rc = mysql_real_query(&mysql, query_str, strlen(query_str));

if (0 != rc)

res = mysql_store_result(&mysql);

if (null == res)

rows = mysql_num_rows(res);

printf("the total rows is: %d\n", rows);

fields = mysql_num_fields(res);

printf("the total fields is: %d\n", fields);

while ((row = mysql_fetch_row(res)))

printf("\n");

}mysql_close(&mysql);

return 0;

}

#include #include #include int main()

/**嘗試與執行在主機上的mysql資料庫引擎建立連線

* localhost為資料庫連線的主機

* root為mysql使用者名稱

* 123456為mysql的密碼

* zhang為連線到的資料庫名稱

* 後面為預設引數

*/if (mysql_real_connect(&mysql,"localhost","root","123456","zhang",0,null,0) == null)

printf("connected mysql successful! \n");

//查詢資料庫中的資料

sprintf(query_str, "select * from people");

rc = mysql_real_query(&mysql, query_str, strlen(query_str));

if (0 != rc)

res = mysql_store_result(&mysql);

if (null == res)

rows = mysql_num_rows(res);

printf("the total rows is: %d\n", rows);

fields = mysql_num_fields(res);

printf("the total fields is: %d\n", fields);

while ((row = mysql_fetch_row(res)))

printf("\n");

}mysql_close(&mysql);

return 0;

}

MySQL 增刪改查操作

toc 登入資料庫 mysql u root p123456 建立資料庫 creat database test 檢視所有資料庫 show databases 檢視資料庫中所有的資料表 show tables 選中資料庫 usedatabases 建立資料表 create table pet nam...

MySQL增刪改查操作

增刪改查操作 查詢表中的所有的記錄 select from 表名 xs 建立資料庫 create database if not exists xsgl 8.2建立表 cerate table if not exists 判斷是否存在 表名 xsb 8.3刪除 drop database if ex...

C 語言連線mysql 執行 增 刪 改 查 操作

c 語言連線mysql api介紹 1.mysql mysql init mysql mysql 描述 分配或初始化與mysql real connect 相適應的mysql物件。如果mysql是null指標,該函式將分配 初始化 並返回新物件。否則,將初始化物件,並返回物件的位址。如果mysql ...