sqlread error, in Matlab 2018

7 次查看(过去 30 天)
Alex Fernandez
Alex Fernandez 2018-5-12
Error using database.odbc.connection/sqlread (line 174) ODBC Driver Error: [MySQL][ODBC 8.0(a) Driver][mysqld-5.6.38]No database selected
What Can I do?

回答(1 个)

Mark Cafaro
Mark Cafaro 2018-5-14
This error will occur if DefaultCatalog is null:
>> conn = database('localmysql', 'root', 'pass');
>> conn.DefaultCatalog
ans =
'null'
>> sqlread(conn, 'mytable')
Error using database.odbc.connection/sqlread (line 174)
ODBC Driver Error: [MySQL][ODBC 5.3(a) Driver][mysqld-5.7.20-log]No database selected
You either need to set the default database for the data source in ODBC Data Source Administrator, as follows:
  1. Open ODBC Data Source Administrator as described at https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/open-the-odbc-data-source-administrator?view=sql-server-2017
  2. Double-click on the Data Source you are using in the Data Sources table.
  3. Use the dropdown next to the Database label to select a valid default database.
  4. Click OK to save the changes.
Or, you need to use the SQL USE statement to switch to a valid database before executing sqlread, as shown:
>> conn.Catalogs
ans =
1×3 cell array
{'information_schema'} {'mysql'} {'performance_schema'}
>> exec(conn, 'USE mysql');
>> sqlread(conn, 'user')
ans =
3×45 table
...

类别

Help CenterFile Exchange 中查找有关 Database Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by