2013-03-20 25 views
2

我想從sqlite數據庫中提取數據。我添加了「libsqlite3.0.dylib」文件並將創建的sqlite數據庫複製到我的應用程序文件夾中。我在appdelegate文件中創建了兩個方法。他們是沒有在Sqlite中找到的表格iOS

//To copy DB 
- (void) copyDatabaseIfNeeded { 

    //Using NSFileManager we can perform many file system operations. 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSString *dbPath = [self getDBPath]; 
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(!success) { 
     NSLog(@"DB File %@ does not exists", dbPath); 
     NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"tms.sqlite"]; 
     success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; 

     NSLog(@"Successfully copied db file to path %@", dbPath); 
     if (!success) 
      NSLog(@"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } else { 
     NSLog(@"DB File %@ already exists", dbPath); 
    } 
} 

//To get DB path 
-(NSString *)getDBPath{ 
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentDir=[paths objectAtIndex:0]; 
    return [documentDir stringByAppendingPathComponent:@"sampleDB.sqlite"]; 
} 

我已在didFinishLaunchingWithOptions稱爲copyDatabaseIfNeeded()和我已經加入ViewController.m文件中的一個方法。那是

-(void)Display 
{ 
    SqlitAppDelegate *appDelegate=(SqlitAppDelegate *)[UIApplication sharedApplication].delegate; 
    NSString *dbPath=[appDelegate getDBPath]; 
    if (sqlite3_open([dbPath UTF8String], &myDB) == SQLITE_OK) 
    { 
     NSLog(@"Database opned successflly"); 
     const char *sql = "select * from sampleTable"; 
     NSLog(@"%s",sql); 
     sqlite3_stmt *selectstmt; 
     if(sqlite3_prepare_v2(myDB, sql, -1, &selectstmt, NULL) == SQLITE_OK) 
      NSLog(@"Prepared successfully"); 
     else 
     { 
      NSLog(@"Preparation failed"); 
      NSLog(@"%s",sqlite3_errmsg(myDB)); 
      NSLog(@"%d",sqlite3_errcode(myDB)); 
     } 


    } 
    else 
     NSLog(@"Couldn't open database"); 


} 

我在didViewLoad方法中調用了這個方法。

我得到follwing輸出:

  • 2013年3月20日12:02:24.026 SqliteSample2 [951:11303]數據庫successflly
  • opned
  • 2013年3月20日12:02:24.027 SqliteSample2 [ 951:11303]從sampleTable
  • 選擇*
  • 2013年3月20日12:02:24.028 SqliteSample2 [951:11303]製備失敗
  • 2013年3月20日12:02:24.029 SqliteSample2 [951:11303]沒有這樣的表格:sampleTable
  • 2013-03-20 12:02:24.029 SqliteSam PLE2 [951:11303] 1個

有人告訴我做什麼??????

在此先感謝...........

+3

你是否從設備/模擬器中刪除了現有的應用程序?新的數據庫更改只會反映您是否安裝新版本。如果文件不存在,您的副本數據庫功能將僅使用新副本。 – 2013-03-20 06:53:01

+0

簡而言之,從設備/模擬器中刪除應用程序,清理並重建項目,然後在設備/模擬器中運行。 – Raptor 2013-03-20 06:56:22

+0

手動執行一件事情,並檢查是否創建了任何數據庫。如果沒有手動複製那裏創建的數據庫,並檢查 – 2013-03-20 06:58:00

回答

0

導入SqlitAppDelegate在您的視圖控制器,使對象像SqlitAppDelegate *app之後@implementation ViewController中

viewDidLoad中

app= (SqlitAppDelegate *)[[UIApplication sharedApplication]delegate]; 

    databasepath = [app getDBPath]; 

    if(sqlite3_open([databasepath UTF8String], &dbAssessor) == SQLITE_OK) 
    { 
     NSString *sql = [NSString stringWithFormat:@"select * from SampleTable ;"]; 

     sqlite3_stmt *selectstmt; 
     const char *sel_query=[sql UTF8String]; 

     if(sqlite3_prepare(dbAssessor, sel_query, -1, &selectstmt, NULL) == SQLITE_OK) 
     { 
      while(sqlite3_step(selectstmt) == SQLITE_ROW) 
      { 
       NSLog(@"Prepared Successfully..."); 
      } 
     } 
     sqlite3_finalize(selectstmt); 
    } 
    else 
     sqlite3_close(dbAssessor); 

請嘗試此代碼並將結果傳遞給我。請享用 !!!