문제

맞춤형 디자인 플러그인을 사용하여 API 및 특정 SQL 구문을 사용하여 다른 데이터베이스 엔진과 인터페이스하는 C ++ API의 개발 작업을 진행하고 있습니다.

현재, 나는 멍청이를 삽입하는 방법을 찾으려고 노력하고 있지만 그 이후로 NULL C/C ++의 종단 문자입니까? 에 집어 넣다 쿼리 문자열. 지금까지 나는 함께 일했습니다

//...
char* sql;
void* blob;
int len;
//...
blob = some_blob_already_in_memory;
len = length_of_blob_already_known;
sql = sqlite3_malloc(2*len+1);
sql = sqlite3_mprintf("INSERT INTO table VALUES (%Q)", (char*)blob);
//...

SQLITE3 대화식 콘솔에서 수행 할 수 있다면 올바르게 탈출 한 상태에서 쿼리 문자열을 구성 할 수 있어야합니다. NULL 캐릭터. SQLITE SQL 구문이 지원하는 표준 SQL과 함께이 작업을 수행하는 방법이 있습니까?

분명히 누군가가 전에 같은 상황에 직면했을 것입니다. 나는 인터넷 검색을 발견하고 몇 가지 답변을 찾았지만 다른 프로그래밍 언어 (Python)에있었습니다.

피드백에 미리 감사드립니다.

도움이 되었습니까?

해결책

You'll want to use this function with a prepared statement.

int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));

In C/C++, the standard way of dealing with NULLs in strings is to either store the beginning of the string and a length, or store a pointer to the beginning of a string and one to the end of the string.

다른 팁

Thank you all again for your feedback. This time I'm reporting how I solved the problem with the help of the indications provided here. Hopefully this will help others in the future.

As suggested by the first three posters, I did use prepared statements — additionally because I was also interested in getting the columns' data types, and a simple sqlite3_get_table() wouldn't do.

After preparing the SQL statement in the form of the following constant string:

INSERT INTO table VALUES(?,?,?,?);

it remains the binding of the corresponding values. This is done by issuing as many sqlite3_bind_blob() calls as the columns. (I also resorted to sqlite3_bind_text() for other "simple" data types because the API I'm working on can translate integers/doubles/etc into a string). So:

void* blobvalue[4];
int blobsize[4];
char *tail, *sql="INSERT INTO table VALUES(?,?,?,?)";
sqlite3_stmt *stmt=0;
sqlite3 *db;
/* ... */
db=sqlite3_open("sqlite.db");
sqlite3_prepare_v2(db, 
                   sql, strlen(sql)+1, 
                   &stmt, &tail);
for(int i=0; i<4; i++)
    sqlite3_ bind_ blob(stmt, 
                        i+1, blobvalue[i], blobsize[i], 
                        SQLITE_TRANSIENT);
if(sqlite3_step(stmt)!=SQLITE_DONE) 
    printf("Error message: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
sqlite3_close(db);

Note also that some functions (sqlite3_open_v2(), sqlite3_prepare_v2()) appear on the later SQLite versions (I suppose 3.5.x and later).

You want to precompile the statement sqlite_prepare_v2(), and then bind the blob in using sqlite3_bind_blob(). Note that the statement you bind in will be INSERT INTO table VALUES (?).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top