Objective
In the previous article, we talked about linking databases from the database Toolbox. In Matlab, you can also use the Visual Tools database Toolbox to perform some SQL operations. However, for database programming, you do not want to manually open the visualizer when executing the script, so you need to link and execute the SQL command on the command line.
Content Summary
This article mainly describes
- Connect to the database on the command line
- Execute SQL statement
- Storing results to an object
- Output data
Specific operation
The main use of this article is the MySQL database, the connection using the JDBC driver (driver introduction, download and installation see MATLAB link MySQL method). If you need to use an ODBC connection, the approximate process is basically the same, the details of the official documents and other information are not discussed here.
Database links, you first need to write the statements used by the link. First, the role of the link statement and write an example:
conn = Database (Instance,username,password,driver,databaseurl)
The above statement describes the writing requirements of the LINK statement: Conn is the return of the linked object, database is the function used in MATLAB to link the databases, the instance parameter is the data name that you need to link MySQL, username is the link mysql user name, Password is the user name username corresponding password, dirver is the database (used in this article is MySQL) and matlab driver, databaseurl description see matlab link MySQL method. For such criteria, this paper gives an example of the actual use of the author, for reference: conn=database (' Test ', ' root ', ', ' Mysql_connector_java_5.1.35_bin.jar ', ' jdbc:mysql:// Localhost:3306/test ')
Then use the EXEC function to execute the above linked statement: EXEC (conn, ' SQLQuery ')
After the linked database succeeds, prepare the relevant SQL statement and execute the
UID=EXEC (conn, ' Select UID form user ');
Uid=fetch (UID);
NOTE: Here the SQL statement execution is divided into: Execute SQL file and execute SQL statement directly two kinds. As for the specific execution of the SQL file operation details here are not elaborated.
- Storing results to an object
After executing the SQL statement described above, the query results need to be processed:
Uiddata=uid.data;
Enter Uiddata to get the desired result information.
The above represents only personal views, if there are errors please correct me.
MATLAB data links and queries