Setting user_version in sqlite
Asked Answered
T

2

4

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];

the output I get is:

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) 

I would like to know: why is the user version number is not setting for me?

Thracophrygian answered 17/1, 2014 at 5:41 Comment(0)
D
8

If you need to set PRAGMA user_version use:

[self.db setUserVersion:yourUserVersion]; // yourUserVersion is of uint32_t type

To read current user_version user

[self.db userVersion];                   // returned value is of uint32_t type

FMDB encapsulate it since 2013 so you don't have to deal with it yourself.

Documentation:

http://ccgus.github.io/fmdb/html/Categories/FMDatabase+FMDatabaseAdditions.html

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.

Dehypnotize answered 6/1, 2015 at 21:2 Comment(1)
i used FMDB 2.7.2, the userVersion rollback after restart the app. Any ideas?Carrousel
T
2

I was about to post a bounty when I figured out the issue. And as often happens, I was just being a bonehead

I was doing executeQuery where I should have been doing executeUpdate

_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];

should instead be:

_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 executeUpdate:query];

executing a Pragma is NOT a query, executeQuery doesn't do anything with it. It is instead in the same category as UPDATE, INSERT, and DELETE.

Thracophrygian answered 20/1, 2014 at 21:13 Comment(1)
I think you just copied/past the same block twice. So, what should be the proper way to do it ? Thanks in advance.Patency

© 2022 - 2024 — McMap. All rights reserved.