0
我在viewDidLoad中()下面的代码:SQLite表没有创建
// 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);
而且我期待这两个表在文件夹/用户创建/ * /库/ Application Support/iPhone Simulator/5.1/Applications/*/Documents /。
但是,当我去到该文件夹并键入sqlite3和.schema,没有任何显示。 我确实看到sqlite.db是在该文件夹中创建的。
你知道为什么吗?
你的哪个'NSLog()被执行?是否报告了“创建数据库和表”?如果没有打印日志消息,则代码不会尝试创建表,因为db已经存在。 – Chris 2012-08-14 09:45:48
日志说“数据库和表创建”。 – Yashu 2012-08-14 11:06:26
我发现了这个问题:在终端中,我宁愿输入命令“sqlite3 sqlite.db”而不是“sqlite3”。 – Yashu 2012-08-14 11:23:34