没有创建SQLite表

时间:2012-08-14 08:38:28

标签: ios sqlite

我在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 / <中创建EM> * /文件/.

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

你知道为什么吗?

1 个答案:

答案 0 :(得分:0)

复制db后

以编程方式创建表..确实可以工作!!!!