Start a transaction with:sqlite3_exec(db,"BEGIN",0);
Commit a transaction with:
一下的代码是用transaction的,首先尝试了把两个SQL语句链接在一起的,用一个sqlite3_prepare——似乎没成功,分开来写,也没成功,难道是有什么特别的?
//sqlite3_exec(database,0);
NSLog(@"querysql :%@",sql1);
NSLog(@"querysql :%@",sql2);
if(sqlite3_prepare_v2(database,[sql1 UTF8String],-1,&compiledStatement,NulL) == sqlITE_OK && sqlite3_prepare_v2(database,[sql2 UTF8String],NulL) == sqlITE_OK)
{
//sqlite3_exec(database,0);
retValue=YES;
}
else {
//sqlite3_exec(database,"RolLBACK",0);
}
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
后来我还是想了好多方法,至少SQL语句看起来是可以的,在sql analzer里面是ok的,
if (sqlite3_exec (database,[updatesqlUTF8String],NulL,&errorMsg) != sqlITE_OK)
{
NSLog(@"upate error");
sqlite3_close(database);
return NO;
}
sqlite3_prepare_v2
后来又发现人们评价说“ BEGIN is a deferred operation. Deferred means that no locks are acquired on the database until the database is first accessed. Thus with a deferred transaction,the BEGIN statement itself does nothing to the filesystem. Locks are not acquired until the first read or write operation. Seesqlite.org/lang_transaction.htmlfor other variations on BEGIN that do attempt to obtain locks ” 老实说没看懂,大概是不建议用这个begin:::
后来我又看到这么段代码,觉得挺靠谱的:
//执行事务 @try { sqlite3_exec(database,"BEGIN TRANSACTION",0); //事务开始 Nsstring *s = [[Nsstring alloc] initWithUTF8String:[要执行的sql 语句 UTF8String]]; char *sql = (char *) [s UTF8String]; int r = sqlite3_exec( database,sql,0 ); if(r != sqlITE_OK){ //NSLog(@" sql: %@",s); //NSLog(@"r = %d",r); } NSLog(@"updatesql %@",ssql); NSLog(@"r = %d",r); [s release]; } @catch (NSException * em) { NSLog(@"Failed to read %@",[em description]); } @finally { //NSLog(@"Failed to read %@",[e description]); }
} intresult = sqlite3_exec(database,&error); //COMMIT 我觉得这次应该i哦y验 饿 我把其中一个update搞失败? 找不到key?
【2】创建表格
//创建表格,假设有五个字段,(ID,cID,Title,imageData ,imageLen )
//说明一下,ID为表格的主键,必须有。
//cID,和Title都是字符串,imageData是二进制数据,imageLen 是该二进制数据的长度。 - (BOol) createChannelstable:(sqlite3*)db{ char *sql = "CREATE table channels (ID integer primary key,\ cID text,\ Title text,\ imageData BLOB,\ imageLen integer)"; sqlite3_stmt *statement; if(sqlite3_prepare_v2(db,&statement,nil) != sqlITE_OK) { 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; }
总结以上是内存溢出为你收集整理的sqlite transaction事务 *** 作全部内容,希望文章能够帮你解决sqlite transaction事务 *** 作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)