【发布时间】:2015-04-10 11:20:13
【问题描述】:
我使用 Anaconda 发行版中的 Python 3.4。在这个发行版中,我发现pymysql 库可以连接到位于另一台计算机上的现有 MySQL 数据库。
import pymysql
config = {
'user': 'my_user',
'passwd': 'my_passwd',
'host': 'my_host',
'port': my_port
}
try:
cnx = pymysql.connect(**config)
except pymysql.err.OperationalError :
sys.exit("Invalid Input: Wrong username/database or password")
我现在想为我的应用程序编写测试代码,我想在每个测试用例的setUp 处创建一个非常小的数据库,最好在内存中。但是,当我突然尝试使用 pymysql 时,它无法建立连接。
def setUp(self):
config = {
'user': 'test_user',
'passwd': 'test_passwd',
'host': 'localhost'
}
cnx = pymysql.connect(**config)
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 61] Connection refused)")
我一直在谷歌搜索,发现了一些关于SQLite 和MySQLdb 的信息。我有以下问题:
-
sqlite3或MySQLdb是否适合在内存中快速创建数据库? - 如何在 Anaconda 包中安装
MySQLdb? - 是否有在
setUp中创建的测试数据库示例?这是个好主意吗?
我的计算机上没有本地运行的 MySQL 服务器。
【问题讨论】:
-
答案很可能是获取一个在本地运行的mysql db。 SQLite 与 mysql 有很多不同,所以用 SQLite 代替 mysql 并不是一个好的测试。
标签: python mysql sqlite mysql-python pymysql