在sqlite中设置user_version [英] Setting user_version in sqlite
问题描述
我在这里看到了关于阅读user_version的其他问题,这似乎对我来说很好。但是我正在尝试在FMDB中使用sqlite来设置我的版本号并且它没有设置。
I have seen other questions on here about reading the user_version, and that seems to be working fine for me. However I'm trying to use sqlite in FMDB to set my version number and it isn't setting.
_db = [self openDatabase];
[_db executeUpdate:[StudentController creationString]];
[_db executeUpdate:[ReadingController creationString]];
[_db executeUpdate:[SessionController creationString]];
NSLog(@"User version is %i", userVersion);
NSString *query = [NSString stringWithFormat:@"PRAGMA USER_VERSION = %i", userVersion];
[_db executeQuery:query];
我得到的输出是:
2014-01-16 22:16:25.438 MyApp[2810:1c103] User version is 2
2014-01-16 22:16:25.439 MyApp[2810:1c103] Query is PRAGMA USER_VERSION = 2
2014-01-16 22:18:09.272 MyApp[2810:1c103] Database copied and created
在运行应用程序一段时间后,数据库保存并加载就好了,我重新启动应用程序并读取版本号,我调用它来检查版本号:
and after running the app for a bit, with the database saving and loading just fine, I restart the app and read the version number and I call this to check the version number:
FMResultSet *ps = [_db executeQuery:@"PRAGMA USER_VERSION"];
NSDictionary *results = [[NSDictionary alloc] init];
while ([ps next]) {
results = [NSDictionary dictionaryWithDictionary:[ps resultDictionary]];
}
结果是一个很好的字典:
and results is a nicely formed dictionary:
(lldb) po results
$0 = 0x09bf5770 {
"user_version" = 0;
}
(lldb)
我想知道:为什么没有为我设置用户版本号?
推荐答案
如果需要设置PRAGMA user_version,请使用:
If you need to set PRAGMA user_version use:
[self.db setUserVersion:yourUserVersion]; // yourUserVersion is of uint32_t type
读取当前user_version用户
To read current user_version user
[self.db userVersion]; // returned value is of uint32_t type
自2013年以来FMDB将其封装,因此您无需处理用它自己。
FMDB encapsulate it since 2013 so you don't have to deal with it yourself.
文档:
http://ccgus.github.io/fmdb/html/Categories/FMDatabase+FMDatabaseAdditions.html
PS:@HalR我认为你可以接受我的答案,因为它可能对访问者搜索如何设置user_version PRAGMA更有用,它也解决了你的neater解决方案的问题。
PS: @HalR I think you can accept my answer as is it probably more useful for visitor searching for "how to set user_version PRAGMA" and it also solves your problem with neater solution as well.
这篇关于在sqlite中设置user_version的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!