c調用資料庫
㈠ c語言如何調用Mysql資料庫文件並進行對資料庫的操作呢。
MYSQL m_sqlCon;//聲明
mysql_init(&m_sqlCon);//初始化
mysql_real_connect(&m_sqlCon, "127.0.0.1", abc, "root", "hibernate", atoi("3306"),NULL,0)//鏈接
mysql_query(&m_sqlCon, "SET NAMES GB2312"); //設置查詢編碼格式
res = mysql_query(&m_sqlCon,"select * from ms_sendlist where flag = 1 order by style desc");//查詢
mysql_query(&m_sqlCon, sql);//插入,刪除
㈡ c語言怎麼連接mysql資料庫
如鵬網上有詳細的視頻教程,楊中科的C語言也能幹大事,裡面講得很清楚。要是在這里講需要寫很多東西,累手,還沒有視頻直觀
㈢ c與資料庫連接的詳細步驟
C#連接資料庫有以下幾個步驟:
1:使用配置的資料庫連接串,創建資料庫連接 Connection 對象
2:構建操作的sql語句
3:定義command對象
4:打開數據連接
5:執行命令
舉一個例子,刪除操作
public class StudentService
{
//從配置文件中讀取資料庫連接字元串
private readonly static string connString = ConfigurationManager.ConnectionStrings["accpConnectionString"].ToString();
private readonly static string dboOwner = ConfigurationManager.ConnectionStrings["DataBaseOwner"].ToString();
AdoNetModels.Student model = new Student();
#region 刪除數據1
public int DeleteStudent(int stuID)
{
int result = 0;
// 資料庫連接 Connection 對象
SqlConnection connection = new SqlConnection(connString);
// 構建刪除的sql語句
string sql = string.Format("Delete From Student Where stuID={0}", stuID);
// 定義command對象
SqlCommand command = new SqlCommand(sql, connection);
try
{
connection.Open();
result = command.ExecuteNonQuery(); // 執行命令
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
connection.Close();
}
return result;
}
#endregion
㈣ C語言資料庫是什麼
資料庫是用來存入數據的倉庫。用戶可以對文件中的數據進行新增、查詢、更新、刪除等操作。但是C語言和資料庫是兩個東西,他們之間的關系就是C語言可以用來開發資料庫管理軟體,也可以通過C語言藉助於SQL語句來操作資料庫。
C語言普適性最強的一種計算機程序編輯語言,它不僅可以發揮出高級編程語言的功用,還具有匯編語言的優點,因此相對於其它編程語言,它具有自己獨特的特點。具體體現在以下三個方面:
其一,廣泛性。C 語言的運算范圍的大小直接決定了其優劣性。C 語言中包含了34種運算符,因此運算范圍要超出許多其它語言,此外其運算結果的表達形式也十分豐富。此外,C 語言包含了字元型、指針型等多種數據結構形式,因此,更為龐大的數據結構運算它也可以應付。
其二,簡潔性。9 類控制語句和32個KEYWORDS是C語言所具有的基礎特性,使得其在計算機應用程序編寫中具有廣泛的適用性,不僅可以適用廣大編程人員的操作,提高其工作效率,同 時還能夠支持高級編程,避免了語言切換的繁瑣。
(4)c調用資料庫擴展閱讀
資料庫架構
1、內層:最接近實際存儲體,亦即有關數據的實際存儲方式。
2、外層:最接近用戶,即有關個別用戶觀看數據的方式。
3、概念層:介於兩者之間的間接層。
㈤ 用C語言怎麼實現與資料庫的連接
#include<mysql/mysql.h>
#include<stdio.h>
intmain()
{
MYSQL*conn;
MYSQL_RES*res;
MYSQL_ROWrow;
char*server="localhost";//本地連接
char*user="root";//
char*password="525215980";//mysql密碼
char*database="student";//資料庫名
char*query="select*fromclass";//需要查詢的語句
intt,r;
conn=mysql_init(NULL);
if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))
{
printf("Errorconnectingtodatabase:%s ",mysql_error(conn));
}else{
printf("Connected... ");
}
t=mysql_query(conn,query);
if(t)
{
printf("Errormakingquery:%s ",mysql_error(conn));
}else{
printf("Querymade... ");
res=mysql_use_result(conn);
if(res)
{
while((row=mysql_fetch_row(res))!=NULL)
{
//printf("num=%d ",mysql_num_fields(res));//列數
for(t=0;t<mysql_num_fields(res);t++)
printf("%8s",row[t]);
printf(" ");
}
}
mysql_free_result(res);
}
mysql_close(conn);
return0;
}
(5)c調用資料庫擴展閱讀
C語言使用注意事項:
1、指針是c語言的靈魂,一定要靈活的使用它:
(1)、指針的聲明,創建,賦值,銷毀等
(2)、指針的類型轉換,傳參,回調等
2、遞歸調用也會經常用到:
(1)、遞歸遍歷樹結構
(2)、遞歸搜索
㈥ 用C語言如何對MySQL資料庫進行操作
里的大部分代碼參考了MySQL發行包裡面的.c源文件,大家也可以去裡面找找相關的代碼,下面這段代碼實現了連接到本地MySQL伺服器上9tmd_bbs_utf8資料庫,從數據表tbb_user中根據輸入的userid取得該用戶的用戶名並列印輸出到終端。
if defined(_WIN32) || defined(_WIN64)為了支持windows平台上的編譯
#include <windows.h> #endif #include <stdio.h> #include <stdlib.h> #include "mysql.h"
我的機器上該文件在/usr/local/include/mysql下
定義MySQL資料庫操作的宏,也可以不定義留著後面直接寫進代碼
define SELECT_QUERY "select username from tbb_user where userid = %d" int main(int argc, char **argv)char **argv 相當於 char *argv[] {
MYSQL mysql,*sock;定義資料庫連接的句柄,它被用於幾乎所有的MySQL函數
MYSQL_RES *res;查詢結果集,結構類型
MYSQL_FIELD *fd ;包含欄位信息的結構
MYSQL_ROW row ;存放一行查詢結果的字元串數組
char qbuf[160];存放查詢sql語句字元串
if (argc != 2) { //檢查輸入參數 fprintf(stderr,"usage : mysql_select <userid>\n\n"); exit(1); } mysql_init(&mysql); if (!(sock = mysql_real_connect(&mysql,"localhost","dbuser","dbpwd","9tmd_bbs_utf8",0,NULL,0))) { fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql)); perror(""); exit(1); } sprintf(qbuf,SELECT_QUERY,atoi(argv[1])); if(mysql_query(sock,qbuf)) { fprintf(stderr,"Query failed (%s)\n",mysql_error(sock)); exit(1); } if (!(res=mysql_store_result(sock))) { fprintf(stderr,"Couldn't get result from %s\n", mysql_error(sock)); exit(1); } printf("number of fields returned: %d\n",mysql_num_fields(res)); while (row = mysql_fetch_row(res)) { printf("Ther userid #%d 's username is: %s\n", atoi(argv[1]),(((row[0]==NULL)&&(!strlen(row[0]))) ? "NULL" : row[0])) ; puts( "query ok !\n" ) ; } mysql_free_result(res); mysql_close(sock); exit(0); return 0;
為了兼容大部分的編譯器加入此行
}
編譯的時候,使用下面的命令
gcc -o mysql_select ./mysql_select.c -I/usr/local/include/mysql -L/usr/local/lib/mysql -lmysqlclient (-lz) (-lm) 後面兩個選項可選,根據您的環境情況運行的時候,執行下面的命令
./mysql_select 1
將返回如下結果:
number of fields returned: 1 Ther userid #1 's username is: Michael query ok !
上面的代碼我想大部分都能看明白,不明白的可以參考一下MySQL提供的有關C語言API部分文檔,各個函數都有詳細說明,有時間我整理一份常用的API說明出來。
㈦ 怎樣在C++中調用MYSQL資料庫中的數據
1、用CAPI連接MySQL資料庫有兩個步驟:
1)初始化一個連接句柄
2)穗虧拿建立連接
所用到的函數如下:
MYSQL *mysql_init(MYSQL *connection); // 初始化連接句柄
//成功返回MySQL結構指針,失敗返回NULL
MYSQL *mysql_real_connect(MYSQL *connection,
const char *server_host,
const char *sql_user_name,
const char *sql_password,
const char *db_name,
unsigned int port_number,
const char *unix_socket_name,
unsigned int flags); //建立連接
//成功返回MySQL結構指針,失敗返回NULL
以下是完整實例:
#include <iostream>
#include <fstream>
#include <猜搭cstdlib>
#include <mysql/mysql.h>
using namespace std;
void mysql_err_function(MYSQL * connection);
int main()
{
//freopen("input.txt","r",stdin);
MYSQL * connection;
connection = mysql_init(NULL);
if (!connection)
{
cout << "mysql_init failed!" << endl;
exit(-1);
}
if (!mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0))
{
cout <<空叢 "Connection To MySQL failed!" << endl;
mysql_err_function(connection);
}
cout << "Connection To MySQL Server is Success..." << endl;
string str;
getline(cin,str);
int res = 0;
int affected_count = 0;
while (str != "close" && str != "" && !res)
{
res = mysql_query(connection,str.c_str());
affected_count += mysql_affected_rows(connection);
if (res)
{
if (mysql_errno(connection))
{
cout << "Error " << mysql_errno(connection) << " : "
<< mysql_error(connection) << '\n' << endl;
break;
}
}
getline(cin,str);
}
cout << "Have affected " << affected_count << " rows!" << endl;
mysql_close(connection);
cout << "Connection To MySQL Server is closed..." << endl;
return 0;
}
void mysql_err_function(MYSQL * connection)
{
if (mysql_errno(connection))
{
cout << "Error " << mysql_errno(connection) << " : "
<< mysql_error(connection) << endl;
exit(-1);
}
}
㈧ 如何在C/C++程序中使用資料庫
一般要看使用的資料庫。如果 操作 sql server 需要用到 ADO 驅動,這種驅動使用MFC做的包裝類比較多一些,在控制台直接編寫代碼可能稍顯繁瑣。
如果操作mysql,在安裝mysql的時候,有相應的include頭文件和庫文件,可以在自己的IDE開發環境中進行設置。