如何使用python程序连接MySQL数据库?
当前回答
Oracle (MySQL)现在支持纯Python连接器。这意味着不需要安装二进制文件:它只是一个Python库。它叫做“连接器/Python”。
http://dev.mysql.com/downloads/connector/python/
安装完成后,您可以在这里看到一些使用示例
其他回答
这里有一种方法,使用MySQLdb,它只支持Python 2:
#!/usr/bin/python
import MySQLdb
# Connect
db = MySQLdb.connect(host="localhost",
user="appuser",
passwd="",
db="onco")
cursor = db.cursor()
# Execute SQL select statement
cursor.execute("SELECT * FROM location")
# Commit your changes if writing
# In this case, we are only reading data
# db.commit()
# Get the number of rows in the resultset
numrows = cursor.rowcount
# Get and display one row at a time
for x in range(0, numrows):
row = cursor.fetchone()
print row[0], "-->", row[1]
# Close the connection
db.close()
参考这里
尝试使用MySQLdb。MySQLdb只支持Python 2。
有一个如何页面在这里:http://www.kitebird.com/articles/pydbapi.html
原文如下:
# server_version.py - retrieve and display database server version
import MySQLdb
conn = MySQLdb.connect (host = "localhost",
user = "testuser",
passwd = "testpass",
db = "test")
cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version:", row[0]
cursor.close ()
conn.close ()
Oracle (MySQL)现在支持纯Python连接器。这意味着不需要安装二进制文件:它只是一个Python库。它叫做“连接器/Python”。
http://dev.mysql.com/downloads/connector/python/
安装完成后,您可以在这里看到一些使用示例
对于较新版本的Python (>=3.6)
使用mysqlclient或pymysql(推荐)。
对于较旧版本的Python (<3.7, 2.4 <= Python <= 2.7)
如果您正在使用较旧版本的Python(不幸的是),那么您也可以尝试-> oursql。
但是请注意,该项目不再维护,错误修复也不会推送。
作为一个db驱动程序,还有oursql。这个链接列出了一些原因,说明了为什么oursql更好:
oursql有真正的参数化,将SQL和数据完全分开发送到MySQL。
Oursql允许文本或二进制数据流进数据库和流出数据库,而不是要求所有内容都缓冲在客户端。 Oursql既可以惰性地插入行,也可以惰性地获取行。 Oursql默认支持unicode。 oursql支持python 2.4到2.7,在2.6+上没有任何弃用警告(见PEP 218),在2.7上也没有完全失败(见PEP 328)。 Oursql在python 3.x上本机运行。
那么如何连接mysql与oursql?
与mysqldb非常相似:
import oursql
db_connection = oursql.connect(host='127.0.0.1',user='foo',passwd='foobar',db='db_name')
cur=db_connection.cursor()
cur.execute("SELECT * FROM `tbl_name`")
for row in cur.fetchall():
print row[0]
文档中的教程相当不错。
当然,对于ORM来说,SQLAlchemy是一个很好的选择,正如已经在其他答案中提到的那样。
Python没有内置的库来与MySQL交互,所以为了在MySQL数据库和Python之间建立连接,我们需要为我们的Python环境安装MySQL驱动程序或模块。
pip install mysql-connector-python
MySQL - connector - Python是一个开源的Python库,可以用几行代码将你的Python代码连接到MySQL数据库。而且它与最新版本的Python非常兼容。
安装MySQL -connector-python后,可以使用下面的代码片段连接到MySQL数据库。
import mysql.connector
Hostname = "localhost"
Username = "root"
Password ="admin" #enter your MySQL password
#set connection
set_db_conn = mysql.connector.connect(host= Hostname, user=Username, password=Password)
if set_db_conn:
print("The Connection between has been set and the Connection ID is:")
#show connection id
print(set_db_conn.connection_id)
连接Django和MySQL
在Django中,要将你的模型或项目连接到MySQL数据库,你需要安装mysqlclient库。
pip install mysqlclient
为了配置你的Django设置,让你的项目可以连接到MySQL数据库,你可以使用下面的设置。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'database_name',
'USER': 'username',
'PASSWORD': 'databasepassword@123',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
我在我的博客上写了一个专门的Python教程,介绍了如何使用Python连接MySQL数据库和创建表。想要了解更多,请点击这里。