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())
Related
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.
Can any one please help me how to Insert the data into database from window form. How to fetch the data to show on window form & same to update the data from database. I am looking for the code that contain sql query with in the code not from the quick select data window. I am very new in powerbuilder.I want to write a code fetch update data from the code any where & show anywhere.
Thanks
I'm not quite sure about your question. Try going to this website http://powerbuilder.hyderabad-colleges.com.
Look for Datawindow control and Datawndow object topics.
There are other ways to manipulate data in Powerbuilder like using Embeded SQL (stored procedure and cursors).
I hope this will help you.
The whole point of the Datawindow is that it does all that work for you.
Retrieve data:
dw_1.Retrieve(arguments)
Update the database:
dw_1.Update()
I'm not understanding the question entirely you must be having trouble with a multi-table update they can be challenging for a new developer.
This will do an update into two tables I did it in a hurry so might be a syntax error or two.
// insert a row
li_row = dw_1.insertrow(0)
dw_1.setitem(li_row, 'col1', 'try reading')
dw_1.setitem(li_row, 'col2', 'the PowerBuilder')
dw_1.setitem(li_row, 'col3', 'manual next time')
// do accept text left out for purposes of brevity
// Update first table and dont bother with another accepttext
// since weve already done one and dont set the updateflags
// so second half of update creates correct sql statement
li_rtn = dw_1.Update(false, false)
if li_rtn = 1 then
dw_1.modify('tbl1_col1.Update = No')
dw_1.modify('tbl1_col2.Update = No')
dw_1.modify('tbl1_col3.Update = No')
dw_1.modify('tbl1_id.Key = No')
dw_1.modify("Datawindow.Table.updateable = 'tbl2'")
dw_1.modify('tbl2_col1.Update = Yes')
dw_1.modify('tbl2_col2_id.Key = Yes')
li_rtn = dw_1.update(false, true)
if li_rtn = 1 then
commit using sqlca;
else
rollback using sqlca;
end if
end if
// cleanup the temp recs
li_rowcount = dw_1.rowcount()
for li_row = li_rowcount to 1 step -1
dw_1.deleterow(li_row)
next
dw_1.Update()
I'm new to TideSDK so I am doing some tests. I found that the API has some methods to manage and retrieve data stored in a local DB. I created a table named Users with only two fields, id and name (This is the example at http://tidesdk.multipart.net/docs/user-dev/generated/#!/api/Ti.Database.DB), which I have fill with some random numbers and names. This is the function I am using:
function consulta (){
//Open the database first
var db = Ti.Database.openFile(Ti.Filesystem.getFile(
Ti.Filesystem.getApplicationDataDirectory(), 'customdatabase.db'));
var rows = db.execute("SELECT * FROM Users ORDER BY firstName");
while (rows.isValidRow()) {
document.getElementById('resultado').innerHTML = 'The user id is '+rows.fieldByName('id')+', and user name is '+rows.fieldByName('firstName')+'<br>';
rows.next();
}
document.getElementById('filas').innerHTML = rows.rowCount( );
//Release memory once you are done with the resultset and the database
rows.close();
db.close();
}**
My problem is this: Though the result of method rowCounts() is 29 (Meaning, of course, that there are 29 rows in the result), the WHILE block is just out puting one single row instead. Can someone help me to make this work? Shouldn't I use the API for this?
Checkout Sample API Usage for Database module here.
Try this:
while (rows.isValidRow()) {
document.write('The user id is '+rows.fieldByName('id')+', and user name is '+rows.fieldByName('firstName')+'<br >'); rows.next(); } document.getElementById('filas').in nerHTML = rows.rowCount( ); //Release memory once you are done with the resultset and the database rows.close(); db.close(); }
Place the script inside the body
I'm trying to create an app using Titanium Studio that will display information from an SQLite database.
To make things simpler, lets say my database have the following columns:
Last Name, Given Name, Age, Race & Religion
I currently have a basic framework for the app, which consists of multiple tabs.
In one of the tabs (which corresponds to a particular window), I would like to have a TableView that displays only Last Name, Given Name & Age in each row.
How do I do that?
Appreciate all the help I can get!
Thanks!
Have you looked at this?
So once you get the rows from the database, just create rows for your tableview. Here is a start:
// Fetch the db and execute a select from your person table
var db = Ti.Database.open('mydb');
var rows = db.execute('SELECT * FROM person');
var persons = [];
while (rows.isValidRow())
{
persons.push({title : + rows.fieldByName('Last Name'));
rows.next();
};
rows.close();
var yourTable = Ti.UI.createTableView({
width : Ti.UI.FILL,
height : Ti.UI.FILL,
data: persons // Set the rows to the table
});
// Dont forget to close the db
db.close();
Use this as a reference to style your table rows more effectively.
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