樹心幽徑

« 20181029在ubuntu用c在mysql ilvssell資料庫建立new_outcome資料表並加入一筆資料(t12.c) | Main | 20181101在伺服端建立mysql連線用戶並在用戶端用C程式碼連線 »

20181030在ubuntu用c在mysql ilvssell資料庫建立new_outcome_detail資料表並加入一筆資料(t13.c)
2018/10/30,20:28

(1)程式碼:t13.c

#include <my_global.h>
#include <mysql.h>

int main(int argc, char **argv)
{

  MYSQL *con = mysql_init(NULL);
  if (con == NULL) {
      fprintf(stderr, "%s\n", mysql_error(con));
      exit(1);
  }

  if (mysql_real_connect(con, "localhost", "treehrt", "abc123abc123",
          "ilvssell", 0, NULL, 0) == NULL) {
      fprintf(stderr, "%s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  } 

  if (mysql_query(con, "DROP TABLE IF EXISTS new_outcome_detail")) {
      fprintf(stderr, "%s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }
  printf("DROP TABLE new_outcome_detail ok!\n");
  mysql_query(con, "set names utf8");

  char st[]="CREATE TABLE new_outcome_detail(flush int default 0,sheet char(20) primary key default '20181030A00001',custno char(10) default '-', goodsno char(20),price float,quantity int,itemno int,goodsname char(30),opday char(20) default '2018-10-30 20:25:00')";
 
  if (mysql_query(con, st)) {
      fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1);
  }
  printf("Execute[%s] ok!!\n", st);

  sprintf(st,"%s","insert into new_outcome_detail (flush,sheet,custno,goodsno,price,quantity,itemno,goodsname, opday) values (0,'20181030A00001','312001','a001',15,10,2,'蕃茄', '2018-10-30 20:25:00')");
  if (mysql_query(con, st)) {
      fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1);
  }
  printf("Execute[%s] ok!!\n", st);

  mysql_close(con);
  exit(0);
}

 

(2)編譯

$ gcc t13.c `mysql_config --cflags --libs`

 

(3)執行

$ ./a.out
DROP TABLE new_outcome_detail ok!
Execute[CREATE TABLE new_outcome_detail(flush int default 0,sheet char(20) primary key default '20181030A00001',custno char(10) default '-', goodsno char(20),price float,quantity int,itemno int,goodsname char(30),opday char(20) default '2018-10-30 20:25:00')] ok!!
Execute[insert into new_outcome_detail (flush,sheet,custno,goodsno,price,quantity,itemno,goodsname, opday) values (0,'20181030A00001','312001','a001',15,10,2,'蕃茄', '2018-10-30 20:25:00')] ok!!

 

(4)在mysql命令列檢視結果

mysql> desc new_outcome_detail;
+-----------+----------+------+-----+---------------------+-------+
| Field     | Type     | Null | Key | Default             | Extra |
+-----------+----------+------+-----+---------------------+-------+
| flush     | int(11)  | YES  |     | 0                   |       |
| sheet     | char(20) | NO   | PRI | 20181030A00001      |       |
| custno    | char(10) | YES  |     | -                   |       |
| goodsno   | char(20) | YES  |     | NULL                |       |
| price     | float    | YES  |     | NULL                |       |
| quantity  | int(11)  | YES  |     | NULL                |       |
| itemno    | int(11)  | YES  |     | NULL                |       |
| goodsname | char(30) | YES  |     | NULL                |       |
| opday     | char(20) | YES  |     | 2018-10-30 20:25:00 |       |
+-----------+----------+------+-----+---------------------+-------+
9 rows in set (0.00 sec)

mysql> select * from new_outcome_detail;
+-------+----------------+--------+---------+-------+----------+--------+-----------+---------------------+
| flush | sheet          | custno | goodsno | price | quantity | itemno | goodsname | opday               |
+-------+----------------+--------+---------+-------+----------+--------+-----------+---------------------+
|     0 | 20181030A00001 | 312001 | a001    |    15 |       10 |      2 | 蕃茄      | 2018-10-30 20:25:00 |
+-------+----------------+--------+---------+-------+----------+--------+-----------+---------------------+
1 row in set (0.00 sec)

mysql> quit

迴響

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType