如何以编程方式创建sqlite数据库?

Dur*_*sad 9 sqlite iphone

我是iPhone开发的新手.我使用mac os x中的终端创建了数据库.现在我正在尝试使用在iPhone应用程序中以编程方式创建数据库objective- C.有人可以告诉我一个很好的网站,我可以查看有效的示例应用程序,是否有任何学习该教程的网站?请有人帮帮我

EXC*_*ESS 14

来自Techtopia的教程:

NSString *docsDir;
NSArray *dirPaths;

// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex: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";
    }
}
[filemgr release];
Run Code Online (Sandbox Code Playgroud)

  • `dbpath == databasePath`?`contactDB`声明在哪里? (2认同)