2014-11-07 6 views
0

sqlite manager (mozilla)를 사용하여 데이터베이스를 생성하고 테이블을 만들었습니다. 이제 해당 데이터를 iOS 응용 프로그램으로 가져 오려고합니다. 프로그래밍 방식으로 어떻게 할 수 있습니까? 어느 누구도 저에게 어떻게 도와 주실 수 있습니까? 고맙습니다.sqlite manager에서 데이터를 검색하는 방법

+0

는 아래의 튜토리얼에 http 따라 저장 어디 경로 테이블 정보와 컬럼이 이름 YourDB이 : // www가 있습니다. tutorialspoint.com/ios/ios_sqlite_database.htm http://www.appcoda.com/sqlite-database-ios-app-tutorial/ –

답변

1
이 같은

사용 무언가 :

수입 :

#import "sqlite3.h" 

CHECK DB가있는 경우 :

NSString *docsDir; 
NSArray *dirPaths; 
NSString *databasePath; 
sqlite3 *DB; 

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

docsDir = [dirPaths objectAtIndex:0]; 
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"YourDbName.sqlite"]]; //put your db name here 

NSFileManager *filemgr = [NSFileManager defaultManager]; 

if ([filemgr fileExistsAtPath: databasePath ] == NO) 
{ 
    const char *dbpath = [databasePath UTF8String]; 

    if (sqlite3_open(dbpath, &DB) == SQLITE_OK) 
    { 
     char *errMsg; 
     const char *sql_stmt = "CREATE TABLE IF NOT EXISTS YourTable (Value INTEGER PRIMARY KEY, column TEXT)"; 


     if (sqlite3_exec(DB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) 
     { 

     } 

     sqlite3_close(DB); 

    } 
} 

가져 DB 값 :

const char *dbpath = [databasePath UTF8String]; 
     sqlite3_stmt *statement; 


     if (sqlite3_open(dbpath, &DB) == SQLITE_OK) //News is a sqlite variable initialized like this: sqlite3* News; 
     { 
      NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM YourDBName"]; 

      const char *query_stmt = [querySQL UTF8String]; 

      if (sqlite3_prepare_v2(DB, query_stmt, -1, &statement, NULL) == SQLITE_OK) 
      { 
       while(sqlite3_step(statement) == SQLITE_ROW) 
       { 
        NSString* example; //example variable to assign data from db 

        example = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)]; //change the 0 to 1,2,3.... for every column of your db 

       } 


       sqlite3_finalize(statement); 
      } 
      sqlite3_close(DB); 
     } 
+0

감사합니다. @ Singo.It 데이터베이스 파일의 경로를 설정해야합니까? –

+0

내 코드를 편집 한 후 먼저 DB가 있는지 확인한 다음 액세스 할 수 있어야합니다. DB 이름과 DB가 필요한 쿼리가 있으면 편집해야합니다 (테이블 이름과 열 names) – Signo

+0

사용하기 전에 시드 된 데이터베이스 파일을 앱 번들에서 앱 문서 디렉토리로 복사해야한다는 것을 분명히해야합니다. 또는 테이블을 프로그래밍 방식으로 만들 수 있으며 복사가 필요하지 않습니다. – trojanfoe

-2

"sqlite"파일을 App 번들에 넣은 다음 해당 파일을 사용할 수 있습니다.

-1

가로 데이터를 검색하려면 iOS 응용 프로그램 .follow 다음 단계 승 당신의 SQLite는 DB를 고려할 수 있습니다 이름, 장소 및 도시 및 DatabasePath에 당신이 당신의 SQLite는 DB

const char *dbpath = [DatabasePath UTF8String]; 
sqlite3_stmt *statement; 

if (sqlite3_open(dbpath, &YourDB) == SQLITE_OK) 
{ 
NSString *querySQL = [NSString stringWithFormat: 
          @"SELECT Place, City FROM students WHERE Name=Bob"]; 
const char *query_stmt = [querySQL UTF8String]; 

    if (sqlite3_prepare_v2(YourDB,query_stmt, -1, &statement, NULL) == SQLITE_OK) 
    { 
     if (sqlite3_step(statement) == SQLITE_ROW) 
     { 
      NSString *Place = [[NSString alloc] 
             initWithUTF8String: 
             (const char *) sqlite3_column_text(
                     statement, 0)]; 

      NSString *City = [[NSString alloc] 
            initWithUTF8String:(const char *) 
            sqlite3_column_text(statement, 1)]; 
    } 
     sqlite3_finalize(statement); 
    } 
    sqlite3_close(YourDB); 
}