Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-10-2011, 03:19 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 3
therabbitfactory is on a distinguished road
Default Shake to view random SQL entry

Hi all

So im a little bit of a noob when it comes to objective C and object oreintated programming and well I have landed quite a nice job but they would like me to build them a IPhone App. What they want is basically a view that contains the company logo with a label. When the user shakes the phone a random 'Tip' appears from a SQL database.

Ideally I would want the app to connect to the server hosting the database at start up to check if there is a update however I am all for the easy way, so I figured that I could import the DB into XCODE and when the DB needs updateing release a updated APP?

Anyways I am fine with the shake I can get the device to show a string in the label when the device is shaken how ever this is a static string, I want it to be random based on a field in a database.

Would you be able to point me in the right direction?

Thanks in advance for your help!
therabbitfactory is offline   Reply With Quote
Old 03-11-2011, 06:59 AM   #2 (permalink)
Registered Member
 
Join Date: Jun 2010
Location: Leeds, UK
Age: 26
Posts: 48
zeolite is on a distinguished road
Default

1. Get a list the ids of all rows in your table. I'm assuming you have one column for the row id and another holding the tip as a VARCHAR.

2. Pick a random number "r" between 0 and (the number of rows in your table - 1)

3. Get the row id of the row "r"

4. Select the row with this id

Code:
1. SELECT row_id FROM my_table // store this in "rows_returned"

2. SELECT COUNT(*) from my_table // store this as "the_count"

srand([[NSDate date] timeIntervalSince1970]); // seed random number generator
int r = rand() % (the_count - 1);

3. int row_id = rows_returned[r].row_id

4. SELECT my_tip FROM my_table WHERE row_id = [r]
__________________
If my post helped you then please make me happy by downloading my free iPhone game - Goats and Tigers

http://www.goatsandtigers.com
zeolite is offline   Reply With Quote
Old 03-11-2011, 11:00 AM   #3 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 324
mariano_donati is on a distinguished road
Default

You can make the database do the job:

SELECT tip FROM tips order by random() limit 1
mariano_donati is offline   Reply With Quote
Old 03-11-2011, 12:14 PM   #4 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 3
therabbitfactory is on a distinguished road
Default

Thanks for your comments guys...

I already know the logic behind it as I have programmed in other languages such as PHP, VB but well were would I enter the query and how would I return the result to the label?

Thanks for your help
therabbitfactory is offline   Reply With Quote
Old 03-11-2011, 02:59 PM   #5 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 324
mariano_donati is on a distinguished road
Default

I thought you were using sqlite. Aren't you?
mariano_donati is offline   Reply With Quote
Old 03-11-2011, 03:02 PM   #6 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 3
therabbitfactory is on a distinguished road
Default

Yes I am as I think that this is the best way to do this?

I have literally imported the sql file to XCODE and imported the framework to the project, any ideas on how to implement the classes so that one result is returned?

Again apologies for my lack of understanding, I am a complete noob but hey, we all have to start some were
therabbitfactory is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, random record, shake, sql

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 367
13 members and 354 guests
Absentia, akphyo, apatsufas, BinHex, fredidf, Gaz, gmarro, jeroenkeij, Kirkout, MarkC, mottdog, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:22 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0