How to take input from Edit Text Field and save the data with Push Button

2 visualizaciones (últimos 30 días)
I'm trying to take input from a user such as first and last name and save the data into my microsoft access database when i click the push button, im fairley new to the 2020 version of matlab so any kind of tips would be helpful

Respuesta aceptada

Sourabh Kondapaka
Sourabh Kondapaka el 18 de Nov. de 2020
Editada: Sourabh Kondapaka el 18 de Nov. de 2020
For accessing database's within Matlab you will need Database Toolbox .
In the startUp function of the app, we create a connection to the database and store it in a variable. When the First Name and Last Name values are entered and Save to dB button is clicked. I have defined a SavetodBButtonPushed callback event which will utilize the connection established in the startUp function to write into the database
Some of the useful lines code are:
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
LastNameEditField matlab.ui.control.EditField
LastNameEditFieldLabel matlab.ui.control.Label
FirstNameEditField matlab.ui.control.EditField
FirstNameEditFieldLabel matlab.ui.control.Label
SavetodBButton matlab.ui.control.Button
end
properties (Access = private)
username; % Username for database
password; % Password for database
datasource; % For Database
data; %data that would be written using sqlwrite
tableName; %Name of the table in the database
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
%Code for creating a connection to the database
app.datasource = "MSSQLServerJDBCAuth"; % For Microsoft SQL Server Database
% You will need to change it to your username and password of your dB.
app.username = "";
app.password = "";
app.conn = database(app.datasource,app.username,app.password);
end
% Button pushed function: SavetodBButton
function SavetodBButtonPushed(app, event)
app.data = table(app.FirstNameEditField, app.LastNameEditField, 'VariableNames', {'First Name', 'Last Name'});
app.tableName = 'UserInfo';
sqlwrite(app.conn, app.tablename, app.data);
end
end
For more information please visit:

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by