iphone - Sqlite3 Database Locked Exception -
i tried many methods, in order rid of error, didn't succeed. newbie , tried figure out other questions , tried advices, didn't work.
here code:
- (void) addobiectasigurat { sqlite3 *database = nil; sqlite3_stmt *addstmt = nil; if(sqlite3_open([[database getdbpath] utf8string], &database) == sqlite_ok) { if(addstmt == nil) { const char *sql = "insert obiectasigurat (idintern, tipobiect, jsontext) values(?, ?, ?)"; if(sqlite3_prepare_v2(database, sql, -1, &addstmt, null) != sqlite_ok) nsassert1(0, @"error while creating add together statement. '%s'", sqlite3_errmsg(database)); } sqlite3_bind_text(addstmt, 1, [idintern utf8string], -1, sqlite_transient); sqlite3_bind_int(addstmt, 2, tipobiect); sqlite3_bind_text(addstmt, 3, [jsontext utf8string], -1, sqlite_transient); if(sqlite_done != sqlite3_step(addstmt)) nsassert1(0, @"error while inserting data. '%s'", sqlite3_errmsg(database)); else { sqlite3_finalize(addstmt); sqlite3_close(database); }
if utilize @ end of method
} else sqlite3_finalize(addstmt); sqlite3_close(database); }
then error looks this:
assertion failure in -[ytoobiectasigurat addobiectasigurat] terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'error while inserting data. 'database locked'' *** first throw phone call stack: (0x1ba3012 0x16fae7e 0x1ba2e78 0x1190665 0xb6661 0x3d534 0x63a82 0x1200589 0x11fe652 0x11ff89a 0x11fe60d 0x11fe785 0x114ba68 0x2696911 0x2695bb3 0x26d3cda 0x1b458fd 0x26d435c 0x26d42d5 0x25be250 0x1b26f3f 0x1b2696f 0x1b49734 0x1b48f44 0x1b48e1b 0x1ad27e3 0x1ad2668 0x63effc 0xf4d5 0x2615) libc++abi.dylib: terminate called throwing exception
if utilize this
sqlite3_finalize(addstmt); } sqlite3_close(database); }
i bad thread 1:
exc_bad_access libsqlite3.dylib`sqlite3_finalize: 0x37fce0: pushl %ebp 0x37fce1: movl %esp, %ebp 0x37fce3: pushl %ebx 0x37fce4: pushl %edi 0x37fce5: pushl %esi 0x37fce6: subl $28, %esp 0x37fce9: calll 0x37fcee ; sqlite3_finalize + 14 0x37fcee: popl %eax 0x37fcef: movl %eax, -16(%ebp) 0x37fcf2: xorl %edi, %edi 0x37fcf4: movl 8(%ebp), %esi 0x37fcf7: testl %esi, %esi 0x37fcf9: je 0x37fe16 ; sqlite3_finalize + 310 0x37fcff: movl (%esi), %ebx 0x37fd01: testl %ebx, %ebx 0x37fd03: je 0x37fd1f ; sqlite3_finalize + 63 0x37fd05: cmpl $2687084183, 60(%ebx) 0x37fd0c: jne 0x37fd1f ; sqlite3_finalize + 63 0x37fd0e: movl 48(%esi), %eax 0x37fd11: cmpl $3186757027, %eax 0x37fd16: je 0x37fd6a ; sqlite3_finalize + 138 0x37fd18: cmpl $1369188723, %eax 0x37fd1d: je 0x37fd6a ; sqlite3_finalize + 138 0x37fd1f: movl -16(%ebp), %esi 0x37fd22: leal 726721(%esi), %eax 0x37fd28: movl %eax, 4(%esp) 0x37fd2c: movl $21, (%esp) 0x37fd33: calll 0x37b7c0 ; sqlite3_log 0x37fd38: leal 705186(%esi), %eax 0x37fd3e: movl %eax, 12(%esp) 0x37fd42: leal 705450(%esi), %eax 0x37fd48: movl %eax, 4(%esp) 0x37fd4c: movl $67126, 8(%esp) 0x37fd54: movl $21, (%esp) 0x37fd5b: calll 0x37b7c0 ; sqlite3_log 0x37fd60: movl $21, %edi 0x37fd65: jmp 0x37fe16 ; sqlite3_finalize + 310 0x37fd6a: movl 12(%ebx), %edi 0x37fd6d: testl %edi, %edi 0x37fd6f: je 0x37fd80 ; sqlite3_finalize + 160 0x37fd71: movl %edi, (%esp) 0x37fd74: movl -16(%ebp), %eax 0x37fd77: calll *759034(%eax) 0x37fd7d: movl 48(%esi), %eax 0x37fd80: movl %edi, -20(%ebp) 0x37fd83: cmpl $3186757027, %eax 0x37fd88: je 0x37fd93 ; sqlite3_finalize + 179 0x37fd8a: xorl %edi, %edi 0x37fd8c: cmpl $1369188723, %eax 0x37fd91: jne 0x37fd9c ; sqlite3_finalize + 188 0x37fd93: movl %esi, %ecx 0x37fd95: calll 0x37ff00 ; sqlite3vdbereset 0x37fd9a: movl %eax, %edi 0x37fd9c: movl 60(%esi), %eax 0x37fd9f: movl (%esi), %ecx 0x37fda1: movl 56(%esi), %edx 0x37fda4: testl %edx, %edx 0x37fda6: je 0x37fdad ; sqlite3_finalize + 205 0x37fda8: movl %eax, 60(%edx) 0x37fdab: jmp 0x37fdb0 ; sqlite3_finalize + 208 0x37fdad: movl %eax, 4(%ecx) 0x37fdb0: movl 60(%esi), %eax 0x37fdb3: testl %eax, %eax 0x37fdb5: je 0x37fdbd ; sqlite3_finalize + 221 0x37fdb7: movl 56(%esi), %edx 0x37fdba: movl %edx, 56(%eax) 0x37fdbd: movl $3053896648, 48(%esi) 0x37fdc4: movl $0, (%esi) 0x37fdca: movl %esi, %edx 0x37fdcc: calll 0x378cd0 ; sqlite3vdbedeleteobject 0x37fdd1: cmpb $0, 50(%ebx) 0x37fdd5: jne 0x37fddf ; sqlite3_finalize + 255 0x37fdd7: cmpl $3082, %edi 0x37fddd: jne 0x37fe00 ; sqlite3_finalize + 288 0x37fddf: movl %ebx, (%esp) 0x37fde2: movl $0, 8(%esp) 0x37fdea: movl $7, 4(%esp) 0x37fdf2: calll 0x37d090 ; sqlite3error 0x37fdf7: movb $0, 50(%ebx) 0x37fdfb: movl $7, %edi 0x37fe00: andl 44(%ebx), %edi 0x37fe03: movl -20(%ebp), %eax 0x37fe06: testl %eax, %eax 0x37fe08: je 0x37fe16 ; sqlite3_finalize + 310 0x37fe0a: movl %eax, (%esp) 0x37fe0d: movl -16(%ebp), %eax 0x37fe10: calll *759042(%eax) 0x37fe16: movl %edi, %eax 0x37fe18: addl $28, %esp 0x37fe1b: popl %esi 0x37fe1c: popl %edi 0x37fe1d: popl %ebx 0x37fe1e: popl %ebp 0x37fe1f: ret
these methods insert, update, delete , select.
- (void) addobiectasigurat { sqlite3 *database = nil; sqlite3_stmt *addstmt = nil; if(sqlite3_open([[database getdbpath] utf8string], &database) == sqlite_ok) { if(addstmt == nil) { const char *sql = "insert obiectasigurat (idintern, tipobiect, jsontext) values(?, ?, ?)"; if(sqlite3_prepare_v2(database, sql, -1, &addstmt, null) != sqlite_ok) nsassert1(0, @"error while creating add together statement. '%s'", sqlite3_errmsg(database)); } sqlite3_bind_text(addstmt, 1, [idintern utf8string], -1, sqlite_transient); sqlite3_bind_int(addstmt, 2, tipobiect); sqlite3_bind_text(addstmt, 3, [jsontext utf8string], -1, sqlite_transient); if(sqlite_done != sqlite3_step(addstmt)) nsassert1(0, @"error while inserting data. '%s'", sqlite3_errmsg(database)); else { sqlite3_finalize(addstmt); sqlite3_close(database); } sqlite3_finalize(addstmt); } sqlite3_close(database); } - (void) updateobiectasigurat { sqlite3 *database = nil; sqlite3_stmt *updatestmt = nil; if(sqlite3_open([[database getdbpath] utf8string], &database) == sqlite_ok) { if(updatestmt == nil) { nsstring *update = [nsstring stringwithformat:@"update obiectasigurat set jsontext = ? idintern='%@'", idintern]; if(sqlite3_prepare_v2(database, [update utf8string], -1, &updatestmt, null) != sqlite_ok) nsassert1(0, @"error while creating update statement. '%s'", sqlite3_errmsg(database)); } sqlite3_bind_text(updatestmt, 1, [jsontext utf8string], -1, sqlite_transient); if(sqlite_done != sqlite3_step(updatestmt)) nsassert1(0, @"error while updating data. '%s'", sqlite3_errmsg(database)); else { sqlite3_finalize(updatestmt); sqlite3_close(database); } sqlite3_finalize(updatestmt); } sqlite3_close(database); } - (void) deleteobiectasigurat { sqlite3 *database = nil; sqlite3_stmt *deletestmt = nil; if(sqlite3_open([[database getdbpath] utf8string], &database) == sqlite_ok) { if(deletestmt == nil) { nsstring *delete = [nsstring stringwithformat:@"delete obiectasigurat idintern='%@'", idintern]; if(sqlite3_prepare_v2(database, [delete utf8string], -1, &deletestmt, null) != sqlite_ok) nsassert1(0, @"error while creating delete statement. '%s'", sqlite3_errmsg(database)); } if(sqlite_done != sqlite3_step(deletestmt)) nsassert1(0, @"error while deleting data. '%s'", sqlite3_errmsg(database)); else { sqlite3_finalize(deletestmt); sqlite3_close(database); } sqlite3_finalize(deletestmt); } sqlite3_close(database); } + (ytoobiectasigurat *) getobiectasigurat:(nsstring *)idintern { ytoobiectasigurat * ob = [[ytoobiectasigurat alloc] init]; sqlite3 *database; sqlite3_stmt *selectstmt = nil; if (sqlite3_open([[database getdbpath] utf8string], &database) == sqlite_ok) { nsstring * sqlstring = [nsstring stringwithformat:@"select idintern, tipobiect, jsontext obiectasigurat idintern='%@'", idintern]; const char *sql = [sqlstring utf8string]; if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, null) == sqlite_ok) { while(sqlite3_step(selectstmt) == sqlite_row) { ob._isdirty = yes; ob.idintern = [nsstring stringwithutf8string:(char *)sqlite3_column_text(selectstmt, 0)]; ob.tipobiect = sqlite3_column_int(selectstmt, 1); ob.jsontext = [nsstring stringwithutf8string:(char *)sqlite3_column_text(selectstmt, 2)]; } } sqlite3_finalize(selectstmt); } sqlite3_close(database); homecoming ob; } + (nsmutablearray *) getlistabytipobiect:(int)tip; { nsmutablearray * _list = [[nsmutablearray alloc] init]; sqlite3 *database; sqlite3_stmt *selectstmt = nil; if (sqlite3_open([[database getdbpath] utf8string], &database) == sqlite_ok) { nsstring * sqlstring = [nsstring stringwithformat:@"select idintern, tipobiect, jsontext obiectasigurat tipobiect=%d",tip]; const char *sql = [sqlstring utf8string]; if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, null) == sqlite_ok) { while(sqlite3_step(selectstmt) == sqlite_row) { ytoobiectasigurat * ob = [[ytoobiectasigurat alloc] init]; ob._isdirty = yes; ob.idintern = [nsstring stringwithutf8string:(char *)sqlite3_column_text(selectstmt, 0)]; ob.tipobiect = sqlite3_column_int(selectstmt, 1); ob.jsontext = [nsstring stringwithutf8string:(char *)sqlite3_column_text(selectstmt, 2)]; [_list addobject:ob]; } } sqlite3_finalize(selectstmt); } sqlite3_close(database); homecoming _list; } @end
edit: solved problem easy.
i didn't finalize , close database in method insert, update, delete, reset statement , create nil. after this, declare method finalize, close database , finalize statements. maybe thought help somebody.
make sure closing database connection in other methods work it.
since phone call method in loop, there much more efficient way work database when need mass insert of data: rebuild application loop through values within insert this: example, insert proper values here
-(void)insertvaluesfromarray:(nsmutablearray *)array { sqlite3 * database; const char *dbpath = [[database getdbpath] utf8string]; if (sqlite3_open(dbpath, &database) == sqlite_ok) { sqlite3_exec(database, "begin transaction", 0, 0, 0); const char *sqlstatement = "insert obiectasigurat (idintern) values(?)"; sqlite3_stmt *compiledstatement; if (sqlite3_prepare_v2(database, sqlstatement, -1, &compiledstatement, null) == sqlite_ok) { int haserror = 0; (int i=0; i<[array count]; i++) { sqlite3_bind_text(compiledstatement, 1, [[array objectatindex:i] utf8string], -1, sqlite_transient); if (sqlite3_step(compiledstatement) != sqlite_done) { haserror=1; nslog(@"prepare-error %s", sqlite3_errmsg(database)); } sqlite3_reset(compiledstatement); } sqlite3_finalize(compiledstatement); if( haserror == 0 ) { sqlite3_exec(database, "commit", 0, 0, 0); nslog(@"inserted successfully"); } else { sqlite3_exec(contactdb, "rollback", 0, 0, 0); nslog(@"can't insert:"); } } sqlite3_close(contactdb); } }
iphone database sqlite
No comments:
Post a Comment