問題描述
如何使用python程序連接到MySQL數據庫?
How do I connect to a MySQL database using a python program?
推薦答案
三步用Python 2連接MYSQL
1 - 設置
您必須先安裝 MySQL 驅動程序,然后才能進行任何操作.與 PHP 不同的是,Python 默認僅安裝 SQLite 驅動程序.最常用的包是MySQLdb,但使用easy_install安裝它很困難.請注意 MySQLdb 僅支持 Python 2.
You must install a MySQL driver before doing anything. Unlike PHP, Only the SQLite driver is installed by default with Python. The most used package to do so is MySQLdb but it's hard to install it using easy_install. Please note MySQLdb only supports Python 2.
對于 Windows 用戶,您可以獲得 MySQLdb 的 exe.
For Windows user, you can get an exe of MySQLdb.
對于Linux,這是一個臨時包(python-mysqldb).(您可以使用 sudo apt-get install python-mysqldb
(對于基于 debian 的發行版)、yum install MySQL-python
(對于基于 rpm 的)或 dnf在命令行中安裝 python-mysql
(用于現代 Fedora 發行版)以進行下載.)
For Linux, this is a casual package (python-mysqldb). (You can use sudo apt-get install python-mysqldb
(for debian based distros), yum install MySQL-python
(for rpm-based), or dnf install python-mysql
(for modern fedora distro) in command line to download.)
對于 Mac,您可以 使用 Macport 安裝 MySQLdb.
For Mac, you can install MySQLdb using Macport.
2 - 用法
安裝后,重新啟動.這不是強制性的,但如果出現問題,它會阻止我在這篇文章中回答 3 或 4 個其他問題.所以請重啟.
After installing, Reboot. This is not mandatory, But it will prevent me from answering 3 or 4 other questions in this post if something goes wrong. So please reboot.
然后就像使用任何其他包一樣:
Then it is just like using any other package :
#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="john", # your username
passwd="megajonhy", # your password
db="jonhydb") # name of the data base
# you must create a Cursor object. It will let
# you execute all the queries you need
cur = db.cursor()
# Use all the SQL you like
cur.execute("SELECT * FROM YOUR_TABLE_NAME")
# print all the first cell of all the rows
for row in cur.fetchall():
print row[0]
db.close()
當然,有成千上萬種可能性和選擇;這是一個非常基本的例子.您將不得不查看文檔.一個好的起點.
Of course, there are thousand of possibilities and options; this is a very basic example. You will have to look at the documentation. A good starting point.
3 - 更高級的用法
一旦你知道它是如何工作的,你可能想要使用 ORM 來避免手動編寫 SQL 并像操作 Python 對象一樣操作您的表.Python 社區中最著名的 ORM 是 SQLAlchemy.
Once you know how it works, You may want to use an ORM to avoid writing SQL manually and manipulate your tables as they were Python objects. The most famous ORM in the Python community is SQLAlchemy.
我強烈建議您使用它:您的生活會輕松得多.
I strongly advise you to use it: your life is going to be much easier.
我最近發現了 Python 世界中的另一顆寶石:peewee.這是一個非常精簡的 ORM,設置和使用非常簡單快捷.它讓我在小型項目或獨立應用程序中度過了一天,在使用 SQLAlchemy 或 Django 等大型工具的情況下是過度的:
I recently discovered another jewel in the Python world: peewee. It's a very lite ORM, really easy and fast to setup then use. It makes my day for small projects or stand alone apps, Where using big tools like SQLAlchemy or Django is overkill :
import peewee
from peewee import *
db = MySQLDatabase('jonhydb', user='john', passwd='megajonhy')
class Book(peewee.Model):
author = peewee.CharField()
title = peewee.TextField()
class Meta:
database = db
Book.create_table()
book = Book(author="me", title='Peewee is cool')
book.save()
for book in Book.filter(author="me"):
print book.title
這個例子開箱即用.除了需要 peewee (pip install peewee
) 外,什么都不需要.
This example works out of the box. Nothing other than having peewee (pip install peewee
) is required.
這篇關于如何在 Python 中連接到 MySQL 數據庫?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!