因为我对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 __ nor
发布于 2014-01-25 11:17:54
是的,我解决了这个问题,…!实际上,我只是将目录的路径更改为直接到我的DB路径。这是我的最后和工作代码…不管怎样,谢谢@HotLick,,,@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);
}}
而这种访问DB的方式只是混淆了…:/
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;发布于 2014-01-24 17:13:45
有时阅读文献资料会有所帮助
如果nByte参数小于零,则将zSql读入第一个零终止符。如果nByte是非负的,那么它就是从zSql读取的最大字节数.
https://stackoverflow.com/questions/21316073
复制相似问题