I have SQLite Studio fot my Mac OSX, I create there my Schemas, in MySQL I use DBFork Manager a case tool to make my Schemas, I export sql file and then import into MySQL Server, so I want to do the same for my iOS App using SQLite.
I mean, I make my SQLite Schema, then I need to import into my App from a *.sql file, I give my code example using a SQL string… Hope you can help me!
- (void)viewDidLoad {
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
_databasePath = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
@"contacts.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
_status.text = @"Failed to create table";
}
sqlite3_close(_contactDB);
} else {
_status.text = @"Failed to open/create database";
}
}
[super viewDidLoad];
}
I assume you have a database file in SQLite and want to install it for use on your iPhone. If you compile it in your program, then it will be in the file bundle. However, that’s read only, so if you want to alter your database at all, you need to copy it to the iphone’s documents directory (or another more appropriate directory). Check out the answer to this stackoverflow question:
SQLite on iPhone