fnngj@fnngj-H24X:~/pyse$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:56)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
mysql> show databases; // 查看當(dāng)前所有的數(shù)據(jù)庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| csvt |
| csvt04 |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.18 sec)
mysql> use test; //作用與test數(shù)據(jù)庫
Database changed
mysql> show tables; //查看test庫下面的表
Empty set (0.00 sec)
//創(chuàng)建user表,name 和password 兩個(gè)字段
mysql> CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); Query OK, 0 rows affected (0.27 sec)
//向user表內(nèi)插入若干條數(shù)據(jù)
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表的數(shù)據(jù)
mysql> select * from user;+------+----------+
| name | password |
+------+----------+
| Tom | 1321 |
| Alen | 7875 |
| Jack | 7455 |
+------+----------+
3 rows in set (0.01 sec)
//刪除name 等于Jack的數(shù)據(jù)
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
//查看表內(nèi)容
mysql> select * from user;+--------+----------+
| name | password |
+--------+----------+
| Tom | 1321 |
| Alen | 1111 |
+--------+----------+
3 rows in set (0.00 sec)
五,python 操作mysql數(shù)據(jù)庫基礎(chǔ)
#coding=utf-8import MySQLdb
conn= MySQLdb.connect(
host='localhost',
port = 3306,
user='root',
passwd='123456',
db ='test',
)
cur = conn.cursor()#創(chuàng)建數(shù)據(jù)表#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")#插入一條數(shù)據(jù)#cur.execute("insert into student values('2','Tom','3 year 2 class','9')")#修改查詢條件的數(shù)據(jù)#cur.execute("update student set class='3 year 1 class' where name = 'Tom'")#刪除查詢條件的數(shù)據(jù)#cur.execute("delete from student where age='9'")cur.close()
conn.commit()
conn.close()