(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 |