python 操作 mqsql 数据库
来源:互联网 发布:征服者数据更新 编辑:程序博客网 时间:2024/05/22 04:30
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'))
阅读全文
0 0
- python 操作 mqsql 数据库
- JDBC-mqsql基本操作`
- MqSQL数据库的差集如何实现?Minus?
- Python操作Mysql数据库
- python的数据库操作
- python操作数据库
- python SQlite数据库操作
- python操作MySQL数据库
- python操作MySQL数据库
- python操作数据库
- Python 数据库操作
- python操作MySQL数据库
- python操作MySQL数据库
- python操作MySQL数据库
- Python 操作Oralce数据库
- python操作MySQL数据库
- python数据库操作
- Python操作Oracle数据库
- 粒子群算法求函数极值
- 解决AndroidStudio导入项目在 Building gradle project info 一直卡住
- Rally安装问题记录
- Camera安卓源码剖析-源码中的设计模式实例
- Core ML and Vision
- python 操作 mqsql 数据库
- Gcc编译出错处理--openssl 依赖问题
- MySql免安装版本配置步骤
- JAVA数据结构之线性表的链式存储结构——循环链表
- win10如何安装思科VPN client
- overflow-y在ios上滑动卡顿问题
- PopupWindonw的简单应用
- iOS学习之如何生成.mobileprovision文件
- 百度ai开发者大会讲了点什么