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

2 ビュー (過去 30 日間)
Keynan Hart
Keynan Hart 2020 年 11 月 11 日
編集済み: Sourabh Kondapaka 2020 年 11 月 18 日
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

採用された回答

Sourabh Kondapaka
Sourabh Kondapaka 2020 年 11 月 18 日
編集済み: Sourabh Kondapaka 2020 年 11 月 18 日
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:

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDatabase Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by