湖濱散記部落格的樹心幽徑[login][主頁]
428:20181029在ubuntu用c在mysql ilvssell資料庫建立new_outcome資料表並加入一筆資料(t12.c)

(1) 程式碼:

treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ cat t12.c
#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);
  } 

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

  char st[]="CREATE TABLE new_outcome(flush int default 0,sheet char(20) primary key default '20181029A00001',custno char(10) default '-', amount int default 0,opday char(20) default '2018-10-29 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 (flush,sheet,custno,amount,opday) values (0,'20181029A00001','712003',550,'2018-10-29 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)編譯:

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

(3)執行

treehrt@treehrt-BM6AE-BM1AE-BP1AE:~/mysqlc$ ./a.out
MySQL client version: 5.7.22
DROP TABLE new_outcome ok!
Execute[CREATE TABLE new_outcome(flush int default 0,sheet char(20) primary key default '20181029A00001',custno char(10) default '-', amount int default 0,opday char(20) default '2018-10-29 20:25:00')] ok!!
Execute[insert into new_outcome (flush,sheet,custno,amount,opday) values (0,'20181029A00001','712003',550,'2018-10-29 20:25:00')] ok!!

 

(4)在mysql命令列查看

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> desc new_outcome;
+--------+----------+------+-----+---------------------+-------+
| Field  | Type     | Null | Key | Default             | Extra |
+--------+----------+------+-----+---------------------+-------+
| flush  | int(11)  | YES  |     | 0                   |       |
| sheet  | char(20) | NO   | PRI | 20181029A00001      |       |
| custno | char(10) | YES  |     | -                   |       |
| amount | int(11)  | YES  |     | 0                   |       |
| opday  | char(20) | YES  |     | 2018-10-29 20:25:00 |       |
+--------+----------+------+-----+---------------------+-------+
5 rows in set (0.00 sec)

mysql> select * from new_outcome;
+-------+----------------+--------+--------+---------------------+
| flush | sheet          | custno | amount | opday               |
+-------+----------------+--------+--------+---------------------+
|     0 | 20181029A00001 | 712003 |    550 | 2018-10-29 20:25:00 |
+-------+----------------+--------+--------+---------------------+
1 row in set (0.00 sec)

 


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