湖濱散記部落格的樹心幽徑[login][主頁]
406:20180605在ubuntu用c在mysql ilvssell資料庫新增一個資料表new_goods並新增一筆資料

(1)新增資料表new_goods  程式碼(t5)摘要

:

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

 mysql_query(con, "set names utf8");

 char st[]="CREATE TABLE new_goods (gdsno char(20) primary key, gdsname char(30) default 'x', spec char(10) default '個',cost float default 0 ,price_in float default 0, price_special float default 0, price_sell float default 0, unit char(10) default '個' , stock_qty int default 0, fac_no char(20) default '0', mem varchar(80) default 'none' )";

  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 t5.c `mysql_config --cflags --libs`

treehrt@hrt:~/mysqlc$ ./a.out
MySQL client version: 5.7.22
Prepare CREATE TABLE new_goods (gdsno char(20) primary key, gdsname char(30) default 'x', spec char(10) default '個',cost float default 0 ,price_in float default 0, price_special float default 0, price_sell float default 0, unit char(10) default '個' , stock_qty int default 0, fac_no char(20) default '0', mem varchar(80) default 'none' )
Execute ok!  CREATE TABLE new_goods (gdsno char(20) primary key, gdsname char(30) default 'x', spec char(10) default '個',cost float default 0 ,price_in float default 0, price_special float default 0, price_sell float default 0, unit char(10) default '個' , stock_qty int default 0, fac_no char(20) default '0', mem varchar(80) default 'none' )

 

(3)在mysql命令列查看結果:

mysql> use ilvssell;
Database changed


mysql> desc new_goods;
+---------------+-------------+------+-----+---------+-------+
| Field         | Type        | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| gdsno         | char(20)    | NO   | PRI | NULL    |       |
| gdsname       | char(30)    | YES  |     | x       |       |
| spec          | char(10)    | YES  |     | 個      |       |
| cost          | float       | YES  |     | 0       |       |
| price_in      | float       | YES  |     | 0       |       |
| price_special | float       | YES  |     | 0       |       |
| price_sell    | float       | YES  |     | 0       |       |
| unit          | char(10)    | YES  |     | 個      |       |
| stock_qty     | int(11)     | YES  |     | 0       |       |
| fac_no        | char(20)    | YES  |     | 0       |       |
| mem           | varchar(80) | YES  |     | none    |       |
+---------------+-------------+------+-----+---------+-------+
11 rows in set (0.00 sec)

(4)新增一筆資料到new_goods  程式碼(t6)摘要

mysql_query(con, "set names utf8");

char st[]="INSERT INTO new_goods (GDSNO,GDSNAME,SPEC, COST,PRICE_in,PRICE_SPECIAL,PRICE_SELL, UNIT,STOCK_QTY,FAC_NO,MEM) VALUES ('a001','蕃茄','個', 10,10,20,20,'個',200,'077','金農行') ";
  if (mysql_query(con, st))
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }

(5)編譯並執行

eehrt@hrt:~/mysqlc$ gcc t6.c `mysql_config --cflags --libs`
treehrt@hrt:~/mysqlc$ ./a.out
MySQL client version: 5.7.22
Prepare INSERT INTO new_goods (GDSNO,GDSNAME,SPEC, COST,PRICE_in,PRICE_SPECIAL,PRICE_SELL, UNIT,STOCK_QTY,FAC_NO,MEM) VALUES ('a001','蕃茄','個', 10,10,20,20,'個',200,'077','金農行')
Execute ok! 

(6)查看

treehrt@hrt:~/mysqlc$ mysql -p -uroot

mysql> use ilvssell;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from new_goods;
+-------+---------+------+------+----------+---------------+------------+------+-----------+--------+-----------+
| gdsno | gdsname | spec | cost | price_in | price_special | price_sell | unit | stock_qty | fac_no | mem       |
+-------+---------+------+------+----------+---------------+------------+------+-----------+--------+-----------+
| a001  | 蕃茄    | 個   |   10 |       10 |            20 |         20 | 個   |       200 | 077    | 金農行    |
+-------+---------+------+------+----------+---------------+------------+------+-----------+--------+-----------+
1 row in set (0.00 sec)

 


select id,article_id,topic,text from lt_articles_text where article_id =406; ok. update lt_articles set num_reads=num_reads +1 where id=406; ok.