sql-servermatlabuser-interfaceeditbox

How to insert values through a matlab gui into sql database automatically?


We have created a registration form in Matlab with some field such as name, age, etc. Also we have created a database using Sql Server(ODBC). Now we can read the TextBox values and display it in command window by get property (it is in pushbutton callback). We have to insert that textbox value in database which is already created We use fastinsert comMand. But its just add the values manualLy(using query) into it, but we want to add it through textbox. Our code in push button callback is here.

conn = database('Addface_DSN','sa','123 ');

if(isempty(conn.message))

     disp('database connected')

 else
     disp('cannot connected')

     disp(conn.message);

     return
end

setdbprefs('DataReturnFormat','numeric')

exdata = {'2','Shalu','22','female'};

fastinsert(conn, 'Faces_Details', {'Id' 'Name' 'Age' 'Gender' },exdata)

commit(conn)

close(conn);

name = get(handles.edit1, 'string'); % we dont want this, But just add to chek. we want this name in database.

disp(name)

age = get(handles.edit2, 'string'); 

disp(age)

Solution

  • Please try this solution Most of the solution is your code only

    conn = database('Addface_DSN','sa','123 ');
    
    if(isempty(conn.message))
    
         disp('database connected')
    
     else
         disp('cannot connected')
    
         disp(conn.message);
    
         return
    end
    
    setdbprefs('DataReturnFormat','numeric')
    
    // Here is the difference
    
    // Taking test_age as 'int' 
    test_name=get(handles.edit1, 'string'); 
    test_age= get(handles.edit2, 'string');
    //Convert to int and then insert
    test_age=str2num(test_age);
    exdata2={'3',test_name,test_age,'Male'};
    
    exdata = {'2','Shalu','22','female'};
    
    //Here you insert the data.
    fastinsert(conn, 'Faces_Details', {'Id' 'Name' 'Age' 'Gender' },exdata2)
    
    commit(conn)
    
    close(conn);
    
    name = get(handles.edit1, 'string'); % we dont want this, But just add to chek. we want   this name in database.
    
    disp(name)
    
    age = get(handles.edit2, 'string'); 
    
    disp(age)