我正在做一个登录和密码应用程序,如果我想将它连接到SQLite DB,我复制了SQLite框架,导入它并将db文件添加到支持文件夹但它不能正常工作,请通过下面的代码检查,并帮助我
-(IBAction)homePage: (id)sender
{
post =[NSString stringWithFormat:@"loginname=%@ & password=%@",loginName.text,password.text];
NSString *hostStr = @"login.db";
//hostStr = [hostStr stringByAppendingString:post];
NSArray *doumentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir=[doumentsPath objectAtIndex:0];
path =[documentDir stringByAppendingPathComponent:hostStr];
if([path isEqualToString:post]){
homePage *hvc = [[homePage alloc]initWithNibName: nil bundle: nil];
hvc.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:hvc animated: YES];
}
请让我知道锄头来解决这个问题
答案 0 :(得分:0)
我想,问题在这里......
if([path isEqualToString:post]) // 'post' is your username & password
它应该是这样的
if([path isEqualToString: hostStr])
试试..
答案 1 :(得分:0)
sqlite3 *yourDB; // .h declarations
NSString *databasePath;
const char *dbpath;
call this method at start of viewDidLoad
- (void) initDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory
stringByAppendingPathComponent:@“yourDB.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
dbpath = [writableDBPath UTF8String];
// NSLog(@"path : %@", writableDBPath);
if (success) return;
// The writable database does not exist, so copy the default to the
// appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"yourDB.sqlite"];
success = [fileManager fileExistsAtPath:defaultDBPath];
//if(success) return;
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath
error:&error];
if (!success)
{
NSAssert1(0, @"Failed to create writable database file with message '%@'.”,
[error localizedDescription]);
}
dbpath = [writableDBPath UTF8String];
NSLog(@"path : %@", writableDBPath);
}