【脚本语言系列】关于Python数据库处理MySQL, 你需要知道的事
2017-05-25 16:33
1336 查看
如何使用MySQL
使用命令行创建数据库
mysql> CREATE DATABASE python; # create a database named python
使用数据库
mysql> USE python; # use the created data
创建表
mysql> CREATE TABLE people (name VARCHAR(30), age INT, sex CHAR(1)); # create a table named people
添加两条表
mysql> INSERT INTO people VALUES('Tom', 20, 'M'); mysql> INSERT INTO people VALUES('Jack', NULL, NULL);
查看创建表
mysql> SELECT * FROM people;
退出
mysql> exit
使用Python MySQLdb处理数据库
适用于Win 32bit(≤Python2.7, x86) 的MySQLdb,其由Andy Dustman编写,已停止更新;
[版本:1.2.4]
https://sourceforge.net/projects/mysql-python/files/mysql-python-test/1.2.4b4/
[版本:1.2.5]
https://pypi.python.org/pypi/MySQL-python/1.2.5
https://github.com/farcepest/MySQLdb1
# -*- coding:utf-8 -*- # import MySQLdb db = MySQLdb.connect(host = 'localhost', user = 'root', passwd = 'root', db = 'python') # connect MySQL, add host, add user, add passwd, add db cur = db.cursor() # get the db cursor cur.execute('insert into people (name, age, sex) values (\'Jee\',21,\'F\')') # execute SQL sentence, add record r = cur.execute('delete from people where age = 20') # execute SQL sentence, del record con.commit() # commit change r = cur.execute('select * from people') # execute SQL sentence, get record r = cur.fetchall() # get data print r # print data cur.close() # close cursor db.close()
注意,此处可能有报错:
Traceback (most recent call last): import _mysql ImportError: DLL load failed: %1 不是有效的 Win32 应用程序。
原因分析:Win 64bit无法使用Win 32bit下编译的dll文件。
解决方案:使用MySQL.connector进行处理。
使用Python MySQL Connector处理数据库
适用于Win 32bit/64bit的Python 2.7, Python 3.3, Python 3.4版本
https://dev.mysql.com/downloads/connector/python/
# -*- coding:utf-8 -*- # import mysql.connector db = mysql.connector.connect(host = 'localhost', user = 'root', passwd = 'root', db = 'python') # connect MySQL, add host, add user, add passwd, add db cur = db.cursor() # get the db cursor cur.execute('insert into people (name, age, sex) values (\'Jee\',21,\'F\')') # execute SQL sentence, add record r = cur.execute('delete from people where age = 20') # execute SQL sentence, del record db.commit() # commit change r = cur.execute('select * from people') # execute SQL sentence, get record r = cur.fetchall() # get data print r # print data cur.close() # close cursor db.close() # close connection
什么是MySQL
MySQL是一个小巧的多用户,多线程SQL数据库服务器。MySQL是一个客户机/服务器结构的实现,其由一个服务器守护进程和客户程序组成。
MySQL提供了对SQL语句的支持。在Python中可以使用MySQLdb模块连接到MySQL, 对MySQL数据库进行操作。
相关文章推荐
- 【脚本语言系列】关于Python数据库处理SQLite数据库,你需要知道的事
- 【脚本语言系列】关于PythonNoSQL数据库处理memcached,你需要知道的事
- 【脚本语言系列】关于PythonNoSQL数据库处理dbm,你需要知道的事
- 【脚本语言系列】关于PythonNoSQL数据库处理redis,你需要知道的事
- 【脚本语言系列】关于Python数据库处理SQLAlchemy,你需要知道的事
- 【脚本语言系列】关于Python数据库处理SQLite,你需要知道的事
- 【脚本语言系列】关于Python基础知识异常处理,你需要知道的事
- 【脚本语言系列】关于Python异常处理,你需要知道的事
- 【脚本语言系列】关于Python数据库访问专用模块,你需要知道的事
- 【脚本语言系列】关于Python数据库访问DAO, 你需要知道的事
- 【脚本语言系列】关于Python结构化二进制文件处理HDF5,你需要知道的事
- 【脚本语言系列】关于Python图像处理scikits-image, 需要知道的事
- 【脚本语言系列】关于Python音频处理WMPlayerOCX,你需要知道的事情
- 【脚本语言系列】关于Python结构化文本文件处理YAML,你需要知道的事
- 【脚本语言系列】关于Python基础知识处理字符串,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理Avro,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理MsgPack,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理csv,你需要知道的事
- 【脚本语言系列】关于 Python 数据库, 你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理HTML,你需要知道的事