0

I am using sqlite3 library on a c project. And I have a database like this:

|  Time  |  Message  |  ID  |  Temp  |
|--------|-----------|------|--------|
|09:05:37|    1514   |  62  |   35   |
|10:14:45|    1515   |  43  |   14   |
|11:18:50|    1997   |  28  |   43   |
|08:04:23|    1998   |  28  |   50   |           

Message is an autoincrement value. What I am trying to do and my questions are:

int ID_in;
printf("Enter the ID you want to check: ");
scanf("%d", &ID_in);

Firstly user will input a value and I have to find does that value appear on ID column and if it appears I can start my operations. Since user will input a value should I use a sqlite3_bind() function on my database to compare the ID_in with the values on database?

If the ID_in appears on the ID column I have to find the highest value on the Message column which is on the same row with ID_in value since there may be more on than one ID_in value on the ID column. For example if the ID_in is 28 I have to access the row which Message value is highest, for this one I have to access the row where Message value is 1998.

After I access that row I should print all the columns on that row. For example if ID_in is 28 find the value 28 on ID column. Then find the highest Message column value which is 1998. Then print Time, Message, ID, Temp on that row which are 08:04:23, 1998, 28, 50 for this example.

kollarotta
  • 9
  • 2
  • 4
  • possible duplicate of [Retrieving the last record in each group](http://stackoverflow.com/questions/1313120/retrieving-the-last-record-in-each-group) – Tomalak Jan 21 '15 at 08:47

1 Answers1

0

Yes, you can use prepared statement and sqlite_bind for passing the user input to query. For selecting the row with highest messsage value you can use,

Select * from [table_name] where id = [user_input] order by message desc limit 1;

Prerak Sola
  • 9,517
  • 7
  • 36
  • 67