gpt4 book ai didi

objective-c - Sqlite3步骤== SQLITE_ROW对我不起作用

转载 作者:行者123 更新时间:2023-12-03 15:07:05 25 4
gpt4 key购买 nike

由于我是Xcode的新手,所以不知道要解决此问题。请帮助我找出实际问题。

建立数据库连接,执行查询,但是执行查询后,我始终收到消息:未找到结果。 :(

- (void)viewDidLoad
{
NSString *MyDirectory;
NSArray *DirectoryPaths;
// Get the documents directory
DirectoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
MyDirectory = [DirectoryPaths objectAtIndex:0];
// Build the path to the database file
MyDBPath = [[NSString alloc]initWithString: [MyDirectory stringByAppendingPathComponent:@"mydb.db"]];
//Status.text = MyDBPath;
const char *Database = [MyDBPath UTF8String];
if (sqlite3_open(Database, &MyConnection) == SQLITE_OK)
{
Status.text = @"Build Connection Successfully...!";
}
else
{
Status.text = @"Failed to open database...!";
}
[super viewDidLoad];
}


- (void) Find
{
const char *Database = [MyDBPath UTF8String];
sqlite3_stmt *SQLStatement;
NSString *Query;
if (sqlite3_open(Database, &MyConnection) == SQLITE_OK)
{
Query = [NSString stringWithFormat:@"SELECT EmpName FROM EmpLogin WHERE EmpID=\"%@\"",MyText.text];
if (sqlite3_prepare_v2(MyConnection, [Query UTF8String], 0, &SQLStatement, nil) == SQLITE_OK)
{
while (sqlite3_step(SQLStatement) == SQLITE_ROW)
{
NSString *Name = [[NSString alloc]
initWithUTF8String:(const char *) sqlite3_column_text(SQLStatement, 0)];
TextResult.text = Name;
}
if (TextResult.text.length > 0)
{
Status.text = @"Result found";
}
else
{
NSLog(@"%s", sqlite3_errmsg(MyConnection));
Status.text = @"Result not found";
TextResult.text = @"";
}
sqlite3_finalize(SQLStatement);
}
else
{
//NSString *decode = [[NSString alloc]initWithCString:Query_Stmt encoding:NSUTF8StringEncoding];
//Status.text = decode;
Status.text = @"Query execution Failed...!";
}
sqlite3_close(MyConnection);
}
}


Sqlite3_step(SQLStatement)== SQLITE_ROW __不起作用

最佳答案

是的,我解决了这个问题... !!!!
实际上,我只是更改目录的路径以直接指向我的数据库路径。
这是我的最终代码和工作代码……无论如何,感谢@ HotLicks,@ CL。 :) :)

这就像一个魅力……:)



- (void) Find
{
MyDBPath = @"/Users/zaibi/Documents/IOSProjects/mydb.db";
MyDatabase = [MyDBPath UTF8String];
sqlite3_stmt *SQLStatement;
NSString *Query;
//NSString *decode = [[NSString alloc]initWithCString:Database encoding:NSUTF8StringEncoding];
//NSLog(@"%@",decode);
if (sqlite3_open(MyDatabase, &MyConnection) == SQLITE_OK)
{
Query = [NSString stringWithFormat:@"SELECT EmpName FROM EmpLogin WHERE EmpID=\"%@\"",MyText.text];
if (sqlite3_prepare_v2(MyConnection, [Query UTF8String], -1, &SQLStatement, nil) == SQLITE_OK)
{
if (sqlite3_step(SQLStatement) == SQLITE_ROW)
{
NSString *Name = [[NSString alloc]
initWithUTF8String:(const char *) sqlite3_column_text(SQLStatement, 0)];
TextResult.text = Name;
Status.text = @"Result found";
}
else
{
NSLog(@"%s", sqlite3_errmsg(MyConnection));
Status.text = @"Result not found";
TextResult.text = @"";
}
sqlite3_finalize(SQLStatement);
}
else
{
NSLog(@"%s", sqlite3_errmsg(MyConnection));
Status.text = @"Query execution Failed...!";
}
sqlite3_close(MyConnection);
}


}



虽然这种访问数据库的方式令人困惑……:/

NSString *MyDirectory;
NSArray *DirectoryPaths;
// Get the documents directory
DirectoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
MyDirectory = [DirectoryPaths objectAtIndex:0];
// Build the path to the database file
MyDBPath = [[NSString alloc]initWithString: [MyDirectory stringByAppendingPathComponent:@"mydb.db"]];
//Status.text = MyDBPath;

关于objective-c - Sqlite3步骤== SQLITE_ROW对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21316073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com