我使用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服务器。