mariadb數據庫中終於建了一個普通賬號gsc,密碼是個秘密,反正我知道。登陸mysql賬號進行建表,報了個錯誤,CREATE command denied to user 'gsc'@'localhost' for table 'stu_info,沒有權限建表;
數據庫中普通用戶權限設置的問題,無法進行創建表。只需給對應賬戶,賦予所有的權限即可:
1、給用戶權限:
mysql>grant all privileges on student_info.* to gsc; //student_info是數據庫,gsc是操縱student_info的用戶
2、刷新系統設置:
mysql>flush privileges; //刷新系統權限表。
新開一個終端:
1、mysql登陸數據庫:
mysql -ugsc -p
Enter password:
2、創建數據庫並使用數據庫:
MariaDB [(none)]> show databases; //查看數據庫 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.001 sec) MariaDB [(none)]> create database student_info; //創建數據庫student_info Query OK, 1 row affected (0.001 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | student_info | +--------------------+ 4 rows in set (0.001 sec) MariaDB [(none)]> use student_info; //使用數據庫student_info Database changed
3、在數據庫student_info中創建表:
MariaDB [student_info]> create table stu_info( //創建表 -> stuId int(11), //->是sql語句的提示符號,不是輸入命令的一個部分 -> stuName varchar(40), -> gender varchar(10), -> phone int(11), -> email varchar(40), -> account varchar(40), -> code varchar(40), -> adminId int(1)); Query OK, 0 rows affected (0.166 sec) MariaDB [student_info]> show tables; //查看數據student_info中表 +------------------------+ | Tables_in_student_info | +------------------------+ | stu_info | +------------------------+ 1 row in set (0.001 sec) MariaDB [student_info]> describe stu_info; //查看表stu_info基本結構 +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | stuId | int(11) | YES | | NULL | | | stuName | varchar(40) | YES | | NULL | | | gender | varchar(10) | YES | | NULL | | | phone | int(11) | YES | | NULL | | | email | varchar(40) | YES | | NULL | | | account | varchar(40) | YES | | NULL | | | code | varchar(40) | YES | | NULL | | | adminId | int(1) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 8 rows in set (0.002 sec) MariaDB [student_info]> show create table stu_info; //查看表stu_info詳細結構語句 +----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | stu_info | CREATE TABLE `stu_info` ( `stuId` int(11) DEFAULT NULL, `stuName` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `gender` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `phone` int(11) DEFAULT NULL, `email` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `account` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `code` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `adminId` int(1) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci | +----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.000 sec)
C語言代碼獲取stu_info的表結構信息: Makefile
sqlGetHead:sqlGetHead.c gcc -Wall -g -I/usr/include/mariadb -o sqlGetHead sqlGetHead.c -lmysqlclient
c代碼sqlGetHead.c
1 //This is c program code! 2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= 3 * 文檔信息: *** :~/WORKM/sqlGetHead.c 4 * 版權聲明: *** :(魎魍魅魑)MIT 5 * 聯絡信箱: *** :guochaoxxl@163.com 6 * 創建時間: *** :2020年11月23日的下午02:37 7 * 文檔用途: *** :數據結構與算法分析-c語言描述 8 * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl) 9 * 修訂時間: *** :2020年第47周 11月23日 星期一 下午02:37 (第328天) 10 * 文件描述: *** :自行添加 11 * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/ 12 #include <stdio.h> 13 #include <stdlib.h> 14 #include <mysql/mysql.h> 15 #include <string.h> 16 17 #define USER "gsc" 18 #define PWD "******" 19 #define DATABASENAME "student_info" 20 #define TABLENAME "stu_info" 21 22 int main(int argc, char** argv) 23 { 24 MYSQL conn; 25 MYSQL_RES res; 26 27 char tableName[30]; 28 char query[1024]; 29 unsigned short numFields; 30 char column[30][40]; 31 32 //1.初始化 33 mysql_init(&conn); 34 35 //2.連接數據庫 36 if(!mysql_real_connect(&conn, "localhost", USER, PWD, DATABASENAME, 0, NULL, 0)) 37 { 38 fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(&conn) ); 39 return -1; 40 } 41 //3.選擇表stu_info 42 strcpy(tableName, TABLENAME); 43 sprintf(query, "select * from %s", tableName); 44 mysql_query(&conn, query); 45 46 //4.取出表stu_info中選擇的內容 47 res = *mysql_store_result(&conn); 48 printf("%s\n", &res); 49 50 //5.統計表stu_info字段 51 numFields = mysql_num_fields(&res); 52 printf("%d\n", numFields); 53 54 //6.打印表stu_info字段 55 for(int i = 0; i < numFields; ++i) 56 { 57 strcpy(column[i], mysql_fetch_field(&res)->name); 58 printf("%s\n", column[i]); 59 } 60 61 return 0; 62 }
make結果:
gcc -Wall -g -I/usr/include/mariadb -o sqlGetHead sqlGetHead.c -lmysqlclient
執行
sqlGetHead 8 stuId stuName gender phone email account code adminId
代碼簡單,不再啰嗦