【问题标题】:Connecting SQLite3 to Ipad将 SQLite3 连接到 Ipad
【发布时间】:2011-12-07 11:51:57
【问题描述】:

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

请告诉我解决这个问题的方法

【问题讨论】:

    标签: iphone ipad sqlite ios5


    【解决方案1】:

    我想,问题就在这里……

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

    应该是这样的

     if([path isEqualToString: hostStr])
    

    试试吧..

    【讨论】:

      【解决方案2】:
               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);
             }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-15
        • 1970-01-01
        • 2014-09-21
        • 2021-04-04
        • 2016-06-18
        • 2018-09-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多