2016-08-05 18 views
0

데이터베이스에서 행 단위로 읽으려고합니다. "할당은 sqlite3_column_text() 함수가 포함 된 모든 행에 대해 [기본적으로 활성화 된 캐스트]없이 포인터에서 정수를 만듭니다"라는 경고가 나타납니다. 그러나이 줄은 여전히 ​​데이터베이스에서 0을 반환하지만 정수를 얻으려고 행에서 이러한 오류가 발생합니다.SQLite 데이터베이스에서 읽을 수 없습니다.

소형 처리를 위해 rc 처리가 제거되었습니다.

typedef struct { 
    char InstrumentType[32]; 
    char Protocol[16]; 
    int RegNum; 
    char RW[4]; 
    char RegisterType[32]; 
    char Signed[8]; 
    char Inverted[8]; 
    char DataType[16]; 
} Register; 

typedef struct { 
     char InstrumentType[32]; 
     int Bus; 
     int Address; 
     int Port; 
     int Sampling; 
     char Sync[2]; 
     Register Reg[]; 
     } Instrument; 

int main(void) { 
    sqlite3 *db; 
    Register Register[8]; 
    sqlite3_stmt* SQL; 
    char SQL2[128]; 
    int i; 
    int MPTotalReg; 

    int rc; 

    rc = sqlite3_open("/root/BBBTest/Instruments.db", &db); 

    for (i=0; i<4; i++){ 

    rc = sqlite3_prepare_v2(db, "SELECT * from instrumentsdefinition WHERE Type='Magprobe' AND Register=?;", -1, &SQL, 0); 

    rc = sqlite3_bind_int(SQL, 1, i); 

     *Register[i].InstrumentType=(const char*) sqlite3_column_text(SQL,2); 
     *Register[i].Protocol=(const char*) sqlite3_column_text(SQL,3); 
     Register[i].RegNum= sqlite3_column_int(SQL,5); 
     *Register[i].RW=(const char*) sqlite3_column_text(SQL,5); 
     *Register[i].RegisterType=(const char*) sqlite3_column_text(SQL,6); 
     *Register[i].Signed=(const char*) sqlite3_column_text(SQL,7); 
     *Register[i].Inverted=(const char*) sqlite3_column_text(SQL,8); 
     *Register[i].DataType=(const char*) sqlite3_column_text(SQL,9); 
     printf("Magprobe %d\n", i); 
     printf("Instrument Type: "); 
     printf("%s\n", Register[i].InstrumentType); 
     printf("Protocol: "); 
     printf("%s\n", Register[i].Protocol); 
     printf("Register: "); 
     printf("%d\n", Register[i].RegNum); 
     printf("R/W?: "); 
     printf("%s\n", Register[i].RW); 
     printf("Register Type: "); 
     printf("%s\n", Register[i].RegisterType); 
     printf("Signed?: "); 
     printf("%s\n", Register[i].Signed); 
     printf("Inverted?: "); 
     printf("%s\n", Register[i].Inverted); 
     printf("Data Type: "); 
     printf("%s\n", Register[i].DataType); 
    } 
     sqlite3_finalize(SQL); 
     sqlite3_close(db); 

    return EXIT_SUCCESS; 
} 

오류는 문자 배열 및 문자와 관련이 있습니다. 그러나이 문제를 해결하기 위해 여러 가지 방법을 시도했습니다. 또한 리턴 된 int가 항상 0이라는 사실은 필자의 prepare 또는 bind 문과도 오류가 있다고 믿게한다. 당신이 다른 배열을 할당 할 수 없습니다 사용

+0

배열을 C로 할당 할 수 없습니다. 문자열 내용을 복사해야합니다. –

+0

@CL. 내가 어떻게 그럴 수 있니? – Mst137

답변

1

, 당신은 그렇게 할 수는 포인터를 사용하여 특정 경우 strcpy

unsigned char *value = sqlite3_column_text(SQL,2); 
if ((value != NULL) && (strlen(value) < sizeof(Register[i].InstrumentType))) 
{  
    strcpy(Register[i].InstrumentType, value); 
} 

를 사용하여 대상 배열에 소스 배열의 내용, 예를 복사해야 귀하의 구조체를 리팩터링.

Register[i].InstrumentType=(const char*) sqlite3_column_text(SQL,2); 
if (Register[i].InstrumentType != NULL) 
{ 
    //... 
} 

나는 때문에 최대한 빨리 sqlite3_finalize 전화로, sqlite3_column_tex의 리턴 값은 t 무효가 있다는 사실을 특정 경우을 썼다.


또 다른 것은 바인드 후 쿼리 실행의 결과를 검색 할 수

rc = sqlite3_step(stmt); 

를 호출해야합니다, 쿼리 실행이다. 당신은해야 반환 된 값까지 루프는 == SQLITE_ROW

for (i=0; i<4; i++) 
{ 
    rc = sqlite3_prepare_v2(db, "SELECT * from instrumentsdefinition WHERE Type='Magprobe' AND Register=?;", -1, &SQL, 0); 

    rc = sqlite3_bind_int(SQL, 1, i); 

    rc = sqlite3_step(stmt); 

    if (rc == SQLITE_ROW) 
    { 
     // QUERY retrieve a record from DB 
    } 

마지막 것은 배열의 이름을 변경하는 것이 더 나은 것입니다. 같은 이름의 struct 유형이 정의되지 않았습니다. 같은 것 Register registers[8];

+0

감사합니다. 이 구현하지만 첫 번째 strncpy() 선, 어떤 아이디어를 어떻게 해결할 수있는 세분화 오류가 얻을려고? – Mst137

+0

죄송합니다. 추가 한 것을 보았을 때 제 의견이 삭제되었습니다. 세그먼테이션 결함은 첫 번째 줄에 있습니다. – Mst137

+0

if도 추가 했습니까? – LPs

관련 문제