湖濱散記部落格的樹心幽徑[login][主頁]
425:20181010在ubuntu用c在mysql ilvssell資料庫new_id_reg資料表加入一筆資料(t11.c)

(1) code : t11.c

treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ cat t11.c
//gcc t11.c `mysql_config --cflags --libs`
#include <my_global.h>
#include <mysql.h>

int main(int argc, char **argv)
{
  printf("MySQL client version: %s\n", mysql_get_client_info());

  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);
  } 


 mysql_query(con, "set names utf8");

 char st[]="insert into new_id_reg (sta_no,inday,custno,money_store, saleno,remain,serial,oldremain) values ('A','2018-10-10 21:24:00','312001',5000, 'X',6100,'A2018101000001',1100)";
  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);

  mysql_close(con);

  exit(0);
}

 

(2)compile

treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ gcc t11.c `mysql_config --cflags --libs`

 

(3)執行

treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ ./a.out
MySQL client version: 5.7.22
Prepare insert into new_id_reg (sta_no,inday,custno,money_store, saleno,remain,serial,oldremain) values ('A','2018-10-10 21:24:00','312001',5000, 'X',6100,'A2018101000001',1100)
Execute ok!  insert into new_id_reg (sta_no,inday,custno,money_store, saleno,remain,serial,oldremain) values ('A','2018-10-10 21:24:00','312001',5000, 'X',6100,'A2018101000001',1100)
treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$

 

(4)用mysql命令列查詢結果

treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ mysql -utreehrt -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.22-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ilvssell           |
+--------------------+
2 rows in set (0.00 sec)

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> show tables;
+--------------------+
| Tables_in_ilvssell |
+--------------------+
| new_cust           |
| new_factory        |
| new_goods          |
| new_id_reg         |
+--------------------+
4 rows in set (0.00 sec)

mysql> select * from new_id_reg;
+--------+---------------------+--------+-------------+--------+--------+----------------+-----------+
| sta_no | inday               | custno | money_store | saleno | remain | serial         | oldremain |
+--------+---------------------+--------+-------------+--------+--------+----------------+-----------+
| A      | 2018-10-10 21:24:00 | 312001 |        5000 | X      |   6100 | A2018101000001 |      1100 |
+--------+---------------------+--------+-------------+--------+--------+----------------+-----------+
1 row in set (0.00 sec)

mysql> quit
Bye

 


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