Connecting SQLite3 to Ipad



我正在做一个登录和密码应用程序,如果我想将其连接到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];
}

请告诉我如何解决这个问题

我认为问题在这里…

 if([path isEqualToString:post])  // 'post' is your username & password

应该是这样的

 if([path isEqualToString: hostStr])

试试. .

         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);
       }

最新更新