Aug 13, 2008

(iPhone) How to use SQLite

1. Add libsqlite3.dylib framwork in your project.

2. Add #import in your interface file.

3. You should create a .sql file, there are many ways to do it: you can create with you console by taping sqlite3 file.sql or you can create by coding in your project. In my case, i create the database dynamically, so i create the database file in my code, and i will post an example below.

4. Declare 2 variables in your interface file:
sqlite3 *database;
NSString *dbPath;
In you implementation file:
- (void) createTable {
dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sql"];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
NSString *createSql = @"CREATE TABLE test (text varchar(255))";
if (sqlite3_exec(database, [createSql cStringUsingEncoding:NSUTF8StringEncoding], NULL, NULL, NULL) == SQLITE_OK) {
NSLog(@"create table");
NSString *insertSql = @"INSERT INTO test (text) VALUES('fff')";
int testvalue = sqlite3_exec(database, [insertSql cStringUsingEncoding:NSUTF8StringEncoding], NULL, NULL, NULL);
if (testvalue == SQLITE_OK) {
NSLog(@"insert query ok");
}
else {
NSLog(@"error code %i", testvalue);
}
}
}
}

and this method will show you how to use the select query:
- (void) findRowNb {
NSString *selectSql = @"SELECT COUNT(*) FROM test";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [selectSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
int count = sqlite3_column_int(statement, 0);
NSLog(@"row nb %i", count);
}
}
}

Aug 12, 2008

(Objective-C) How to use NSURLConnection

Create an object of NSMutableURLRequest, you can set the header field in this object. And then create an object of NSURLConnection with the parameter of NSMutableURLRequest and delelgate (its self).

The start method "start" is not necessary, in my case i got application crashed when i add start method. Don't forget set up a timer
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];
after the start method.

The last thing is use the delegate methods of NSURLConnection, such as:
(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
(void)connectionDidFinishLoading:(NSURLConnection *)connection

In these method, you can check the status of connection, received data and cancel the connectin in connectionDidFinishLoading.

Aug 4, 2008

(Objective-C) Add a wait time in NSUrlConnection

Here is a regular connection object

NSURLConnection *askConnection = [[NSURLConnection alloc] initWithRequest:askRequest delegate:self];
[askConnection start];

this connection in the main thread, it has a default time to wait for http response and received data. But if we put this connection in a separate thread, it won't wait for response, in this case we should add the follow line to wait the connection response

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10]];

(Objective-C) How to use multi-threading in Objective-C

To create a new thread:
[NSThread detachNewThreadSelector:@selector(threadSelector) toTarget:self withObject:nil];

in the threadSelector method we add what we want the thread to do, but don't forget to create a new NSAutoreleasePool object to manage all the autoreleased objects in the thread.

+ (void) threadSelector {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

//Add what you want the thread to do

[NSThread exit];
[pool release];
}

Aug 1, 2008

(C) How to printf a long long data type integer

The conversion specifiers for long long include "%lld" "%llu" "%llx". And "%lld" is for long long type, "%llu" is for unsigned long long type