VeighNa量化社区
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 2
声望: 0

我需要将vnpy连接MySQL数据库,在我创建了MySQL数据库stock之后,并且已经在投研Jupyter Notebook中成功地连接到了这个数据库。然而,当我试图在vnpy中使用 get_database() 函数来获取数据库连接时,我收到了一个错误:OperationalError: (1049, "Unknown database 'database.db'")。

一开始,我print(SETTINGS["database.database"]),发现是'database.db',我以为是这个问题。
等我SETTINGS["database.database"] = "stock"之后,能够正确print。
但是get_database()还是这个错误。

然后我尝试重置 database 全局变量为 None,然后再次调用 get_database(),但是我还是收到了相同的错误OperationalError: (1049, "Unknown database 'database.db'")。

Member
avatar
加入于:
帖子: 2
声望: 0

完整报错信息

OperationalError Traceback (most recent call last)
File D:\veighna_studio\lib\site-packages\peewee.py:3173, in Database.connect(self, reuse_if_open)
3172 with exception_wrapper:
-> 3173 self._state.set_connection(self._connect())
3174 if self.server_version is None:

File D:\veighna_studio\lib\site-packages\peewee.py:4131, in MySQLDatabase._connect(self)
4130 raise ImproperlyConfigured('MySQL driver not installed!')
-> 4131 conn = mysql.connect(db=self.database, **self.connect_params)
4132 return conn

File D:\veighna_studio\lib\site-packages\pymysql\connections.py:353, in Connection.init(self, user, password, host, database, unix_socket, port, charset, sql_mode, read_default_file, conv, use_unicode, client_flag, cursorclass, init_command, connect_timeout, read_default_group, autocommit, local_infile, max_allowed_packet, defer_connect, auth_plugin_map, read_timeout, write_timeout, bind_address, binary_prefix, program_name, server_public_key, ssl, ssl_ca, ssl_cert, ssl_disabled, ssl_key, ssl_verify_cert, ssl_verify_identity, compress, named_pipe, passwd, db)
352 else:
--> 353 self.connect()

File D:\veighna_studio\lib\site-packages\pymysql\connections.py:633, in Connection.connect(self, sock)
632 self._get_server_information()
--> 633 self._request_authentication()
635 if self.sql_mode is not None:

File D:\veighna_studio\lib\site-packages\pymysql\connections.py:932, in Connection._request_authentication(self)
931 if self._auth_plugin_name == "caching_sha2_password":
--> 932 auth_packet = _auth.caching_sha2_password_auth(self, auth_packet)
933 elif self._auth_plugin_name == "sha256_password":

File D:\veighna_studio\lib\site-packages\pymysql_auth.py:239, in caching_sha2_password_auth(conn, pkt)
238 print("caching sha2: succeeded by fast path.")
--> 239 pkt = conn._read_packet()
240 pkt.check_error() # pkt must be OK packet

File D:\veighna_studio\lib\site-packages\pymysql\connections.py:725, in Connection._read_packet(self, packet_type)
724 self._result.unbuffered_active = False
--> 725 packet.raise_for_error()
726 return packet

File D:\veighna_studio\lib\site-packages\pymysql\protocol.py:221, in MysqlPacket.raise_for_error(self)
220 print("errno =", errno)
--> 221 err.raise_mysql_exception(self._data)

File D:\veighna_studio\lib\site-packages\pymysql\err.py:143, in raise_mysql_exception(data)
142 errorclass = InternalError if errno < 1000 else OperationalError
--> 143 raise errorclass(errno, errval)

OperationalError: (1049, "Unknown database 'database.db'")

During handling of the above exception, another exception occurred:

OperationalError Traceback (most recent call last)
Cell In[38], line 30
25 from vnpy.trader.database import get_database
26 #from vnpy.trader.object import BarData, TickData
27
28
29 # 获取数据库实例
---> 30 database = get_database()

File D:\veighna_studio\lib\site-packages\vnpy\trader\database.py:158, in get_database()
155 module: ModuleType = import_module("vnpy_sqlite")
157 # Create database object from module
--> 158 database = module.Database()
159 return database

File D:\veighna_studio\lib\site-packages\vnpy_mysql\mysql_database.py:162, in MysqlDatabase.init(self)
160 """"""
161 self.db: PeeweeMySQLDatabase = db
--> 162 self.db.connect()
163 self.db.create_tables([DbBarData, DbTickData, DbBarOverview, DbTickOverview])

File D:\veighna_studio\lib\site-packages\peewee.py:3172, in Database.connect(self, reuse_if_open)
3169 raise OperationalError('Connection already opened.')
3171 self._state.reset()
-> 3172 with exception_wrapper:
3173 self._state.set_connection(self._connect())
3174 if self.server_version is None:

File D:\veighna_studio\lib\site-packages\peewee.py:3010, in ExceptionWrapper.exit(self, exc_type, exc_value, traceback)
3008 new_type = self.exceptions[exc_type.name]
3009 exc_args = exc_value.args
-> 3010 reraise(new_type, new_type(exc_value, *exc_args), traceback)

File D:\veighna_studio\lib\site-packages\peewee.py:192, in reraise(tp, value, tb)
190 def reraise(tp, value, tb=None):
191 if value.traceback is not tb:
--> 192 raise value.with_traceback(tb)
193 raise value

File D:\veighna_studio\lib\site-packages\peewee.py:3173, in Database.connect(self, reuse_if_open)
3171 self._state.reset()
3172 with exception_wrapper:
-> 3173 self._state.set_connection(self._connect())
3174 if self.server_version is None:
3175 self._set_server_version(self._state.conn)

File D:\veighna_studio\lib\site-packages\peewee.py:4131, in MySQLDatabase._connect(self)
4129 if mysql is None:
4130 raise ImproperlyConfigured('MySQL driver not installed!')
-> 4131 conn = mysql.connect(db=self.database, **self.connect_params)
4132 return conn

File D:\veighna_studio\lib\site-packages\pymysql\connections.py:353, in Connection.init(self, user, password, host, database, unix_socket, port, charset, sql_mode, read_default_file, conv, use_unicode, client_flag, cursorclass, init_command, connect_timeout, read_default_group, autocommit, local_infile, max_allowed_packet, defer_connect, auth_plugin_map, read_timeout, write_timeout, bind_address, binary_prefix, program_name, server_public_key, ssl, ssl_ca, ssl_cert, ssl_disabled, ssl_key, ssl_verify_cert, ssl_verify_identity, compress, named_pipe, passwd, db)
351 self._sock = None
352 else:
--> 353 self.connect()

File D:\veighna_studio\lib\site-packages\pymysql\connections.py:633, in Connection.connect(self, sock)
630 self._next_seq_id = 0
632 self._get_server_information()
--> 633 self._request_authentication()
635 if self.sql_mode is not None:
636 c = self.cursor()

File D:\veighna_studio\lib\site-packages\pymysql\connections.py:932, in Connection._request_authentication(self)
930 # https://dev.mysql.com/doc/internals/en/successful-authentication.html
931 if self._auth_plugin_name == "caching_sha2_password":
--> 932 auth_packet = _auth.caching_sha2_password_auth(self, auth_packet)
933 elif self._auth_plugin_name == "sha256_password":
934 auth_packet = _auth.sha256_password_auth(self, auth_packet)

File D:\veighna_studio\lib\site-packages\pymysql_auth.py:239, in caching_sha2_password_auth(conn, pkt)
237 if DEBUG:
238 print("caching sha2: succeeded by fast path.")
--> 239 pkt = conn._read_packet()
240 pkt.check_error() # pkt must be OK packet
241 return pkt

File D:\veighna_studio\lib\site-packages\pymysql\connections.py:725, in Connection._read_packet(self, packet_type)
723 if self._result is not None and self._result.unbuffered_active is True:
724 self._result.unbuffered_active = False
--> 725 packet.raise_for_error()
726 return packet

File D:\veighna_studio\lib\site-packages\pymysql\protocol.py:221, in MysqlPacket.raise_for_error(self)
219 if DEBUG:
220 print("errno =", errno)
--> 221 err.raise_mysql_exception(self._data)

File D:\veighna_studio\lib\site-packages\pymysql\err.py:143, in raise_mysql_exception(data)
141 if errorclass is None:
142 errorclass = InternalError if errno < 1000 else OperationalError
--> 143 raise errorclass(errno, errval)

OperationalError: (1049, "Unknown database 'database.db'")

哪位大神能解答一下,谢谢

Member
avatar
加入于:
帖子: 4870
声望: 292

启动的目录不同,读到了不同.vntrader下的setting吧

© 2015-2022 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】