时间:2021-07-01 10:21:17 帮助过:41人阅读
CREATE DATABASE
IF NOT EXISTS
db_name
CHARACTER SET UTF8
COLLATE utf8_general_ci;
SHOW DATABASES;
SHOW CREATE DATABASE db_name;
ALTER DATABASE db_name CHARACTER SET GBK;
DROP DATABASE IF EXISTS db_name;
USE db_name;
-- 创建一张表的指令 CREATE TABLE table_name( column_name1 type1, column_name1 type1, column_name1 type1 )CHARACTER SET UTF8 COLLATE utf8_general_ci;
实例
CREATE TABLE SCHOOL_TABLE(
ID INT(10),
NAME CHAR(10)
?
)CHARACTER SET UTF8 COLLATE UTF8_GENERAL_CI;
DESC table_name;
SHOW TABLES;
SHOW CREATE TABLE table_name;
-- 在表 table_name 中添加了类型为 new_type 一列
newcolumn_name [FIRST | AFTER column_name]
ALTER TABLE table_name ADD newcolumn_name new_type FIRST;
实例
ALTER TABLE school_table ADD num INT FIRST;
ALTER TABLE table_name MODIFY column_name new_type;
实例
ALTER TABLE school_table MODIFY ID CHAR
ALTER TABLE table_name CHANGE column_name new_column_name new_type;
实例
ALTER TABLE SCHOOL_TABLE CHANGE NUM ABC CHAR;
ALTER TABLE table_name DROP column_name;
DROP TABLE table_name;
INSERT INTO stu_info VALUE(1, "小明", "男", 1, now());
INSERT INTO stu_info(name, class) VALUE("小红", 2);
INSERT INTO stu_info VALUES (1, "小明", ‘男‘, 1, "2010-06-07 10:54:08"), (2, "小红", ‘女‘, 2, "2011-06-07 10:54:08"), (3, "小黄", ‘女‘, 3, "2012-06-07 10:54:08"), (4, "小绿", ‘男‘, 5, "2013-06-07 10:54:08"), (5, "小清", ‘女‘, 3, "2014-06-07 10:54:08"), (6, "小紫", ‘男‘, 2, "2015-06-07 10:54:08"), (7, "小白", ‘女‘, 2, "2016-06-07 10:54:08"), (8, "小黑", ‘女‘, 1, "2017-06-07 10:54:08");
UPDATE stu_info SET sex=‘男‘;
UPDATE stu_info SET sex=‘男‘ WHERE name="小清";
DELETE FROM stu_info WHERE name="小紫";
DELETE FROM stu_info;
ALTER TABLE stu_info MODIFY id int PRIMARY KEY;
CREATE TABLE stu_info( id int PRIMARY KEY AUTO_INCREMENT, name varchar(20) UNIQUE KEY, sex ENUM(‘男‘, ‘女‘), classid INT NOT NULL, score INT default 0, start_data DATETIME DEFAULT now() )CHARACTER SET UTF8 COLLATE utf8_general_ci;
CREATE TABLE class_info( id int PRIMARY KEY AUTO_INCREMENT, name varchar(20) NOT NULL )CHARACTER SET UTF8 COLLATE utf8_general_ci;
ALTER TABLE stu_info ADD FOREIGN KEY stu_info(classid) REFERENCES class_info(id);
SELECT * FROM stu_info;
SELECT name, sex FROM stu_info;
SELECT DISTINCT classid FROM stu_info;
SELECT name, score*0.6 FROM stu_info;
SELECT name as 姓名, score*0.6 as 成绩 FROM stu_info;
SELECT * FROM stu_info WHERE classid > 1;
SELECT * FROM stu_info WHERE classid IN (1, 3, 5);
SELECT * FROM stu_info WHERE classid BETWEEN 3 AND 5;
SELECT * FROM stu_info WHERE name LIKE "%g";
SELECT * FROM stu_info WHERE name LIKE "%g" OR sex="女";
SELECT * FROM stu_info ORDER BY start_data ASC;
SELECT count(*) FROM stu_info;
SELECT AVG(score) FROM stu_info;
SELECT SUM(score) FROM stu_info;
SELECT classid 班级 ,count(*) 人数 from stu_info GROUP BY classid
SELECT classid 班级 ,count(*) 人数 from stu_info GROUP BY classid HAVING classid IN (1,3);
SELECT * FROM stu_info ORDER BY score DESC LIMIT 0, 3
SELECT * FROM stu_info, class_info;
SELECT * FROM stu_info, class_info WHERE stu_info.classid = class_info.id;
SELECT * FROM stu_info right join class_info on stu_info.classid = class_info.id;
SELECT DISTINCT * FROM class_info WHEREid IN ( SELECT classid FROM stu_info WHERE sex = "女" );
SELECT * FROM stu_info, class_info WHERE EXISTS( SELECT score FROM stu_info WHERE score > 80 ) AND stu_info.classid = class_info.id ORDER BY score DESC LIMIT 0, 3
SELECT * FROM stu_info, class_info WHERE score=ANY ( -- 女生的成绩 SELECT score FROM stu_info WHERE sex="女" )AND stu_info.classid = class_info.id AND sex=‘男‘;
SELECT * FROM stu_info, class_info WHERE stu_info.score >ALL ( SELECT score FROM stu_info WHERE sex="男" )AND stu_info.classid = class_info.id AND sex="女";
mysqldump -u root -p tankwar_net > d:/tankwar_net_10_15.sql
mysqldump -u root -p tankwar_net < d:/tankwar_net_10_15.sql
mysql> use tankwar_net Database changed mysql> source d:/tankwar_net_10_15.sql;
1.在Mysql安装目录下把【include文件夹、libmysql.dll、libmysql.lib】3个拷贝到项目文件
#include <iostream> using namespace std; ? // 1. 包含 mysql 需要用到的头文件,应用程序的环境要和mysql一致 #include "include/mysql.h" // 2. 链接到 mysql 提供的 lib 文件 #pragma comment(lib, "libmysql.lib") ? // 用于执行没有返回值的 sql 指令 void exec(MYSQL & mysql, LPCSTR sql) { // 1. 使用函数执行 sql 指令 mysql_query(&mysql, sql); ? // 2. 对结果进行判断 if (mysql_errno(&mysql) != 0) { // 使用 mysql_error 输出错误的原因 printf("error: %s\n", mysql_error(&mysql)); system("pause"); ExitProcess(0); } } ? // 用于执行有返回值的 sql 指令 void select(MYSQL& mysql, LPCSTR sql) { // 1. 使用函数执行 sql 指令 mysql_query(&mysql, sql); ? // 2. 对结果进行判断 if (mysql_errno(&mysql) != 0) { // 使用 mysql_error 输出错误的原因 printf("error: %s\n", mysql_error(&mysql)); system("pause"); ExitProcess(0); } ? // 3. 获取 sql 指令的执行结果 MYSQL_RES* mysql_res = mysql_use_result(&mysql); ? // 4. 获取查询到的结果的列数 int count = mysql_num_fields(mysql_res); ? // 5. 通过获取到的列数便利获取所有的列名 MYSQL_FIELD* mysql_field = mysql_fetch_fields(mysql_res); for (int i = 0; i < count; ++i) { // 排除掉空指针的情况 if (mysql_field[i].name == nullptr) printf("none\t"); else printf("%s\t", mysql_field[i].name); } printf("\n"); ? // 6. 循环输出每一行的信息 MYSQL_ROW mysql_row = { 0 }; while (mysql_row = mysql_fetch_row(mysql_res)) { // 根据列数输出每一行的信息 for (int i = 0; i < count; ++i) { // 排除掉空指针的情况 if (mysql_row[i] == nullptr) printf("none\t"); else printf(u8"%s\t", mysql_row[i]); } printf("\n"); } } ? int main(int argc, char* argv[]) { // 3. 初始化 mysql 库,需要提供命令行参数 mysql_library_init(argc, argv, nullptr); ? // 4. 初始化 mysql 对象,之后对数据库的操作都需要传入 MYSQL mysql = { 0 }; mysql_init(&mysql); ? // 5. 连接到数据库 mysql_real_connect(&mysql, "127.0.0.1", "root", "123456", "school_table", 3306, nullptr, 0); ? // 6. 使用 mysql_errno 判断数据库是否连接成功 if (mysql_errno(&mysql) != 0) { // 使用 mysql_error 输出错误的原因 printf("error: %s\n", mysql_error(&mysql)); system("pause"); return 0; } ? // 7. 执行数据的添加操作 exec(mysql, "INSERT INTO class_info(name) VALUE(‘123‘);"); ? // 8. 执行数据的查询操作 select(mysql, "select * from stu_info;"); ? // 9. 清理工作 mysql_library_end(); ? return 0; }
# encoding: utf8 ? import pymysql ? ? # mysql 类,用于操作数据库 class Mysql: ? # 构造函数,用于获取连接对象和游标 def __init__(self): try: # 1.获取连接对象 self.connect = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123456", db="chat_table") # 2.获取游标对象 self.cursor = self.connect.cursor() except Exception as info: print(info) ? # 实例方法,用于执行没有返回值的语句 def insert(self, sql): try: # 执行 mysql 指令 self.cursor.execute(sql) # 必须要提交数据,否则不会影响数据库 self.connect.commit() except Exception as info: # 回滚操作 self.connect.rollback() # 输出错误信息 print(info) ? # 实例方法,用于执行有返回值的语句 def select(self, sql): try: # 执行 mysql 指令 self.cursor.execute(sql) # 获取到总的列数 count = self.cursor.rowcount # 获取查询到的所有的数据 return count, self.cursor.fetchall() except Exception as info: # 回滚操作 self.connect.rollback() # 输出错误信息 print(info) ? ? ? ? if __name__ == "__main__": mysql = Mysql() ? # 返回的是一个元组,保存的是由每一行的信息组成的一个元组 # 想要获取第一行的第一列需要使用 rows[0][0] print(mysql.select("select * from chat_user;")) ? # user = "xiaogang" # pswd = "xiaogangdemima" # mysql.insert("INSERT INTO chat_user(user_name, user_pswd) VALUE(‘%s‘, md5(‘%s‘))" % (user, pswd))
Mysql基本操作、C++Mysql简单应用、PythonMysql简单应用
标签:exit 数据 ber none ios date 学分 argv 分组