如何使用python程序连接MySQL数据库?
当前回答
去图书馆的第一步: 打开终端,执行pip install mysql-python-connector。 安装完成后进入第二步。
导入库的第二步: 打开你的python文件并编写以下代码: 进口mysql.connector
第三步连接服务器: 编写以下代码:
Conn = mysql.connector。连接(主机=您的主机名,如localhost或127.0.0.1, 用户名=您的用户名,如root, 密码=您的密码)
第三步制作光标: 使游标便于我们运行查询。 要使游标使用以下代码: Cursor = conn.cursor()
执行查询: 对于执行查询,您可以执行以下操作: cursor.execute(查询)
如果查询更改了表中的任何内容,则需要在执行查询后添加以下代码: conn.commit ()
从查询中获取值: 如果你想从查询中获取值,那么你可以做以下事情: 游标。execute ('SELECT * FROM table_name') for i in cursor: print(i) #或for i in cursor.fetchall(): print(i)
fetchall()方法逐行返回一个包含您请求的值的元组列表。
关闭连接: 要关闭连接,您应该使用以下代码: conn.close ()
处理例外: 要处理异常,你可以用下面的方法: try: #逻辑通过,mysql.connector.errors.Error: #逻辑通过 使用数据库: 例如,您是一个帐户创建系统,您将数据存储在名为blabla的数据库中,您只需向connect()方法添加一个数据库参数,如
mysql.connector。连接(database =数据库名)
不要删除主机、用户名、密码等其他信息。
其他回答
尝试使用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 ()
对于python 3.3
CyMySQL https://github.com/nakagami/CyMySQL
我在windows 7上安装了pip,只是 安装cymysql
(你不需要cython) 快速无痛
这里有一种方法,使用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()
参考这里
首先,从https://dev.mysql.com/downloads/connector/python/安装python-mysql连接器
在Python控制台输入:
pip install mysql-connector-python-rf
import mysql.connector
再来看看Storm。它是一个简单的SQL映射工具,允许您轻松地编辑和创建SQL条目,而无需编写查询。
这里有一个简单的例子:
from storm.locals import *
# User will be the mapped object; you have to create the table before mapping it
class User(object):
__storm_table__ = "user" # table name
ID = Int(primary=True) #field ID
name= Unicode() # field name
database = create_database("mysql://root:password@localhost:3306/databaseName")
store = Store(database)
user = User()
user.name = u"Mark"
print str(user.ID) # None
store.add(user)
store.flush() # ID is AUTO_INCREMENT
print str(user.ID) # 1 (ID)
store.commit() # commit all changes to the database
查找和使用对象:
michael = store.find(User, User.name == u"Michael").one()
print str(user.ID) # 10
用主键查找:
print store.get(User, 1).name #Mark
有关更多信息,请参阅教程。
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 如何关闭mysql密码验证?
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- MySQL区分大小写查询
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- MySQL数据库表中的最大记录数