樹心幽徑

« 20180605在ubuntu用c在mysql ilvssell資料庫新增一個資料表new_goods並新增一筆資料 | Main | 20180608安裝並使用osCommerce »

20180608在ubuntu用c在mysql ilvssell資料庫新增一個資料表new_factory並新增一筆資料
2018/06/08,13:02

(1)

treehrt@hrt:~/mysqlc$ cat t7.c
:

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



 mysql_query(con, "set names utf8");

 char st[]="CREATE TABLE new_factory (facno char(20) primary key, facname char(30) default 'x', simplify char(10) default 'x',phone1 char(20) default '0823' , phone2 char(20) default '0', fax char(20) default '0', intername char(20) default '0', interphone char(20) default '0', master char(10) default '0' , facaddr char(40) default '0' , comaddr char(40) default '-' , lastdeal char(10) default '-', rate char(10) default '-' ,invoice char(20) default '-', accountd char(20) default '-', ps char(40) default '0')";

  printf("Prepare %s\n", st);

  if (mysql_query(con, st))
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }
  printf("Execute ok!  %s\n", st);

 

(2)

treehrt@hrt:~/mysqlc$ gcc t7.c `mysql_config --cflags --libs`

(3)

treehrt@hrt:~/mysqlc$ ./a.out
MySQL client version: 5.7.22
DROP TABLE new_factory ok!
Prepare CREATE TABLE new_factory (facno char(20) primary key, facname char(30) default 'x', simplify char(10) default 'x',phone1 char(20) default '0823' , phone2 char(20) default '0', fax char(20) default '0', intername char(20) default '0', interphone char(20) default '0', master char(10) default '0' , facaddr char(40) default '0' , comaddr char(40) default '-' , lastdeal char(10) default '-', rate char(10) default '-' ,invoice char(20) default '-', accountd char(20) default '-', ps char(40) default '0')
Execute ok!

 

(4) 用mysql命令列指令列出new_factory資料表欄位結構:

mysql> desc new_factory;
+------------+----------+------+-----+---------+-------+
| Field      | Type     | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+-------+
| facno      | char(20) | NO   | PRI | NULL    |       |
| facname    | char(30) | YES  |     | x       |       |
| simplify   | char(10) | YES  |     | x       |       |
| phone1     | char(20) | YES  |     | 0823    |       |
| phone2     | char(20) | YES  |     | 0       |       |
| fax        | char(20) | YES  |     | 0       |       |
| intername  | char(20) | YES  |     | 0       |       |
| interphone | char(20) | YES  |     | 0       |       |
| master     | char(10) | YES  |     | 0       |       |
| facaddr    | char(40) | YES  |     | 0       |       |
| comaddr    | char(40) | YES  |     | -       |       |
| lastdeal   | char(10) | YES  |     | -       |       |
| rate       | char(10) | YES  |     | -       |       |
| invoice    | char(20) | YES  |     | -       |       |
| accountd   | char(20) | YES  |     | -       |       |
| ps         | char(40) | YES  |     | 0       |       |
+------------+----------+------+-----+---------+-------+
16 rows in set (0.00 sec)

(5)  寫入一筆資料到new_factory資料表C程式碼t8.c 摘述:

 

 mysql_query(con, "set names utf8");

 char st[]="insert into new_factory (facno,facname,phone1,master, ps,accountd,invoice,rate,lastdeal,comaddr,facaddr, interphone,intername,fax,phone2,simplify) values ('707','金門農場','082-333274','吳小花','-',123456,'0','0','0','addr','-', '082-333274','-','-','-','-') ";

  printf("Prepare %s\n", st);

  if (mysql_query(con, st))
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }
  printf("Execute ok!  %s\n", st);

 

(6) 編譯並執行

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

treehrt@treehrt-fire:~/mysqlc$ ./a.out
MySQL client version: 5.7.22
Prepare insert into new_factory (facno,facname,phone1,master, ps,accountd,invoice,rate,lastdeal,comaddr,facaddr, interphone,intername,fax,phone2,simplify) values ('707','金門農場','082-333274','吳小花','-',123456,'0','0','0','addr','-', '082-333274','-','-','-','-')
Execute ok! 

 

(7)

mysql> select * from new_factory;
+-------+--------------+----------+------------+--------+------+-----------+------------+-----------+---------+---------+----------+------+---------+----------+------+
| facno | facname      | simplify | phone1     | phone2 | fax  | intername | interphone | master    | facaddr | comaddr | lastdeal | rate | invoice | accountd | ps   |
+-------+--------------+----------+------------+--------+------+-----------+------------+-----------+---------+---------+----------+------+---------+----------+------+
| 707   | 金門農場     | -        | 082-333274 | -      | -    | -         | 082-333274 | 吳小花    | -       | addr    | 0        | 0    | 0       | 123456   | -    |
+-------+--------------+----------+------------+--------+------+-----------+------------+-----------+---------+---------+----------+------+---------+----------+------+
1 row in set (0.00 sec)

 

迴響

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType