Discussion:
How to use sqlite3_exec() to execute SQL command with Unicode text?
bigboss97
2009-09-29 14:07:54 UTC
Permalink
My program is using sqlite3_exec() to run SQL command which has the type of
char*. Now I want to allow unicode in my DB. What do I have to change?
Obviously, sqlite3_exec() won't work any more.

Phuoc

-----
........................
www.folksfun.com
........................
--
View this message in context: http://www.nabble.com/How-to-use-sqlite3_exec%28%29-to-execute-SQL-command-with-Unicode-text--tp25663732p25663732.html
Sent from the SQLite mailing list archive at Nabble.com.
Kees Nuyt
2009-09-29 14:48:52 UTC
Permalink
On Tue, 29 Sep 2009 07:07:54 -0700 (PDT), bigboss97
Post by bigboss97
My program is using sqlite3_exec() to run SQL command which has the type of
char*. Now I want to allow unicode in my DB. What do I have to change?
Obviously, sqlite3_exec() won't work any more.
Phuoc
This page tells it all in a nutshell:
http://www.sqlite.org/c3ref/stmt.html
--
( Kees Nuyt
)
c[_]
bigboss97
2009-10-03 12:14:35 UTC
Permalink
Thank you for the link.
Then I started solving the callback problem. After a bit research, I found
out that I could end up with a new exec() function. So I took the original
exec() and modified it. It's working, no guaratee :-D

int sqlite3_exec16(
sqlite3 *db, /* The database on which the SQL executes */
const short *zSql, /* The SQL(16) to be executed */
sqlite3_callback16 xCallback, /* Invoke this callback routine */
void *pArg, /* First argument to xCallback() */
short **pzErrMsg /* Write error messages here */
){
int rc = SQLITE_OK;
const short *zLeftover;
sqlite3_stmt *pStmt = 0;
short **azCols = 0;

int nRetry = 0;
int nCallback;

if( zSql==0 ) return SQLITE_OK;
while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0]
){
int nCol;
short **azVals = 0;

pStmt = 0;
rc = sqlite3_prepare16_v2(db, zSql, -1, &pStmt, &zLeftover);
assert( rc==SQLITE_OK || pStmt==0 );
if( rc!=SQLITE_OK ){
continue;
}
if( !pStmt ){
/* this happens for a comment or white-space */
zSql = zLeftover;
continue;
}

nCallback = 0;

nCol = sqlite3_column_count(pStmt);
azCols = sqliteMalloc(2*nCol*sizeof(const short *) + 1);
if( azCols==0 ){
goto exec_out;
}

while( 1 ){
int i;
rc = sqlite3_step(pStmt);

/* Invoke the callback function if required */
if( xCallback && (SQLITE_ROW==rc ||
(SQLITE_DONE==rc && !nCallback && db->flags&SQLITE_NullCallback))
){
if( 0==nCallback ){
for(i=0; i<nCol; i++){
azCols[i] = (short *)sqlite3_column_name16(pStmt, i);
}
nCallback++;
}
if( rc==SQLITE_ROW ){
azVals = &azCols[nCol];
for(i=0; i<nCol; i++){
azVals[i] = (short *)sqlite3_column_text16(pStmt, i);
}
}
if( xCallback(pArg, nCol, azVals, azCols) ){
rc = SQLITE_ABORT;
goto exec_out;
}
}

if( rc!=SQLITE_ROW ){
rc = sqlite3_finalize(pStmt);
pStmt = 0;
if( rc!=SQLITE_SCHEMA ){
nRetry = 0;
zSql = zLeftover;
while( iswspace((unsigned char)zSql[0]) ) zSql++;
}
break;
}
}

sqliteFree(azCols);
azCols = 0;
}

exec_out:
if( pStmt ) sqlite3_finalize(pStmt);
if( azCols ) sqliteFree(azCols);

rc = sqlite3ApiExit(0, rc);
if( rc!=SQLITE_OK && rc==sqlite3_errcode(db) && pzErrMsg ){
*pzErrMsg = sqlite3_malloc(1+strlen(sqlite3_errmsg16(db)));
if( *pzErrMsg ){
strcpy(*pzErrMsg, sqlite3_errmsg16(db));
}
}else if( pzErrMsg ){
*pzErrMsg = 0;
}

assert( (rc&db->errMask)==rc );
return rc;
}
Post by Kees Nuyt
http://www.sqlite.org/c3ref/stmt.html
--
( Kees Nuyt
-----
........................
www.folksfun.com
........................
--
View this message in context: http://www.nabble.com/How-to-use-sqlite3_exec%28%29-to-execute-SQL-command-with-Unicode-text--tp25663732p25728190.html
Sent from the SQLite mailing list archive at Nabble.com.
Loading...