c語言也能幹大事之向資料庫存入資料

2021-06-12 22:45:18 字數 2150 閱讀 4440

用c語言連線mysql資料庫,連線資料庫的框架**是如鵬網的板書中獲得的,本例實現了從本地檔案中讀取資料存入mysql資料庫的功能。

本地資料如下:

將資料按使用者名稱、密碼的形式存入test資料庫中的user表裡面。

具體實現**:

#define login_timeout 30

#define maxbuflen 255

#define checkdbstmterror(hwnd,result,hstmt) if(sql_error==result);

sqlinteger sqlerr;

long erg2 = sqlgetdiagrec(type, sqlhandle,1,

(sqlchar *)pstatus,&sqlerr,(sqlchar

*)pmsg,100,&sqlmsglen);

wsprintf(error,"%s (%d)\n",pmsg,(int)sqlerr);

messagebox(hwnd,error,text("資料庫執行錯誤 "),mb_iconerror|mb_ok);

} void showdbconnerror(hwnd hwnd,sqlhdbc hdbc)

void showdbstmterror(hwnd hwnd,sqlhstmt hstmt)

//主要功能實現的函式

void dbtest(hwnd hwnd)

;server=127.0.0.1;uid=root;pwd=root;database=test;charset =gbk;";

sqlchar connstrout[maxbuflen];

//分配環境控制代碼

result = sqlallochandle(sql_handle_env, sql_null_handle,

&henv);

//設定管理環境屬性

result = sqlsetenvattr(henv, sql_attr_odbc_version,

(void*)sql_ov_odbc3, 0);

//分配連線控制代碼

result = sqlallochandle(sql_handle_dbc, henv, &hdbc);

//設定連線屬性

result = sqlsetconnectattr(hdbc, sql_login_timeout,(void*)login_timeout, 0);

//連線資料庫

result = sqldriverconnect(hdbc,null,

connstrin,sql_nts,

connstrout,maxbuflen,

(sqlsmallint

*)0,sql_driver_noprompt);

if(sql_error==result)

//初始化語句控制代碼

result = sqlallochandle(sql_handle_stmt, hdbc, &hstmt);

//sql_nts telling the function the previous parameter is null-terminated string,

//please alculate the string length for me

file *fp;

if ((fp=fopen("d:","rt"))==null)

unsigned int j;

char buf[40];

while(fscanf(fp,"%s %d",buf,&j)!=-1)

sqlfreestmt(hstmt,sql_close);

sqldisconnect(hdbc);

sqlfreehandle(sql_handle_dbc,hdbc);

sqlfreehandle(sql_handle_env,henv);

messagebox(hwnd,text("執行成功"),text("標題"),mb_ok);

}

資料庫中的資料:

C語言也能幹大事 第2講筆記

開發windows程式 2.控制項 在resourceview裡面,選擇dialog,可以拖動按鈕,在控制項的工具欄裡面,有相應的控制項。3.作業 做乙個註冊介面 4.先學windows程式設計 不需要mfc,只需要懂得c語言mfc不過就是把api封裝了一層又一層 mfc亂七八糟的東西太多,隱藏了太...

C語言也能幹大事 第十一講 Socket網路程式設計

網路程式設計 計算機與計算機之間的通訊。網域名稱 ip位址 dns伺服器。手機聯絡人 號碼 乙個計算機可以同時執行多個網路程式,引入了埠port 與就是乙個計算機上可以併發執行多個網路程式,而不會再互相之間產生干擾 c s b s結構。p2p程式 即時客戶端,優勢伺服器端。tcp傳輸控制協議 udp...

c語言向自定陣列 資料結構之用C語言實現定義陣列

include include include define max array dim 8 define ok 1 define error 0 define overflow 1 typedef int status typedef int elemtype typedef structarra...