WEB开发网
开发学院手机开发iPhone 开发 iphone开发-SQLite数据库使用 阅读

iphone开发-SQLite数据库使用

 2010-05-29 06:15:00 来源:WEB开发网   
核心提示:NSLog(@"Error: failed to prepare statement:create channels table");return NO;}int success = sqlite3_step(statement);sqlite3_finalize(statement);if ( s

NSLog(@"Error: failed to prepare statement:create channels table");

return NO;

}

int success = sqlite3_step(statement);

sqlite3_finalize(statement);

if ( success != SQLITE_DONE) {

NSLog(@"Error: failed to dehydrate:CREATE TABLE channels");

return NO;

}

NSLog(@"Create table 'channels' successed.");

return YES;

}

【3】向表格中插入一条记录

假设channle是一个数据结构体,保存了一条记录的内容。

- (BOOL) insertOneChannel:(Channel*)channel{

NSData* ImageData = UIImagePNGRepresentation( channel.image_);

NSInteger Imagelen = [ImageData length];

sqlite3_stmt *statement;

static char *sql = "INSERT INTO channels (cid,title,imageData,imageLen)

VALUES(?,?,?,?)";

//问号的个数要和(cid,title,imageData,imageLen)里面字段的个数匹配,代表未知的值,将在下面将值和字段关联。

int success = sqlite3_prepare_v2(database_, sql, -1, &statement, NULL);

if (success != SQLITE_OK) {

NSLog(@"Error: failed to insert:channels");

return NO;

}

//这里的数字1,2,3,4代表第几个问号

sqlite3_bind_text(statement, 1, [channel.id_ UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(statement, 2, [channel.title_ UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_blob(statement, 3, [ImageData bytes], Imagelen, SQLITE_TRANSIENT);

sqlite3_bind_int(statement, 4, Imagelen);

success = sqlite3_step(statement);

sqlite3_finalize(statement);

if (success == SQLITE_ERROR) {

NSLog(@"Error: failed to insert into the database with message.");

return NO;

}

NSLog(@"Insert One Channel#############:id = _);

return YES;

}

【4】数据库查询

Tags:iphone 开发 SQLite

编辑录入:coldstar [复制链接] [打 印]
赞助商链接