python 操作 mqsql 数据库
参考网址:http://www.runoob.com/python/python-mysql.html
http://www.cnblogs.com/fnng/p/3565912.html
1、安装mqsql客户端和服务器
略过
2、安装MySQLdb
MySQLdb是python操作mysql数据库的一个python库,提供了一些api接口
1)下载 MySQLdb的tar包
网址:https://pypi.python.org/pypi/MySQL-python/1.2.5
2)安装 MySQLdb
unzip MySQL-python-1.2.5.zip
cd MySQL-python-1.2.5
python setup.y build
sudo python setup.py install
3) 测试是否 成功安装 MySQLdb
python
import MySQLdb
没有错误就说明成功了
4)MySQLdb 的API文档为
网址:http://mysql-python.sourceforge.net/MySQLdb-1.2.2/
3、使用MySQLdb
(1)查看mysql 数据库
[@localhost bin]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 63545
Server version: 5.5.53-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 63545
Server version: 5.5.53-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| committeeserverdb |
| datadefdb |
| gamerecorderdb |
| growdb |
| logerrdb |
| logindb |
| matchhistorydb |
| mysql |
| performance_schema |
| python |
| statcommdb |
| taskdatadb |
| test |
| userinfodb |
| waredb |
+--------------------+
16 rows in set (0.00 sec)
+--------------------+
| Database |
+--------------------+
| information_schema |
| committeeserverdb |
| datadefdb |
| gamerecorderdb |
| growdb |
| logerrdb |
| logindb |
| matchhistorydb |
| mysql |
| performance_schema |
| python |
| statcommdb |
| taskdatadb |
| test |
| userinfodb |
| waredb |
+--------------------+
16 rows in set (0.00 sec)
mysql> use python
Database changed
mysql> show tables;
+------------------+
| Tables_in_python |
+------------------+
| user |
+------------------+
1 row in set (0.00 sec)
mysql> select * from user;
+------+----------+
| name | password |
+------+----------+
| Tom | 1321 |
| Alen | 7875 |
| Jack | 7455 |
+------+----------+
3 rows in set (0.00 sec)
mysql>
Database changed
mysql> show tables;
+------------------+
| Tables_in_python |
+------------------+
| user |
+------------------+
1 row in set (0.00 sec)
mysql> select * from user;
+------+----------+
| name | password |
+------+----------+
| Tom | 1321 |
| Alen | 7875 |
| Jack | 7455 |
+------+----------+
3 rows in set (0.00 sec)
mysql>
2)编写python.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import MySQLdb
if __name__ == '__main__':
conn = MySQLdb.connect(host='localhost',port = 3306,user='root', passwd='123456',db ='python',)
cur = conn.cursor()
cur.execute("select * from user")
data = cur.fetchall()
print data
cur.close()
conn.close()
# -*- coding: UTF-8 -*-
import MySQLdb
if __name__ == '__main__':
conn = MySQLdb.connect(host='localhost',port = 3306,user='root', passwd='123456',db ='python',)
cur = conn.cursor()
cur.execute("select * from user")
data = cur.fetchall()
print data
cur.close()
conn.close()
3)执行结果
[@localhost mysql]$ ./mysqltest.py
(('Tom', '1321'), ('Alen', '7875'), ('Jack', '7455'))
(('Tom', '1321'), ('Alen', '7875'), ('Jack', '7455'))