![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/417863/image.png)
How to take input from Edit Text Field and save the data with Push Button
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Sourabh Kondapaka
2020-11-18
编辑:Sourabh Kondapaka
2020-11-18
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/417863/image.png)
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:
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Database Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!