This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

How to search a list of results from an sql query?

Help for those learning Tcl or writing their own scripts.
Post Reply
R
Reserve
Voice
Posts: 6
Joined: Mon Apr 18, 2005 5:25 am
Location: Nottingham, UK

How to search a list of results from an sql query?

Post by Reserve »

I’m trying to write a script that queries a database, sorts the results and then searches the results for a match.

The select query will be something like

Code: Select all

 mysqlsel $mysql "SELECT * FROM sql_table where last_event < $time"
 set mycount [mysqlresult $mysql rows?]
  mysqlmap $mysql {id name score kills} {
 }

I have googled and searched the forum and found a few matches but I can’t get what I'm trying to do to work and don’t seem to be getting any error messages to help work out why it’s not working.

Once I have the results from the query I need to build a list of id, name, score and kills and sort first on score (DESC) and then on kills (DESC). That bit of the problem I can do with the actual select statement. The bit I'm having problems with is I need to somehow generate an index number for each result starting from 1 and ending with the last result to the query as follows

Code: Select all

  Index    name     score   kills
  1        name1    100     20
  2        name4    95      15
  3        name2    82      10
  4        name5    82      8
  5        name3    60      5
From this list I then need to do a search for a particular name and select that whole row but the important part of the row will be the index number for that row. I’ve seen references to lsearch and think this may be the way to do it but in not sure how to actually implement it.

Can anyone offer me any help or guidance with this script?

TIA

Regards

Reserve
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

build your result set as list of lists:

Code: Select all

...
mysqlmap $db {id name score kills} {
...
lappend ::result [list $id $name $score $kills]
...
}
then you'll be able to sort by index in sublist:

Code: Select all

proc foo {...} {
...
# sort by name
set ::result [lsort -index 1 $::result]
...
}
refer to lsort manpage for more info and examples
connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use

Code: Select all

 tag when posting logs, code
Post Reply