How to create MySQL Database using matlab?

14 次查看(过去 30 天)
I am doing some data analytics using MySQL and Matlab. My program works fine if there is already an existing database present. But it doesn't work when there is not database for which I am trying to create connection. Now, what I want to do is to create a database with a fixed name if there is no database present in that name. I searched all over the internet and haven't found any option for that. May be I am missing something.
Additionally, I would like to create a table on the newly created database and store some random data on them. I can do this part. But I am stucked on the first part which is creating database programmatically using matlab.
Please note that, I have to use only matlab for this project. Any kind cooperation will be greatly appreciated.
  2 个评论
James Johnson
James Johnson 2018-2-2
编辑:James Johnson 2018-2-2
I don't think Matlab CAN make a SQL database? Please let me know if you have solved this. I am working on similar issues. One can create an sqlite database, but one cannot store matrices or strings in it (as near as I can tell). You have to make the database elsewhere for anything other than sqlite.
I asked a related question and in that you can find some code that might get you started if you had given up in April.

请先登录,再进行评论。

回答(1 个)

Piyush Kumar
Piyush Kumar 2024-10-29,6:07
To programmatically create a MySQL database using MATLAB, follow these steps:
  1. Establish a Database Connection: First, connect to the MySQL server without specifying a database.
  2. Create the Database: Use SQL commands to create the database if it doesn't already exist.
% Establish a database connection
conn = database('', 'username', 'password', 'Vendor', 'MySQL', 'Server', 'localhost', 'PortNumber', 3306);
dbName = 'test';
% Form the SQL query
sqlCreateDB = sprintf('CREATE DATABASE IF NOT EXISTS %s', dbName);
execute(conn, sqlCreateDB);
close(conn);
The above code will create a database named "test" if it does not exist already. If the database exists, it will not create the database.
After creating this database, you can establish a new connection to this newly created database and execute other commands like create tables, fetching rows from tables etc.

类别

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