I created new DB in Documents using SQLite Manager.
Created a table there too with a sample row.
This code I am using for db path :
+(ModelManager *) getInstance
{
if(!instance)
{
instance=[[ModelManager alloc]init];
instance.database=[FMDatabase databaseWithPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"Inventorydb.sqlite"]];
}
return instance;
}
Now when I am using function to display data; it shows :
DB Error: 1 "no such table: inventorydata"
Code for Display data is like this :
-(void) displayData
{
[instance.database open];
FMResultSet *resultSet=[instance.database executeQuery:#"SELECT * FROM inventorydata"];
if(resultSet)
{
while([resultSet next])
NSLog(#"UPC : %# Name : %#",[resultSet stringForColumn:#"upc"],[resultSet stringForColumn:#"name"]);
}
[instance.database close];
}
Image Showing Created table in sqlite manager.
Whats the issue here.
I missed to copy DB file inside App Delegate file.
[Util copyFile:#"Inventorydb.sqlite"];
Related
As the title, how to use Vacuum to shrink a SQLite database using FMDB?
Thanks in advance!
Thanks for all your support
I've found out the answer : [database executeUpdate:#"vacuum"];
1.Database Updating
I have a single database controller — VSDatabaseController in my latest app — that talks to SQLite via FMDB.
FMDB differentiates between updates and queries. To update the database the app calls:
-[VSDatabaseController runDatabaseBlockInTransaction:(VSDatabaseUpdateBlock)databaseBlock]
VSDatabaseUpdateBlock is simple:
typedef void (^VSDatabaseUpdateBlock)(FMDatabase *database);
runDatabaseBlockInTransaction is also simple:
- (void)runDatabaseBlockInTransaction:(VSDatabaseUpdateBlock)databaseBlock {
dispatch_async(self.serialDispatchQueue, ^{
#autoreleasepool {
[self beginTransaction];
databaseBlock(self.database);
[self endTransaction];
}
});
}
Here’s a simple example of a call to update the database:
SELECT ALL
- (void)emptyTagsLookupTableForNote:(VSNote *)note {
NSString *uniqueID = note.uniqueID;
[self runDatabaseBlockInTransaction:^(FMDatabase *database) {
[database executeUpdate:
#"delete from tagsNotesLookup where noteUniqueID = ?;", uniqueID];
}];
}
[self.database executeUpdate:
#"CREATE INDEX if not exists noteUniqueIDIndex on tagsNotesLookup (noteUniqueID);"];
Database Fetching
To fetch objects, the app calls:
SELECT ALL
-[VSDatabaseController runFetchForClass:(Class)databaseObjectClass
fetchBlock:(VSDatabaseFetchBlock)fetchBlock
fetchResultsBlock:(VSDatabaseFetchResultsBlock)fetchResultsBlock];
These two lines do much of the work:
SELECT ALL
FMResultSet *resultSet = fetchBlock(self.database);
NSArray *fetchedObjects = [self databaseObjectsWithResultSet:resultSet
class:databaseObjectClass];
A database fetch using FMDB returns an FMResultSet. With that resultSet you can step through and create model objects.
3.Keeping Objects in Memory
FMResultSet *resultSet = [self.database executeQuery:#"select uniqueID from some_table"];
4.Web APIs
- (void)uploadNote:(VSNote *)note {
VSNoteAPICall *apiCall = [[VSNoteAPICall alloc] initWithNote:[note detachedCopy]];
[self enqueueAPICall:apiCall];
}
5.Handling Web API Return Values
VSNote *cachedNote = [self.mapTable objectForKey:downloadedNote.uniqueID];
6.Database Migration
[self.database executeUpdate:#"CREATE TABLE if not exists tags "
"(uniqueID TEXT UNIQUE, name TEXT, deleted INTEGER, deletedModificationDate DATE);"];
I hope, it will help you.
I need to display entire friend list's avatar images inside UITableView.
I could as well ask this into Quickblox forums but seems like their support is less responsive there.
I have read Quickblox documentation but can't find an efficient way of getting user's avatar images. All I see is [QBContent TDownloadFileWithBlobID], but I do not know how to use it.
I am fetching contacts using chatContactListDidChange delegate call like below. Now how can I also get all these users' avatar images also?
Note that friendsArray is my data source for the table view, so ideally I would want to store the avatar images as part of the same array.
- (void)chatContactListDidChange:(QBContactList *)contactList
{
NSLog(#"contact list changed");
NSLog(#"current contact list %#", [QBChat instance].contactList);
[self fetchContacts:[QBChat instance].contactList.contacts];
}
- (void) fetchContacts : (NSArray *) contactArray
{
NSMutableArray * userIDArray = [[NSMutableArray alloc] init];
for (QBContactListItem * contact in contactArray)
{
NSString * userIDString = [NSString stringWithFormat:#"%ld", (unsigned long)contact.userID];
[userIDArray addObject:userIDString];
}
if (!userIDArray.count)
{
return;
}
NSString * requestString = [userIDArray componentsJoinedByString:#","];
[QBUsers usersWithIDs:requestString delegate:self context:#"FetchFriends"];
}
- (void)completedWithResult:(Result *)result context:(void *)contextInfo
{
if([result isKindOfClass:[QBUUserPagedResult class]])
{
NSString * context = (__bridge NSString *)contextInfo;
// Success result
if(result.success)
{
QBUUserPagedResult * pagedResult = (QBUUserPagedResult *)result;
if ([context isEqualToString:#"FetchFriends"])
{
if (friendsArray)
{
[friendsArray removeAllObjects];
}
else
{
friendsArray = [[NSMutableArray alloc] init];
}
QBUUser * user = [pagedResult.users objectAtIndex:0];
//user.blobID
friendsArray addObjectsFromArray:pagedResult.users];
}
}
else
{
NSLog(#"Error getting users: %#", context);
}
}
}
Use QBUUser's fields - customData for public url of user's image and blobID for uploaded blob.
Image --> [QBContent TUploadFile...] --> Getting callback with result (QBCFileUploadTaskResult) --> getting blob (QBCBlob) --> save blob id to user.blobID --> get public url of your image: [currentBlob publicURL] --> save to user.customData --> update user: [QBUsers updateUser...]
Hope it helps you.
In my ios app, I am using Magical Record and NSFetchedResultsController. I am trying to implement below functionality:
User navigates to settings screen
He selects - 'Delete Account'
All his data is deleted
He is navigated to re-registration screen
To delete all his data I wrote below code:
- (void)cleanAndResetupDB
{
[MagicalRecord cleanUp];
BOOL isSuccess = YES;
for (NSString *dbStore in [self dbBackups]) {
NSError *error = nil;
NSURL *storeURL = [NSPersistentStore MR_urlForStoreName:dbStore];
if(![[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error]){
NSLog(#"An error has occurred while deleting %#", dbStore);
NSLog(#"Error description: %#", error.description);
isSuccess = NO;
}
}
if (isSuccess) {
[MagicalRecord setupCoreDataStackWithStoreNamed:CRP_DB];
}
}
- (NSArray *)dbBackups
{
NSString *shmFileName = [NSString stringWithFormat:#"%#-shm",CRP_DB];
NSString *walFileName = [NSString stringWithFormat:#"%#-wal",CRP_DB];
return #[CRP_DB,shmFileName,walFileName];
}
When registration is complete user is navigated to contacts screen, where we retrieve related contacts from server and store it in local DB. Since FRC is used to retrieve data from local DB and show it in table view, as soon as data is saved in db it automatically appears in table view.
Problem is-
If I quit the app after removing local db, on relaunch it shows proper records, but if I don't quit the app after removing local db, then it shows duplicate records.
Any clues?
If you are using Core Data and you want to remove your database, you have to actually remove your persistent store. Simply deleting the database files is not enough. Core Data caches objects in memory and if it doesn't know that they should be deleted, they could be re-committed to the database. In particular, you are missing the call to removePersistentStore:error:.
NSPersistentStoreCoordinator *storeCoordinator = ...; // you should already have a persistent store coordinator
NSURL *storeURL = [NSPersistentStore MR_urlForStoreName:dbStore];
[storeCoordinator removePersistentStore:store error:&error];
[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
I want to offer the users of my app the possibility to create a backup of the core data database, especially in case he switches to a new device etc.
How would I do that? Especially how can I reimport that file? I mean let's say he makes a backup copy of the database, then changes a ton of stuff and wants to reset to the previous saved backup copy. How would I do that?
Thx!
Take a look at this sample app, it includes functions for making backups, copying backups to and from iCloud, emailing backups and importing backups from email.
http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/
BTW it's much safer to use migratePersistentStore API to make/import backups if your are doing so to and from ICloud. Also be aware that the sample app assumes you are not using WAL mode which is the default mode for iOS 7. WAL mode uses multiple files which all need to be backed up or copied.
Here is a link to a video demonstrating the sample Apps backup and restore capabilities.
http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/sample-apps-explanations/backup-files/
Here are the methods used to create copies for backup. Note that it is possible to open the store with multiple persistentStoreCoordinators so no need to close it down while you make a backup. Restoring it does obviously require the existing store to be removed first. Note that there is little difference between the two methods below except that the source store is opened with or without iCloud options.
/*! Creates a backup of the ICloud store
#return Returns YES of file was migrated or NO if not.
*/
- (bool)backupICloudStore {
FLOG(#"backupICloudStore called");
// Lets use the existing PSC
NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
// Open the store
id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self icloudStoreURL] options:[self icloudStoreOptions] error:nil];
if (!sourceStore) {
FLOG(#" failed to add old store");
migrationPSC = nil;
return FALSE;
} else {
FLOG(#" Successfully added store to migrate");
NSError *error;
FLOG(#" About to migrate the store...");
id migrationSuccess = [migrationPSC migratePersistentStore:sourceStore toURL:[self backupStoreURL] options:[self localStoreOptions] withType:NSSQLiteStoreType error:&error];
if (migrationSuccess) {
FLOG(#"store successfully backed up");
migrationPSC = nil;
// Now reset the backup preference
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:_makeBackupPreferenceKey];
[[NSUserDefaults standardUserDefaults] synchronize];
return TRUE;
}
else {
FLOG(#"Failed to backup store: %#, %#", error, error.userInfo);
migrationPSC = nil;
return FALSE;
}
}
migrationPSC = nil;
return FALSE;
}
/*! Creates a backup of the Local store
#return Returns YES of file was migrated or NO if not.
*/
- (bool)backupLocalStore {
FLOG(#"backupLocalStore called");
// Lets use the existing PSC
NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
// Open the store
id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self localStoreURL] options:[self localStoreOptions] error:nil];
if (!sourceStore) {
FLOG(#" failed to add old store");
migrationPSC = nil;
return FALSE;
} else {
FLOG(#" Successfully added store to migrate");
NSError *error;
FLOG(#" About to migrate the store...");
id migrationSuccess = [migrationPSC migratePersistentStore:sourceStore toURL:[self backupStoreURL] options:[self localStoreOptions] withType:NSSQLiteStoreType error:&error];
if (migrationSuccess) {
FLOG(#"store successfully backed up");
migrationPSC = nil;
// Now reset the backup preference
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:_makeBackupPreferenceKey];
[[NSUserDefaults standardUserDefaults] synchronize];
return TRUE;
}
else {
FLOG(#"Failed to backup store: %#, %#", error, error.userInfo);
migrationPSC = nil;
return FALSE;
}
}
migrationPSC = nil;
return FALSE;
}
/** Sets the selected file as the current store.
Creates a backup of the current store first.
#param fileURL The URL for the file to use.
*/
- (BOOL)restoreFile:(NSURL *)fileURL {
FLOG(#" called");
// Check if we are using iCloud
if (_isCloudEnabled) {
FLOG(#" using iCloud store so OK to restore");
NSURL *currentURL = [self storeURL];
FLOG(#" currentURL is %#", currentURL);
FLOG(#" URL to use is %#", fileURL);
[self saveContext];
[self backupCurrentStoreWithNoCheck];
// Close the current store and delete it
_persistentStoreCoordinator = nil;
_managedObjectContext = nil;
[self removeICloudStore];
[self moveStoreFileToICloud:fileURL delete:NO backup:NO];
} else {
FLOG(#" using local store so OK to restore");
NSURL *currentURL = [self storeURL];
FLOG(#" currentURL is %#", currentURL);
FLOG(#" URL to use is %#", fileURL);
[self saveContext];
[self backupCurrentStoreWithNoCheck];
// Close the current store and delete it
_persistentStoreCoordinator = nil;
_managedObjectContext = nil;
NSError *error = nil;
NSFileManager *fm = [[NSFileManager alloc] init];
// Delete the current store file
if ([fm fileExistsAtPath:[currentURL path]]) {
FLOG(#" target file exists");
if (![fm removeItemAtURL:currentURL error:&error]) {
FLOG(#" error unable to remove current store file");
NSLog(#"Error removing item Error: %#, %#", error, error.userInfo);
return FALSE;
} else {
FLOG(#" current store file removed");
}
}
//
//simply copy the file over
BOOL copySuccess = [fm copyItemAtPath:[fileURL path]
toPath:[currentURL path]
error:&error];
if (copySuccess) {
FLOG(#" replaced current store file successfully");
//[self postFileUpdateNotification];
} else {
FLOG(#"Error copying items Error: %#, %#", error, error.userInfo);
return FALSE;
}
}
// Now open the store again
[self openPersistentStore];
return TRUE;
}
Whatever the persistent store is that you use (binary, SQLite, etc.); it is just a file on the filesystem. You can make a copy of it whenever you want.
If you are using SQLite in iOS 7, be sure to make a copy of the other files associated with it as they are the journal files that go with it. If you are using binary then there will be only a single file.
If you just copy the file there is no import step, you just copy it back to restore it.
There are more advanced designs such as exporting the entire database to something that is portable, such as JSON but that is a different subject.
Update
Well I've used the standard Xcode core data template, so according to the code I've just checked I'm using SQLite. So how do I find all related files? Or could you show me with some example code how to copy and insert back the files needed?
You use NSFileManager to copy the files. You can look at the documents directory in your iOS simulator application to see the names of all the files. Or you could use NSFileManager to scan the documents directory, find everything that starts with the same file name (MyData.* for example) and copy that into a back up directory.
As for sample code, no; it is only a couple of lines of code once you look at the documentation for NSFileManager.
I created the following method with the help of Apple sample code. This will take a backup of core data files and place it to the path that you want.
Swift 5
/// Backing up store type to a new and unique location
/// The method is illustrated in the following code fragment, which shows how you can use migratePersistentStore to take a back up of a store and save it from one location to another.
/// If the old store type is XML, the example also converts the store to SQLite.
/// - Parameters:
/// - path: Where you want the backup to be done, please create a new unique directory with timestamp or the guid
/// - completion: Passes error in case of error or pass nil in case of success
class func backUpCoreDataFiles(path : URL, completion : #escaping (_ error : String?) -> ())
{
// Every time new container is a must as migratePersistentStore method will loose the reference to the container on migration
let container = NSPersistentContainer(name : "<YourDataModelName>")
container.loadPersistentStores
{ (storeDescription, error) in
if let error = error
{
fatalError("Failed to load store: \(error)")
}
}
let coordinator = container.persistentStoreCoordinator
let store = coordinator.persistentStores[0]
do
{
try coordinator.migratePersistentStore(store, to : path, options : nil, withType : NSSQLiteStoreType)
completion(nil)
}
catch
{
completion("\(Errors.coredataBackupError)\(error.localizedDescription)")
}
}
I am developing an app for ipad and i am using sqlite sentences (select, update, insert, delete).
I open (sqlite3_open) the database at the beginning and close (sqlite3_close) at the end of each sentence. But sometimes i´ve got the "database is locked" message.
I don´t know what can i do to solve this.
Thanks and sorry for this little information.
If I'm not mistaken , the problem with sqllite is that you can only access it once at a time.
If you have multiple threads, you can run in this situation. Example:
Run method1 (which accesses the database) on thread t1.
Run method2 (which accesses the database) on thread t2 after x seconds.
If method1 is not finished in those x seconds , both methods will access it at the same time.
And , as I said , I know that sqllite does not support this.
You should try to flag the usage of your database and if you want to access it but it is in use , try again after x seconds. Like this:
- (void) generalMethodThatUsesDatabses
{
if(databaseIsUsed)
{
[self performSelector:#selector(generalMethodThatUsesDatabses) withObject:nil afterDelay:5];
return;
}
databaseIsUsed = TRUE; //global bool variable
//your code here
databaseIsUsed = FALSE;
}
Hope this helps. Cheers!
You probably opened the database before using the same simulator.
To conclude all actions to a database and release all resources you always
have to use both (!) statements:
sqlite3_finalize(statement);
sqlite3_close(database);
A good way to solve this is to wrap this into a C++ library. This way, you can can create the library wrapper on the stack. This means that the moment that the function goes out of scope, you can close the connection in the destructor.
(note that I use reference counting for the Objective-C)
For instance:
NSArray* ScoreBoard::getAllScores()
{
ScoreBoard::ensureExistingTable();
//Stack allocated
SqliteWrapper sqlite("Scores.sqlite");
NSArray* result = sqlite.RunQuery("SELECT * FROM Scores ORDER BY ID DESC");
return result;
//after this, the sqlite destructor is called
}
It is very nice that the Objective-C compiler allows you to merge C++. It can be extremely useful.
Also
void SqliteWrapper::Close()
{
sqlite3_close(db);
}
as Vincent has pointed out, you have to finalize the statement. If you want keep the connection open, use finalize after each statement. Close out the connection the moment you are discarding the connection.
This method works for me.
it is use for three methed
1.isert
2.update
3. delete.
-(NSMutableArray *)resultSet
-(void)insertWithTitle:(NSString *)title Body:(NSString *)body
-(BOOL)updateAtIndex:(int)index Title:(NSString *)title Body:(NSString *)body
NSMutableArray *result = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
FMResultSet *rs = [db executeQuery:[self SQL:#"SELECT * FROM %#" inTable:TABLE_NAME]];
while ([rs next]) {
Record *tr = [[Record alloc] initWithIndex:[rs intForColumn:#"id"]
Title:[rs stringForColumn:#"title"]
Body:[rs stringForColumn:#"body"]];
[result addObject:tr];
[tr release];
}
[rs close];
2....
return result;
[db executeUpdate:[self SQL:#"INSERT INTO %# (title, body) VALUES (?,?)" inTable:TABLE_NAME], title, body];
if ([db hadError]) {
NSLog(#"Err %d: %#", [db lastErrorCode], [db lastErrorMessage]);
Delete record :
BOOL success = YES;
[db executeUpdate:[self SQL:#"DELETE FROM %# WHERE id = ?" inTable:TABLE_NAME], [NSNumber numberWithInt:index]];
if ([db hadError]) {
NSLog(#"Err %d: %#", [db lastErrorCode], [db lastErrorMessage]);
success = NO;
}
return success;
}