如何以编程方式创建sqlite数据库? [英] How to create sqlite database programmatically?

查看:73
本文介绍了如何以编程方式创建sqlite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to iPhone development. I have created the database using the terminal in mac os x. Now i am trying to create the database programatically in iPhone application using objective- C. Can somebody tell me a good site where i can view the sample applications which are worked and is there any site for learning the tutorial? Please somebody help me.

推荐答案

来自关于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];

这篇关于如何以编程方式创建sqlite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆