1

As my Question says that how can i get maximum value from Table?

In my apps. I have table name dataset_master

And table has field name is dataset_id, it is add manually as auto_inc.

So, First time when no record is inserted in Table and when I insert first record then I add dataset_id is 1. (this is only first time)

And Then after insert next record for dataset_id i fire query for get max value of dataset_id and I insert dataset_id +1. (This is for next record and so on..)

In my case I use following Query for get maximum dataset_id.

SELECT MAX(dataset_id) FROM dataset_master where project_id = 1

Here in my application I want to get maximum value of field name is dataset_id from dataset_master table.

This Query properly work when I insert record to dataset_master table each time I get proper maximum number of dataset_id. But when I delete record in sequins such like (1 to 5 from 10) in table and after I insert new record then I got each time last maximum number such like

if my table has 10 record then my dataset_id is 1 to 10; When I delete record such like 1 to 5 then remains 6 to 10 record and also dataset_id in Table. And then after I insert new record then each time I got 10 (maximum Number) so each time new record has dataset_id is dataset_id + 1 so 11.

What is problem I don't know (may be mistake in Query ?), please give your suggestion.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216

2 Answers2

1

You need to reset the sequence in the sqlite_sequence table. I'd advise you not to worry about this though, as by the time it becomes a problem, this will be the least of your headaches.

Community
  • 1
  • 1
hd1
  • 33,938
  • 5
  • 80
  • 91
  • @hd1.. i i found that i can not insert dataset_id more then 10 .. if i insert record then each time i got max dataset_id is 10.. how it is possible ? –  Jul 23 '13 at 09:38
  • This is why you shouldn't mess with the sqlite_sequence table – hd1 Jul 23 '13 at 09:39
  • @hd1.. how can i fire Query of sqlite_sequence table. please give me example because i am new at iOS Development so.. please –  Jul 23 '13 at 09:41
  • @hd1.. my dataset_id is not auto_incr field ...so also is this query (your) work for me ??? :( –  Jul 23 '13 at 10:45
  • If you are using auto_incr field, which is stated in your question, this will work for you, if you aren't then all bets are off and, as I implied in my answer, you have a bigger headache. – hd1 Jul 23 '13 at 12:48
  • @hd1-- ya i have headache bt how can you know it ? –  Jul 23 '13 at 13:15
  • I'm just good like that, if your field is not auto_inc, why do you not make it so? – hd1 Jul 23 '13 at 14:28
0

I think the problem is not in your query, but in your insert. Do you force dataset_id when inserting new rows?

LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46