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 05-16-2011, 04:03 AM   #1 (permalink)
Registered Member
 
franium's Avatar
 
Join Date: Nov 2010
Posts: 68
franium is on a distinguished road
Default Create a sqlite3 table programmatically

Hi all,
I have to add programmatically two tables to a sqlite3 database.
Have you some useful links or examples of how I could do it?
Thanks in advance,
Fran
franium is offline   Reply With Quote
Old 05-16-2011, 04:41 AM   #2 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

I suppose that you are able to do a query, and you miss only how create a table with SQL.

Quote:
CREATE TABLE "table_name"
("column 1" "data_type_for_column_1",
"column 2" "data_type_for_column_2",
... )
SQL CREATE TABLE Statement
__________________
dany_dev is offline   Reply With Quote
Old 05-16-2011, 07:02 AM   #3 (permalink)
iOSDeveloperz
 
iOSDeveloperz's Avatar
 
Join Date: Apr 2011
Location: INDIA
Posts: 99
iOSDeveloperz is on a distinguished road
Send a message via Skype™ to iOSDeveloperz
Default

Quote:
Originally Posted by franium View Post
Hi all,
I have to add programmatically two tables to a sqlite3 database.
Have you some useful links or examples of how I could do it?
Thanks in advance,
Fran
Code:
-(void) createTableNamed:(NSString *) tableName withField1:(NSString *) field1 withField2:(NSString *) field2 {
char *err; 
NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' TEXT PRIMARY KEY, '%@' TEXT);", tableName, field1, field2];
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) { 
sqlite3_close(db);
 NSAssert(0, @"Table failed to create.");
}
}
Here db is an object of type sqlite3
__________________
iOS Developerz
You Think, We Create...

http://iosdeveloperz.com/

View our latest apps
iOSDeveloperz is offline   Reply With Quote
Old 05-16-2011, 10:29 AM   #4 (permalink)
Registered Member
 
franium's Avatar
 
Join Date: Nov 2010
Posts: 68
franium is on a distinguished road
Default

Quote:
Originally Posted by iOSDeveloperz View Post
Code:
-(void) createTableNamed:(NSString *) tableName withField1:(NSString *) field1 withField2:(NSString *) field2 {
char *err; 
NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' TEXT PRIMARY KEY, '%@' TEXT);", tableName, field1, field2];
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) { 
sqlite3_close(db);
 NSAssert(0, @"Table failed to create.");
}
}
Here db is an object of type sqlite3
Thanks.
Do you the difference in using sqlite3_exec instead of sqlite3_prepare_v2? I've no the ideas very clear on this point.
franium is offline   Reply With Quote
Old 05-16-2011, 10:32 AM   #5 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

One-Step Query Execution Interface
Quote:
The sqlite3_exec() interface is a convenience wrapper around sqlite3_prepare_v2(), sqlite3_step(), and sqlite3_finalize(), that allows an application to run multiple statements of SQL without having to use a lot of C code.
__________________
dany_dev is offline   Reply With Quote
Old 05-16-2011, 10:42 AM   #6 (permalink)
Registered Member
 
franium's Avatar
 
Join Date: Nov 2010
Posts: 68
franium is on a distinguished road
Default

Quote:
Originally Posted by dany_dev View Post
Thank you.
I'll do some tries.
franium is offline   Reply With Quote
Reply

Bookmarks

Tags
create, iphone, objective c, sqlite database, table

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
6 members and 361 guests
doffing81, dre, iOS.Lover, Kirkout, MikaelBartlett, PlutoPrime
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

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