SQLite3的API应用(续)

使用sqlite3_prepare和sqlite3_step完成查询操作:

    sqlite3 *sqlite3db = NULL;    
    int rc,nCol;
    char *sql;
    sqlite3_stmt *pStmt;
    const char *pTail;
    sql="select * from Phonetable";
    rc=sqlite3_prepare(sqlite3db,sql,(int)strlen(sql),&pStmt,&pTail);
    if(rc!=SQLITE_OK)
    {
        fprintf(stderr,"SQLerror:%s\n",sqlite3_errmsg(sqlite3db));
    }
    rc=sqlite3_step(pStmt);
    nCol=sqlite3_column_count(pStmt);
    while(rc==SQLITE_ROW)
    {
        for(int i=0;i<nCol;i++)
            fprintf(stderr,"%s  ",sqlite3_column_text(pStmt,i));
        fprintf(stderr,"\n");
        rc=sqlite3_step(pStmt);
    }
    sqlite3_finalize(pStmt);
    sqlite3_close(sqlite3db);

使用sqlite3_get_table完成查询操作:
int rc;
char *sql;
char **result;
int nRows,nCols;
int nRIndex,nCIndex;
sql="SELECT *FROM PHONETABLE";
rc=sqlite3_get_table(sqlite3db,sql,&result,&nRows,&nCols,&errmsg);
if(rc==SQLITE_OK)
{
    for(nRIndex=0;nRIndex<nRows;nRIndex++)
    {
        for(nCIndex=0;nCIndex<nCols;nCIndex++)
            printf("%s = %s\n",result[nCIndex],result[(nRIndex+1)*nCols+nCIndex]);
    }
}
sqlite3_free_table(result);//释放资源

你可能感兴趣的:(SQLite3的API应用(续))