· 5 years ago · Apr 13, 2020, 04:12 AM
1#include <stdlib.h>
2#include <iostream>
3#include <mysql.h>
4#include <stdio.h>
5
6void finish_with_error(MYSQL *Connect)
7 {
8 fprintf(stderr, "%s\n", mysql_error(Connect));
9 mysql_close(Connect);
10 exit(1);
11 }
12
13int main()
14{
15 MYSQL *Connect = mysql_init(NULL);
16 MYSQL *Statement;
17 MYSQL * ResultSet;
18
19
20 if(Connect == NULL)
21 {
22 fprintf(stderr, "%s\n", mysql_error(Connect));
23 exit(1);
24 }
25
26 if (mysql_query(Connect, "DROP TABLE IF EXISTS TTU")) {
27 finish_with_error(Connect);
28 }
29
30
31
32
33 if (mysql_real_connect(Connect, "localhost", "root", "password", NULL, 0, NULL, 0) == NULL) {
34
35 fprintf(stderr, "%s\n", mysql_error(Connect));
36 mysql_close(Connect);
37 exit(1);
38
39 }
40
41 if(mysql_query(Connect, "CREATE DATABASE TTU"))
42 {
43 fprintf(stderr, "%s\n", mysql_error(Connect));
44 mysql_close(Connect);
45 exit(1);
46
47 }
48
49
50
51 if (mysql_query(Connect, "CREATE TABLE students(tnumber char(8) primary key, firstname varchar(20) not NULL, lastname varchar(20) not NULL, dateofbirth date, credits numeric(3,0), ENGINE=INNODB"))
52 {
53 finish_with_error(Connect);
54 }
55
56 if (mysql_query(Connect, "INSERT INTO students VALUES('00001234','Joe','Smith','1950-08-12',35)"))
57 {
58 finish_with_error(Connect);
59 }
60
61 if (mysql_query(Connect, "SELECT * FROM students"))
62 {
63 finish_with_error(Connect);
64
65 }
66
67 mysql_close(Connect);
68 exit(0);
69}