1. 程式人生 > >簡單講一下C語言連線MySQL資料庫

簡單講一下C語言連線MySQL資料庫

1.首先你得將MySQL安裝上
  apt-get install mysql-server
 (mysql -uroot -p 登陸測試)

2.安裝連結庫
  apt-get install libmysqlclient-dev

3.進行資料庫的建立,例如:
  create table t1(a int, name varchar(10));
  insert into t1 values(1,"Jack");
  insert into t1 values(2,"Smith");
  insert into t1 values(3,"Bruce");

4.進行c語言程式碼的編寫
#include <stdio.h>
#include <stdlib.h>

#include "mysql.h"

int main()
{
   MYSQL m_conn;
   int ret;
  
   mysql_init(&m_conn);
   if(mysql_real_connect(&m_conn, "localhost","root","******","Jack",0,NULL,0))
   {
 //insert
 printf("coneect mysql successful");
 ret = mysql_query(&m_conn, "insert into t1 values(10,'Tony')");
   if(!res)
 {
     printf("insert %lu rows\n",(unsigned long)mysql_affected_rows(&m_conn));
 }
 else
 {
     printf("insert error\n");
 }

  //delete
 char* deleteData = "delete from t1 where a=3";
        res = mysql_real_query(&m_conn, deleteData,(unsigned int)strlen(deleteData));
        if(!res)
 {
    printf("delete successful");
 }
 else
   {
    printf("delete error\n");
 }

 //show all data
        MYSQL_ROW row;
 MYSQL_RES *res;
 int t;
 char *allData = "select * from t1";
 res = mysql_real_query(&m_conn, allData, (unsigned int)strlen(allData));
 
 res = mysql_store_result(&m_conn); //
     while(row=mysql_fetch_row(res))
 {
    for(t=0; t<mysql_num_fields(res); t++)
    {
  printf("%s\t",row[t]);
     }
    printf("\n");
 }
 
 
 mysql_close(&m_conn);
    }
   return 0;
}


4.編譯時注意加上鍊接庫啥的
 gcc -I/usr/include/mysql mysqlTest.c -L/usr/lib/mysql -lmysqlclient -o mysqlTest
 ./mysqlTest