How to take input from Edit Text Field and save the data with Push Button
조회 수: 3 (최근 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일
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!