0

我的 viewDidLoad() 中有以下代码:

// Create a string containing the full path to the sqlite.db inside the documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"sqlite.db"];

// Check to see if the database file already exists
bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];

// Open the database and store the handle as a data member
if (sqlite3_open([databasePath UTF8String], &databaseHandle) == SQLITE_OK)
{
    // Create the database if it doesn't yet exists in the file system
    if (!databaseAlreadyExists)
    {
        // Create the PERSON table
        const char *sqlStatement = "CREATE TABLE IF NOT EXISTS PERSON (ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRSTNAME TEXT, LASTNAME TEXT, BIRTHDAY DATE)";
        char *error;
        if (sqlite3_exec(databaseHandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK)
        {
            // Create the ADDRESS table with foreign key to the PERSON table
            sqlStatement = "CREATE TABLE IF NOT EXISTS ADDRESS (ID INTEGER PRIMARY KEY AUTOINCREMENT, STREETNAME TEXT, STREETNUMBER INT, PERSONID INT, FOREIGN KEY(PERSONID) REFERENCES PERSON(ID))";
            if (sqlite3_exec(databaseHandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK)
            {
                NSLog(@"Database and tables created.");
            }
            else
            {
                NSLog(@"Error: %s", error);
            }
        }
        else
        {
            NSLog(@"Error: %s", error);
        }
    }
}

sqlite3_close(databaseHandle);

我希望这两个表都在文件夹 /Users/ * /Library/Application Support/iPhone Simulator/5.1/Applications/ * /Documents/ 中创建。

但是当我去那个文件夹并输入 sqlite3 和 .schema 时,什么都没有出现。我确实看到 sqlite.db 是在该文件夹中创建的。

你知道为什么吗?

4

1 回答 1

0

复制数据库后,以编程方式创建表..它肯定会工作!!!!

于 2013-02-02T07:26:52.800 回答