数据库应用程序无法正常工作
在我的应用程序中,我在一个循环中多次打开数据库,然后再次打开数据库,它不会执行 if (sqlite3_open([[self getDBPath] UTF8String], &database) == SQLITE_OK)这个声明所以我的应用程序不起作用......
-(NSString *)getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"BasicGreetings.sqlite"];
}
-(void) fillimage:(NSInteger)imgno
{
testAppDelegate *appDelg = (testAppDelegate *)[[UIApplication sharedApplication]delegate];
sqlite3 *database= nil;
if (sqlite3_open([[self getDBPath] UTF8String], &database) == SQLITE_OK)
{
const char *sql = "select setflag from Tblsetflag where imgNo = ?";
sqlite3_stmt *selectStmt = nil;
if (sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK)
{
sqlite3_bind_int(selectStmt, 1,imgno);
while (sqlite3_step(selectStmt) == SQLITE_ROW)
{
appDelg.a = sqlite3_column_int(selectStmt, 0);
NSLog(@"%d",appDelg.a);
}
}
}
sqlite3_close(database);
}
In my application i open the database many time in one loop and then again i open the database it will not execute if (sqlite3_open([[self getDBPath] UTF8String], &database) == SQLITE_OK)
this statement so my application doesnt work....
-(NSString *)getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"BasicGreetings.sqlite"];
}
-(void) fillimage:(NSInteger)imgno
{
testAppDelegate *appDelg = (testAppDelegate *)[[UIApplication sharedApplication]delegate];
sqlite3 *database= nil;
if (sqlite3_open([[self getDBPath] UTF8String], &database) == SQLITE_OK)
{
const char *sql = "select setflag from Tblsetflag where imgNo = ?";
sqlite3_stmt *selectStmt = nil;
if (sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK)
{
sqlite3_bind_int(selectStmt, 1,imgno);
while (sqlite3_step(selectStmt) == SQLITE_ROW)
{
appDelg.a = sqlite3_column_int(selectStmt, 0);
NSLog(@"%d",appDelg.a);
}
}
}
sqlite3_close(database);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您能否将代码发布到 getDBPath,我怀疑它找不到该文件...检查从该方法返回的值(在模拟器中运行)并在 Finder 中浏览模拟器文档目录并验证文件是否存在。
Can you post the code to getDBPath, I suspect it can't find the file... check that value you return from that method (run in the simulator) and browse the simulator documents directory in Finder and verify the file exists.
不要在每次通过
-fillimage:
方法时打开和关闭数据库。您确实应该在应用程序启动时(或当您的应用程序从后台变为活动状态时)打开它一次,并在应用程序退出或转到后台时关闭它。您也永远不会完成准备好的语句,这可能会导致
sqlite3_close()
失败。同样,您可能希望创建一次准备好的语句,然后在每次运行该方法时重置它,并在 SQLite 数据库关闭之前完成它。Don't open and close your database on every pass through your
-fillimage:
method. You really should open it once at the start of your application (or when your application has become active from the background) and close it when your application exits or goes to the background.You also never finalize your prepared statement, which may be leading to the
sqlite3_close()
failing. Again, you may want to create a prepared statement once, then reset it every time you run that method and finalize it before the SQLite database is closed.