2.1 Mysql 数据库的简单使用
mysql> show databases; // 查看当前所有的数据库 +--------------------+ | Database | +--------------------+ | information_schema | | csvt | | csvt04 | | mysql | | performance_schema | | test | +--------------------+ rows in set (0.18 sec) mysql> use test; //作用与test数据库 Database changed mysql> show tables; //查看test库下面的表 Empty set (0.00 sec) //创建user表,name 和password 两个字段 mysql> CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); Query OK, 0 rows affected (0.27 sec) //向user表内插入若干条数据 mysql> insert into user values('Tom','1321'); Query OK, 1 row affected (0.05 sec) mysql> insert into user values('Alen','7875'); Query OK, 1 row affected (0.08 sec) mysql> insert into user values('Jack','7455'); Query OK, 1 row affected (0.04 sec) //查看user表的数据 mysql> select * from user; +------+----------+ | name | password | +------+----------+ | Tom | 1321 | | Alen | 7875 | | Jack | 7455 | +------+----------+ rows in set (0.01 sec) //删除name 等于Jack的数据 mysql> delete from user where name = 'Jack'; Query OK, 1 rows affected (0.06 sec) //修改name等于Alen 的password 为 1111 mysql> update user set password='1111' where name = 'Alen'; Query OK, 1 row affected (0.05 sec) Rows matched: 1 Changed: 1 Warnings: 0 //查看表内容 mysql> select * from user; +--------+----------+ | name | password | +--------+----------+ | Tom | 1321 | | Alen | 1111 | +--------+----------+ rows in set (0.00 sec)
2.2 Python 操作Mysq数据库
#coding=utf-8 import MySQLdb conn= MySQLdb.connect( host='localhost', port = 3306, user='root', passwd='123456', db ='test', ) cur = conn.cursor() #创建数据表 #cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))") #插入一条数据 #cur.execute("insert into student values('2','Tom','3 year 2 class','9')") #修改查询条件的数据 #cur.execute("update student set class='3 year 1 class' where name = 'Tom'") #删除查询条件的数据 #cur.execute("delete from student where age='9'") cur.close() conn.commit() conn.close()