您的位置:首页 > 编程语言 > Python开发

【脚本语言系列】关于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数据库进行操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐