On iOS5, NullException occurs while reading new data from Sqlite

Hi.

My app worked totally fine on iOS4, but crashes frequently on iOS5.

I have been finding out what is wrong and I just found the clue.

It seems there is a bug to read data from Sqlite on iOS5.

The old data that made on lower iOS than iOS5 can read properly in both Unity and Cocoa.

When I make new data into database and try to get the record in Unity, unity sends an exception and an App get crashed.

This is the code

	public UserInfo getUserInfo(int key)
	{
		UserInfo userInfo = new UserInfo();
				
		string query = "SELECT * FROM user_info WHERE id= " + key;
		IDbCommand dbcmd = dbcon.CreateCommand();
		dbcmd.CommandText = query;
		dbcmd.Prepare();
		IDataReader reader = dbcmd.ExecuteReader(); // <- crash here
		byte[] imageByte = null;
		long imageSize = 0;
		
	    while(reader.Read()) {
            userInfo.id 	= reader.GetInt32(0);
        	imageSize		= reader.GetBytes(1, 0, null, 0, 0);
    		if(imageSize > 0)
    		{
        		imageByte 		= new byte[imageSize];
        		reader.GetBytes(1, 0, imageByte, 0, (int)imageSize);
    			userInfo.photo = new Texture2D(FACE_FRAME_WIDTH, FACE_FRAME_HEIGHT);
				userInfo.photo.LoadImage(imageByte);
    		}
			userInfo.sex 	= reader.GetInt32(2);
			userInfo.type 	= reader.GetInt32(3);
       	}

		reader.Close();	
		reader = null;
		dbcmd.Dispose();
		dbcmd = null;
		
		return userInfo;
	}

In addition, The most strange thing is that the new data can be read In Cocoa without any problem.

    HUserInfo *info  = [aquaDB getHUserInfo:personID];
    NSLog(@" id = %d", info.iD);
    NSLog(@" type = %d", info.type); // No matter to read.

Does anybody have problem to handle Sqlite on iOS5 like me?

If you are storing your database in Documents folder then Cloud synchronization might be doing its dark job: https://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/iCloud/iCloud.html#//apple_ref/doc/uid/TP40007072-CH5-SW1

Thanks for reply, mantasp.
I have one more question if the folder is synchronized by iCloud and SQLite is not supported, why is it possible to read data from SQLite in XCode? Could you see the second code?

Your Xcode sample is too short to say anything about it. If you are using Core Data then it is supposed to work as stated in the document I linked.

I do not use CoreData. I implement a wrapper class of sqlite3.lib and it manages sqlite directly.

This is a part of the class.

- (HUserInfo* ) getHUserInfo : (NSInteger)key
{
//	NSLog(@"->getHUserInfo()\n");
	NSString *query = @"SELECT * FROM user_info WHERE id= ? ORDER BY id";
	sqlite3_stmt *stmt;
	HUserInfo* userinfo = [[HUserInfo alloc]init];
	if( sqlite3_prepare_v2(database, [query UTF8String], -1, &stmt, nil) == SQLITE_OK)
	{
		sqlite3_reset(stmt);
		sqlite3_bind_int(stmt, 1, key);
		
		if( sqlite3_step(stmt) == SQLITE_ROW ) {
			
			//id
			userinfo.iD = sqlite3_column_int(stmt, 0);
			//photo
			NSData *data = [[NSData alloc]initWithBytes:sqlite3_column_blob(stmt, 1) length:sqlite3_column_bytes(stmt, 1)];
			if(data == nil)
				NSLog(@"No image founde.");
			else 
				userinfo.photo = [UIImage imageWithData:data];
			//sex
			userinfo.sex = sqlite3_column_int(stmt, 2);
			//type
			userinfo.type = sqlite3_column_int(stmt, 3);
			[data release];
		}
	}
	else {
		NSLog(@"preparing failed");
	}
	
	sqlite3_finalize(stmt);
	
	return userinfo;
}

Is the all files and database in Document Directory syncronized by iCould, even though an app does have any association with iCould?

Here are a couple of tutorials on using SQLite with the new IOS 5 SDK. This may give you some insight with the changes between the versions. From what I can see, you code looks ok.

http://klanguedoc.hubpages.com/hub/Tutorial-on-Creating-an-IOS-5-SQLite-Database-Application-IOS-5-SQLite

http://klanguedoc.hubpages.com/hub/IOS-5-SDK-Database-Insert-Update-Delete-with-SQLite-and-Objective-C-C-How-To

kevlangdo