樹心幽徑

« 20181101在伺服端建立mysql連線用戶並在用戶端用C程式碼連線 | Main | 20181105調減new_goods資料表中指定貨品編號(gdsno)的貨品庫存量(stock_qty) t15.c »

20181104修改new_cust資料表中某一客戶(custno)的儲值金額(remain) t14.c
2018/11/04,09:03

(1) code : t14.c

treehrt@hrt:~/mysqlc$ cat t14.c
//gcc t14.c `mysql_config --cflags --libs`
#include <my_global.h>
#include <mysql.h>
#define HOST "localhost"
#define USER "treehrt"
#define PWD "abc123abc123"
#define DB "ilvssell"

typedef struct CUSTREC
{
  //char uid[10];
  double remain;
  //char inyear[5];
  //char dept[10];
  //char grade[10];
  //char class[10];
  char cardid[10];
  char uname[10];
  //char pid[10];
  //char addr[80];
  //char tel[15];
  //char oldclass[10];
  //char sno[2];
  //int lostc;
  //char gen_code;
  //char in_use;
  //char cid[4];

} custRec ;
void finish_with_error(MYSQL *con)
{
  fprintf(stderr, "%s\n", mysql_error(con));
  mysql_close(con);
  exit(1);       
}

int  GetCustRecByNo(char *custno, custRec *pv)
{
  char st[280];
  sprintf(st,"select cardid,UName,remain from new_cust where cardid='%s'",custno);

  MYSQL *con = mysql_init(NULL);
  if (con == NULL) finish_with_error(con);

  if (mysql_real_connect(con, HOST, USER, PWD, DB, 0, NULL, 0) == NULL) finish_with_error(con);
  mysql_query(con, "set names utf8");

  if (mysql_query(con, st)) finish_with_error(con);
//  printf("SQL: [ %s ] Execute ok! \n\n", st);

  MYSQL_RES *result = mysql_store_result(con);
  if (result == NULL) finish_with_error(con);

  MYSQL_ROW row;
  row = mysql_fetch_row(result);
  if (row){
    strcpy(pv->cardid,row[0]);
    strcpy(pv->uname,row[1]);
    pv->remain=atof(row[2]);
    mysql_free_result(result);
    return 1;
  }
  else {
    printf("can not found cardid: %s!!\n",custno);
    mysql_free_result(result);
    return 0;
    }
}

void  add_custs_remain(char *custno, double moneyadd)
{
    char st[500];
        sprintf(st,"update new_cust set remain=remain+(%.2f) where cardid='%s'",moneyadd,custno);
    MYSQL *con = mysql_init(NULL);
    if (con == NULL)  finish_with_error(con);
    if (mysql_real_connect(con, HOST , USER, PWD, DB, 0, NULL, 0) == NULL) finish_with_error(con);
    mysql_query(con, "set names utf8");
    if (mysql_query(con, st)) finish_with_error(con);
      printf("SQL指令: [%s] 執行成功! \n\n", st);
}
void  update_custs_remain(char *custno, double remain)
{
    char st[500];
        sprintf(st,"update new_cust set remain=%.2f where cardid='%s'",remain,custno);
    MYSQL *con = mysql_init(NULL);
    if (con == NULL)  finish_with_error(con);
    if (mysql_real_connect(con, HOST , USER, PWD, DB, 0, NULL, 0) == NULL) finish_with_error(con);
    mysql_query(con, "set names utf8");
    if (mysql_query(con, st)) finish_with_error(con);
    printf("SQL: [ %s ] Execute ok! \n\n", st);
}
int main(int argc, char **argv)
{
  char custno[20];
  printf("顧客儲值程式\n輸入顧客編號gdsno=");
  scanf("%s",custno);

  custRec cR;
  GetCustRecByNo(custno, &cR);
  printf("儲值前 %s[%s] 帳戶餘額=%.2f\n",custno,cR.uname,cR.remain);

  double moneyadd=0;
  printf("輸入要儲值的金額 moneyadd=");
  scanf("%lf",&moneyadd);
  add_custs_remain(custno, moneyadd);


  GetCustRecByNo(custno, &cR);
  printf("儲值後 %s[%s] 帳戶餘額=%.2f\n",custno,cR.uname,cR.remain);
  exit(0);
}

 

(2)編譯

treehrt@treehrt-fire:~/mysqlc$ gcc t14.c `mysql_config --cflags --libs`

(3)執行結果

treehrt@hrt:~/mysqlc$ ./a.out
顧客儲值程式
輸入顧客編號gdsno=812001
儲值前 812001[王一志] 帳戶餘額=6956.00
輸入要儲值的金額 moneyadd=-4000
SQL指令: [update new_cust set remain=remain+(-4000.00) where cardid='812001'] 執行成功!

儲值後 812001[王一志] 帳戶餘額=2956.00
treehrt@hrt:~/mysqlc$

 

迴響

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType