4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
16 * Main file for the SQLite library. The routines in this file implement the
17 * programmer interface to the library. Routines in other files are for
18 * internal use by SQLite and should not be accessed by users of the library.
23 #include "sqliteInt.h"
28 * Execute SQL code. Return one of the SQLITE_ success/failure codes. Also
29 * write an error message into memory obtained from malloc() and make *pzErrMsg
30 * point to that message.
32 * If the SQL is a query, then for each row in the query result the xCallback()
33 * function is called. pArg becomes the first argument to xCallback(). If
34 * xCallback=NULL then no callback is invoked, even for queries.
37 sqlite3
*db
, /* The database on which the SQL executes */
38 const char *zSql
, /* The SQL to be executed */
39 sqlite3_callback xCallback
, /* Invoke this callback routine */
40 void *pArg
, /* First argument to xCallback() */
41 char **pzErrMsg
/* Write error messages here */
45 const char *zLeftover
;
46 sqlite3_stmt
*pStmt
= 0;
53 if( zSql
==0 ) return SQLITE_OK
;
55 while( (rc
==SQLITE_OK
|| (rc
==SQLITE_SCHEMA
&& (++nRetry
)<2)) && zSql
[0] )
61 rc
= sqlite3_prepare(db
, zSql
, -1, &pStmt
, &zLeftover
);
65 sqlite3_finalize(pStmt
);
70 /* this happens for a comment or white-space */
75 db
->nChange
+= nChange
;
78 nCol
= sqlite3_column_count(pStmt
);
79 azCols
= sqliteMalloc(2*nCol
*sizeof(const char *));
89 rc
= sqlite3_step(pStmt
);
91 /* Invoke the callback function if required */
92 if( xCallback
&& (SQLITE_ROW
==rc
||
93 (SQLITE_DONE
==rc
&& !nCallback
&&
94 db
->flags
&SQLITE_NullCallback
)) )
100 azCols
[i
] = (char *)sqlite3_column_name(pStmt
, i
);
106 azVals
= &azCols
[nCol
];
107 for(i
=0; i
<nCol
; i
++)
109 azVals
[i
] = (char *)sqlite3_column_text(pStmt
, i
);
112 if( xCallback(pArg
, nCol
, azVals
, azCols
) )
121 rc
= sqlite3_finalize(pStmt
);
124 nChange
= db
->nChange
;
126 if( rc
!=SQLITE_SCHEMA
)
130 while( isspace((unsigned char)zSql
[0]) ) zSql
++;
141 if( pStmt
) sqlite3_finalize(pStmt
);
142 if( azCols
) sqliteFree(azCols
);
143 if( sqlite3_malloc_failed
)
145 if( rc
!=SQLITE_OK
&& rc
==sqlite3_errcode(db
) && pzErrMsg
)
147 *pzErrMsg
= malloc(1+strlen(sqlite3_errmsg(db
)));
150 strcpy(*pzErrMsg
, sqlite3_errmsg(db
));