C語言寫數據庫(二)

簡單的實現增刪查改的操作后,實現了一個先讀寫其中一個表的某兩項內容,再把相關字符段寫入到另外一張表中去。涉及到查詢和插入兩個步驟。

其中還涉及到漢字的讀寫和插入,會有字符的操作產生亂碼。所以要先保證mysql的漢字字符編碼,linux終端字符編碼都是統一的。

/*** 
QueryAndInsert
***/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"mysql.h"int main()
{printf("version = 1.0\n");MYSQL mysql;MYSQL_RES * res;MYSQL_ROW row;int r;char* query;char taskID[256],taskName[256];char insert[256];int flag;mysql_init(&mysql);//mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "gdk");if(!mysql_real_connect(&mysql,"localhost","root","mysql123","csql",0,NULL,0)){printf("Can't connect  the mysql ! errInfo = [ %s ] ",mysql_error(&mysql));}else{//query partprintf("connect the mysql successfully! \n");query = "select taskID,taskName from task where taskState=4 order by taskLastEditTime desc";mysql_query(&mysql, "set names utf8");flag = mysql_real_query(&mysql,query,(unsigned int )strlen(query));if(flag){printf("query failed\n");return 0;}else{printf("[%s] made....\n",query);res = mysql_store_result(&mysql);while(row = mysql_fetch_row(res)){    sprintf(taskID,"%s",row[0]);sprintf(taskName,"%s",row[1]);}}}printf("taskID =  %s, taskName = %s\n",taskID,taskName);sprintf(insert,"insert into EnvironmentRealData (TaskID,taskName) values(\'%s\',\'%s\');",taskID,taskName);printf("insert = %s\n",insert);
//insertif(mysql_real_query(&mysql,insert,(unsigned int)strlen(insert))){printf("insert data is faile\n");return 0;}else{printf("Insert successfully\n");}mysql_close(&mysql);return 0; }

gcc編譯:gcc QueryAndInsert.c -o QueryAndInsert -I /usr/inlcude/mysql -L /usr/lib/mysql -lmysqlclient

轉載于:https://www.cnblogs.com/wanghao-boke/p/10979027.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/385054.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/385054.shtml
英文地址,請注明出處:http://en.pswp.cn/news/385054.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

wireshark源代碼分析

各位親&#xff0c;不是我不想回復你們的問題。是我也不了解。不能誤導。希望大家相互幫助。看看能否幫那些提問的小盆友們回復一下呢&#xff1f; 這些都是轉載的&#xff0c;如果實在沒有辦法&#xff0c;可以打開鏈接到原作者哪里去提問試試看。。。 經過多次嘗試&#xf…

C語言寫數據庫(三)

遇到的問題以及解決思路方法 1.外部導入數據庫文件 進入mysql&#xff0c;創建數據庫sh_robot source /home/exbot/sh_robot.sql 查看數據庫編碼格式 show variables like “%char%”; 2.數據庫插入操作 進入相關數據庫&#xff1a;use 數據庫名&#xff1b; 查詢存在該表是否存…

Makefile(一)

在一個文件夾中建一個c文件 //main.c #include<stdio.h> int main() {printf("version 1.0");return 0; } 在當前目錄下編寫makefile文件 //makefile: test : main.o //一種依賴關系聲明&#xff0c;生成test可執行程序需要以來main.o文件gcc -o test main.…

Defunct進程 僵尸進程

在測試基于 DirectFBGstreamer 的視頻聯播系統的一個 Demo 的時候&#xff0c;其中大量使用 system 調用的語句&#xff0c;例如在 menu 代碼中的 system("./play") &#xff0c;而且多次執行&#xff0c;這種情況下&#xff0c;在 ps -ef 列表中出現了大量的 defunc…

make文件基礎用法

參照&#xff1a;https://www.jianshu.com/p/0b2a7cb9a469 創建工作目錄&#xff0c;包含一下文件 main.cperson.cb.hc.h/*** c.h ***/ //this is c.h /*** b.h ***/ //this is b.h /*** main.c ***/ #include<stdio.h> //#include"a1.h" //#include"b.h&…

一個Linux下C線程池的實現(轉)

1.線程池基本原理 在傳統服務器結構中, 常是 有一個總的 監聽線程監聽有沒有新的用戶連接服務器, 每當有一個新的 用戶進入, 服務器就開啟一個新的線程用戶處理這 個用戶的數據包。這個線程只服務于這個用戶 , 當 用戶與服務器端關閉連接以后, 服務器端銷毀這個線程。然而頻繁地…

二維數組作為函數參數

#include<stdio.h> //#include<> //二位數組作為函數參數時&#xff0c;可以不指定第一個下標 void print_buf(int (*p)[3],int a,int b) //void print_buf(int p[][3],int a,int b) {int i,j;for(i 0 ; i < a; i){for(j 0; j < b; j){printf("p[%…

mystrcat

#include<stdio.h> //如果一個數組做為函數的形參傳遞&#xff0c;那么數組可以在被調用的函數中修改 //有時候不希望這個事發生&#xff0c;所以對形參采用const參數 //size_t strlen(const char *s); //strcpy(char* s1,const char* s2); void mystrcat(char *s1,cons…

關于非阻塞的recv的時候返回的處理

注意recv&#xff08;&#xff09;如果讀到數據為0&#xff0c;那么就表示文件結束了&#xff0c;如果在讀的過程中遇到了中斷那么會返回-1&#xff0c;同時置errno為EINTR。 因此判斷recv的條件&#xff1a; 如果read返回<0 如果0 表示文件結束&…

帶參程序

windows環境 #include<stdio.h>int main(int argc, char *argv[]) {printf("argc %d\n", argc);for (int i 0; i < argc; i){printf("argv[%d] %s\n",i, argv[i]);}system("pause");return 0; } windows環境下&#xff0c;帶參函數…

Ubuntu安裝mysql步驟

1.打開終端&#xff0c;輸入&#xff1a; sudo apt-get updata 輸入root用戶密碼 2.更新完畢后&#xff0c;輸入 sudo apt-get install mysql-server ubuntu14.04安裝中間會讓你設置密碼&#xff0c;輸入密碼后點擊確認(mysql123) 3.安裝結束后&#xff0c;查看端口號是否開啟 …

Pthread創建線程后必須使用join或detach釋放線程資源

這兩天在看Pthread 資料的時候&#xff0c;無意中看到這樣一句話(man pthread_detach): Either pthread_join(3) or pthread_detach() should be called for each thread that an application creates, so that system resources for the thread can be released. …

二維數組求平均值(指針的使用)

#include<stdio.h>int main() {int buf[3][5] {{1,2,3,4,5},{4,5,6,7,8},{7,8,9,10,11}};int i;int j;//求行平均值 for(i 0; i < 3; i){int sum 0;for(j 0; j < 5; j){sum (*(*(buf i) j));}printf("sum %d\n",sum/5);}//求列平均值for(i 0; i …

linux終端關閉時為什么會導致在其上啟動的進程退出?

現象 經常在linux下開發的人應該都有這樣的經驗&#xff0c;就是在終端上啟動的程序&#xff0c;在關閉終端時&#xff0c;這個程序的進程也被一起關閉了。看下面這個程序&#xff0c;為了使進程永遠運行&#xff0c;在輸出helloworld后&#xff0c;循環調用sleep&#xff1a; …

二維數組做函數參數傳遞

#include<stdio.h> //#include<> //二位數組作為函數參數時&#xff0c;可以不指定第一個下標 void print_buf(int (*p)[3],int a,int b) //void print_buf(int p[][3],int a,int b) {int i,j;for(i 0 ; i < a; i){for(j 0; j < b; j){printf("p[%…

libevent源碼深度剖析

第一章 1&#xff0c;前言 Libevent是一個輕量級的開源高性能網絡庫&#xff0c;使用者眾多&#xff0c;研究者更甚&#xff0c;相關文章也不少。寫這一系列文章的用意在于&#xff0c;一則分享心得&#xff1b;二則對libevent代碼和設計思想做系統的、更深層次的分析&#xff…

函數返回指針類型(strchr函數)

#include<stdio.h> #include<string.h> char *mystrchr(char *s,char c) {while(*s){if(*s c){return s;}s;}return NULL; }int main() {char str[100] "hello world";//char* s strchr(str,a);char *s mystrchr(str,e);//返回ello world字符串 prin…

函數與指針

#include<stdio.h>int add(int a,int b) {return ab; }int main() {void *p(int,char *); //聲明了一個函數 &#xff0c;函數名為p&#xff0c;函數返回值為void*,函數的 void (*p)(int,char *);//定義了一個指向參數為int和char*返回值為void的函數指針//定義一個參數為…

使用指針在函數中交換數值

#include<stdio.h>void swap(int* a,int *b) {/*int temp *a;*a * b;*b temp;*/*a *b;*b *a - *b;*a *a - *b; }int main() {int a 10;int b 20;swap(&a,&b);printf("a %d,b %d\n",a,b);} 轉載于:https://www.cnblogs.com/wanghao-boke/p/1…

linux C 基于鏈表鏈的定時器

源碼如下&#xff1a;util_timer.h#ifndef LST_TIMER#define LST_TIMER#include <time.h>#include <sys/time.h>#include <stdlib.h>#include <signal.h>#define BUFFER_SIZE 64struct util_timer;/*struct client_data{sockaddr_in address;int sockf…