I am relatively new to GUI's in Matlab, and I have created a simple GUI using GUIDE. I want to connect to a database (already defined and working!) and populate a listbox with the values from the database so the user can choose which to use (in this case they are chemical compounds). I haven't been able to find a good tutorial or clues on how to populate the listbox in this way. So far, I have:
function load_listbox(hObject,handles)
conn = database('antoine_db','','');
setdbprefs('datareturnformat','structure'); %sets the db preferences to a structure
query = 'SELECT ID,"Compound Name" FROM antoine_data ORDER BY ID';
result = fetch(conn,query);
%%The following creates a structure containing the names and ID's
%%of everything in the database
data = struct([]);
for i=1:length(result.ID)
data(i).id = result.ID(i);
data(i).name = char(result.CompoundName(i));
end
names = data.name;
handles.compounds = names;
whos;
set(handles.listbox1,'String',handles.compounds,'Value',1);
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
end
What would be the simplest way to populate a listbox from a database (or large array) like this? As of right now, the listbox is populated with only the first item in names, which is because somehow names contains only the first item. Although, if I just display 'data.name', I get the entire list of 300 items in the list!
I got it! So, the problem was that I was converting the data.name to a character -> originally it was a cell. Thus, I added names(i) = data(i).name; in the for loop, and removed names=data.name; It is now populated with all of the names of the compound! The working function looks like this:
function load_listbox(hObject,handles)
conn = database('antoine_db','','');
setdbprefs('datareturnformat','structure'); %sets the db preferences to a structure
query = 'SELECT ID,"Compound Name" FROM antoine_data ORDER BY ID';
result = fetch(conn,query);
%%The following creates a structure containing the names and ID's
%%of everything in the database
data = struct([]);
for i=1:length(result.ID)
data(i).id = result.ID(i);
data(i).name = (result.CompoundName(i)); %this is a cell
names(i) = data(i).name;
end
handles.compounds = names;
set(handles.listbox1,'String',handles.compounds,'Value',1);
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
end
Related
i have this code in mysql
def get_table():
cur = self.cnn.cursor()
cur.execute("SELECT * FROM table")
data = cur.fetchall()
cur.close()
return data
that returns the entire table, as a list. And then I can use it in another module to fill a TreeView (tkinter):
for row in data:
TreeView.insert('',END,text=row[0], values=(row[1],row[2],row[3],etc
and this works well. But now I want to do it using Peewee, and I don't know how to return to the module that uses the TreeView the whole table. I thought about doing the below, but that doesn't work.
def get_table():
for row in Students.select():
data = data + row.get()
return data
I guess I should try to make a matrix from the data in each row, but I don't know how to do it.
Thanks in advance!
def get_table():
return list(Students.select())
Using VS 2013 (VB) and SQL server 2016.
I have linQ query that returns two columns from a database. The query is as follows.
Dim val = (From value In db.ngc_flowTypes
Where value.defaultValue IsNot Nothing
Select value.flowName, value.defaultValue)
The data it returns is a as follows.
I want to iterate through each row of the results and pass the values to certain variables. A ForEach statement doesnt seem to work as it just runs through once. I am sure this must be easy but I ont quite understand it. Am I getting the data returned in the best way via my query? Can I transpose the data to a data table in VB? so I can work with it easier?
The end result I want is string for each flow name with its corresponding default value (along with some other text). So something like this.
dim strsubmission as string = flowName + " has a value of " + defaultValue
Use ToDictionary.
Dim val = (From value In db.ngc_flowTypes
Where value.defaultValue IsNot Nothing
Select value).ToDictionary(Function(key) key.flowName,
Function(value) value.defaultValue)
This will actually execute the SQL of the linq on the database (approx. Select * From ngc_flowTypes Where defaultValue Is Not NULL), traverse each record into a key/value pair (flowName, defaultValue) and put it into a in-memory dictionary variable (val).
After that you can do whatever you like with the dictionary.
For Each flowName In val.Keys
Console.WriteLine("{0} has a value of {1}", flowName, val(flowName))
Next
Edit:
This will only work as long flowName is unique in table ngc_flowTypes
Im having some trouble with the design of my database. In microsoft Access I have two tables. One named table1 this table contains three fields (Name, Surname and Birthdate). The other table, named table2 contains two fields (Name and Surname). I want the following to happen. If I make a new record in table1 using a form the Name and Surname get passed/inserted to table2 automaticly. What is the best way of doing this? I was messing around with the primary key but that doenst seem to work. And since I'm a beginner I dont know where to go from here.
Thanks in advance for your time and efford!
Just figured out the awnser to my question! I used the following code
Private Sub addNew_Click()
Dim db As Object
Dim rst As Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("select * from Person", dbOpenDynaset)
rst.AddNew
rst!PeopleSoftNr = tbPeopleSoftNr.Value
rst!Name = tbName.Value
rst!Birthday = tbBirthday.Value
rst.Update
Set rst2 = db.OpenRecordset("select * from Dental", dbOpenDynaset)
rst2.AddNew
rst2!PeopleSoftNr = tbPeopleSoftNr.Value
rst2!Dental = tbDental.Value
rst2.Update
End Sub
This allows me to write data to multiple tables.
In my opinion the best way to do, what you described, is creating a form for the personal data and include a subform to assign courses of the persons.
Detail information about the courses should be edited in an own form for the courses.
Which one could be a better practice? In my situation, I need to check if a specific data exists in a table. I am iterating through an Excel file and verifying if a code there exists in my table using VB.NET. I have two options to do this (or if there is a better way to do this, I am open for suggestions).
First is to check it one by one, this code is executed per loop:
SQL = "SELECT TOP 1 * FROM Table1 WHERE Code = '" & codeFromExcel & "'"
rs = dbConn.Execute(SQL)
If Not rs.EOF Then
isFound = True
Else
isFound = False
End If
The other one is I load all the codes in a List(Of T)
Dim myList As New List(Of String)()
rs = Nothing
rs = dbConn.Execute("Select Code from Table1")
If Not rs.EOF Then
Do While Not rs.EOF
myList.Add(rs.Fields("Code").Value.ToString)
rs.MoveNext()
Loop
End If
Then check every record if it is in the List(Of T) while iterating in the Excel.
If myList.Contains(codeFromExcel) Then
isFound = True
Else
isFound = False
End If
I've been working with this kind of stuff most of the time and I want to know which one is the most efficient way to use. At the moment I only have a few records in my database. I want my code to be ready and efficient when the time comes that I need to deal with numerous records. Thanks in advance!
Additional info: The data doesn't need to be "fresh" as that table is meant for one-time entry only.
Personally I prefer to open as less connections to data base as possible.
So:
If the table is not very large (some hundred rows) I would go with the "cache" option.
Generally:
I would gather all excel codes in a list. ( excelCodes )
Then I would query something like Select Distinct Code from Table1 Where Code In ( excelCodesList ) and store it in a second list ( foundCodes ).
Then I would compare these lists.
I test it on a table with 6.143.993 rows.
To select just one column (description) to "cache" took 1'29".
On the other hand query like:
select distinct description from ItemDetail where description in ( 'abc','cddd','xxx' )
took 0'58".
UPDATE
An index on Code column might help with performance.
I am trying to get the data from SQL Server database.
I have a table called Standard in my database. It has three columns StandardID, StandardName, and Description.
I have a combobox in which I fill the values of StandardName
Here is the code :
Using db As New SchoolDBEntities
ComboSelectStandardToEdit.DataSource = db.Standards.ToList()
ComboSelectStandardToEdit.ValueMember = "StandardID"
ComboSelectStandardToEdit.DisplayMember = "StandardName"
End Using
Now I have 2 textboxes called txtStandardName and txtDescription.
I want to fill the values of these 2 textboxes based on selected StandardName from the combobox.
Here is the code I tried :
Using db As New SchoolDBEntities
Dim standard = From s In db.Standards
Where s.StandardId = CInt(ComboSelectStandardToEdit.SelectedValue)
Select s
txtStandardName.Text = CType(standard, Standard).StandardName
End Using
but unfortunately I got error :
Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[EF_WinForms_VB.Standard]' to type 'EF_WinForms_VB.Standard'.
try using
Dim standard = (From s In db.Standards
Where s.StandardId = CInt(ComboSelectStandardToEdit.SelectedValue)
Select s)
.FirstOrDefault
txtStandardName.Text = standard.StandardName
your Linq query currently is returing a projection which could contain multiple entries. By explicitly requesting the first object of the projection, you won't need to cast your standard before accessing it's value.
Try using
Dim standard = (From s In db.Standards.AsEnumerable
Where s.StandardId = Convert.ToInt32(ComboSelectStandardToEdit.SelectedValue)
Select s)
.FirstOrDefault
txtStandardName.Text = standard.StandardName
Hope it helps.