我是 IOS 开发新手,所以我正在关注这个
正如本教程中提到的,我使用 SQLITE 命令行创建了数据库,创建了表,然后通过将数据库添加到支持文件文件夹来将数据库导入到我的 XCode 4.6 项目中。
我只想用数据填充表,因此我有一个函数首先找到我的数据库并将其复制到 Documents 文件夹(如果不存在,则已经存在):
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Customers.db"];
FMDatabase* db = [FMDatabase databaseWithPath:writableDBPath];
这工作正常,因为 writebleDBPath 指向 Customers.db 所在的实际路径(在项目的 Documents 文件夹中)
现在我打开数据库并尝试添加新记录:
[db open];
BOOL success = [db executeUpdate:@"INSERT INTO customers (firstname,lastname) VALUES (?,?);",[patient.firstName UTF8String],[patient.secondName UTF8String], nil];
[db close];
但成功值(value)始终为“否”。
我包含用于创建 sqlite 数据库的代码:
CREATE TABLE customers(id integer primary key, firstname varchar(30), lastname varchar(30))
我错过了什么?
最佳答案
如果 id
是主键,也许您也需要设置它。
尝试调用[db lastErrorMessage]
来查看问题所在。
关于ios - 无法使用 FMDB 包装器插入 SQLITE 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15350784/