我似乎无法完全理解这个数据库问题。
首先,我应该从一开始就打开数据库连接并保持打开状态直到应用程序进入后台吗?或者我应该在每次插入新连接时打开并关闭它。
我的打开连接方法
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [docPaths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"insider.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
[database executeUpdate:@"create table if not exists userInfo (device_token text primary key, device_type text, carrier text, app_version text,os_version text, first_name text, last_name text,last_viewed_item text, last_added_item text, last_item_price_tag text, name_entered integer, login_screen integer, item_detailed_screen integer,item_selected integer )"];
// [database close];
我使用此方法启动应用程序(在 appDelegate applicationDidFinishLauch 中),但在 session 期间我想将所有新数据保存到同一行。
但使用我正在使用的方法
-(void)getUserFirstName:(NSString *)firstName {
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [docPaths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"insider.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
[database executeUpdate:@"INSERT INTO userInfo (first_name) VALUES (?)",firstName];
[database close];
}
和我的其他方法
-(void)getUserLastName:(NSString *)lastname {
NSLog(@"soyisim giriliyo");
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [docPaths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"insider.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
[database executeUpdate:@"INSERT INTO userInfo (last_name) VALUES (?)",lastname];
[database close];
}
但是当我保存这些并尝试收集它们时,我得到了这个
2014-09-07 20:26:08.522 PushChat[2974:60b] first Name: Frank
lastname: (null)
2014-09-07 20:26:08.522 PushChat[2974:60b] first Name:: (null)
lastname: Jemmyson
2014-09-07 20:26:08.522 PushChat[2974:60b] first Name: george
lastname: (null)
2014-09-07 20:26:08.522 PushChat[2974:60b] first Name: (null)
lastname: herose
我获取每行的字段值。
我尝试更改我的创建表方法并添加id整数主键,
但在其他插入期间我无法跟踪此 session id,我认为这不是一个好的方法为了性能。
那么我应该这样做吗?我总共有 13 个字段,其中一些可能为空,但在 session 期间所有这些字段都必须位于同一行。
提前致谢
最佳答案
一些想法:
为什么有两种方法来插入名字和姓氏?你需要那个吗?您有什么理由不在单个 SQL 语句中执行此操作?
假设您必须像这样在单独的语句中执行此操作,第一个 SQL 语句应该是
INSERT
,第二个 SQL 语句应该是UPDATE
。如果您愿意,SQLite 可以为您跟踪该行的唯一标识符。因此,添加一个名为
user_info_id integerprimary key autoincrement
的列。然后,在插入数据后,立即查看[database lastInsertRowId]
,这将返回为您的user_info_id
列自动生成的值。因此,回到之前的观点,您将插入一个包含名字的行,立即检索
lastInsertRowId
,然后将其用作WHERE
的参数UPDATE
语句中设置姓氏的子句,例如,success = [database executeUpdate:@"UPDATE userInfo SET last_name = ? WHERE user_info_id = ?", lastName, @(rowIdValue)];
请注意,
user_info_id
的值可能存储在sqlite_int64
变量中(例如,lastInsertRowId
方法返回的值) ,因此当您在executeUpdate
调用中使用它时,通过用@(rowIdValue)
包装它使其成为NSNumber
。虽然 FMDB 通过将值绑定(bind)到列等方式为您省去了很多麻烦,但您仍然应该检查返回值(通常是 FMDB 中的
BOOL
值)。如果失败,则记录[database lastErrorMessage]
。不要为每个 SQL 语句打开和关闭数据库。打开数据库一次并保持打开状态。当您执行
INSERT
/UPDATE
语句时,它会自动提交它们,因此非常强大。
关于mysql - 如何将 sqlite 中每个 session 的所有新值插入到同一行(适用于 iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25713002/