If you have no variables in your SQL (like SELECT * FROM ...) then you don't bind.

If you have "SELECT * FROM junk WHERE name=?" then you need to bind like this. The bind is good until you do a reset.

char that_name = "Harry";
...
rc = sqlite3_bind_text(xek, 1, that_name, -1, SQLITE_STATIC);
if (rc == SQLITE_OK) return(FALSE);

/*Could return with an error, but have errcode OK.*/
err = sqlite3_errcode(db);
if (err != SQLITE_OK) {
errormet("908", (char *)sqlite3_errmsg(db), (char *)fnm); /*Fatal DB Error.*/
  return(TRUE);
}  /*if*/
return(FALSE);

Sqlite will see - SELECT * FROM junk WHERE name="Harry".

Mark Wyszomierski wrote:
I suppose this is correct:

strSql.Format(_T("SELECT * FROM test"));

char szSomething[500];
int nTest = sqlite3_bind_text(pStmt, 1, szSomething, 500, SQLITE_STATIC);
if (nTest != SQLITE_OK) {
TRACE("sqlite3_bind_text fails!! [%i] [%s]\n", nTest, sqlite3_errmsg(db));
}

but can I use the string "SELECT * FROM test", if not how do I use
those question marks or AAA variable identifiers in the string itself
to achieve binding?

Thanks,
Mark

On 8/14/06, Mark Wyszomierski <[EMAIL PROTECTED]> wrote:

John, Cory, thank you very much. I got execute plain statements ok by
modifying my earlier posting a bit. I was able to create a table using
the prepare statement.

Previously I was using sqlite3_exec() to execute my statements and I
could pass it a callback function which I could use to fetch data for
SELECT statements. How do we do that now with the prepare() and step()
statements? Just some pseudocode like before would be fine. Here in my
test table I have two dummy records and the while loop correctly
iterates over both of them - but how to get the values in each row?

   // Table test looks like:

   // something | something_else
   // ------------------------------------------
   //   hello       |   there
   //   bye        |   guy

   strSql.Format(_T("SELECT * FROM test"));

   sqlite3_stmt *pStmt;
   const char *pszTailPointer;
   int nRetVal = sqlite3_prepare(db, strSql, strSql.GetLength(),
&pStmt, &pszTailPointer);
   if (nRetVal != SQLITE_OK) {
       TRACE("prepare fails!! [%i] [%s]\n", nRetVal, sqlite3_errmsg(db));
       return false;
   }

   nRetVal = sqlite3_step(pStmt);
   while (nRetVal == SQLITE_BUSY || nRetVal == SQLITE_ROW) {
       Sleep(100);
       // Try again.
       nRetVal = sqlite3_step(pStmt);

       // How do I get the information out of this returned record?

       // By the way, why would we want a reset() in here?
     //  sqlite3_reset(pStmt);
   }
   switch (nRetVal) {
       case SQLITE_DONE:
           TRACE("Done ok\n");
           break;
       case SQLITE_ERROR:
           TRACE("ERROR!!!!\n");
           break;
       case SQLITE_MISUSE:
           TRACE("MISUSE!!!!\n");
           break;
       default:
           break;
   }
   sqlite3_finalize(pStmt);


Thanks,
Mark



On 8/14/06, John Stanton <[EMAIL PROTECTED]> wrote:
> Mark Wyszomierski wrote:
> > Hi Cory,
> >
> > Alright I gave it a shot from the docs but I'm not handling the
> > prepare statement correctly. I'm trying the ASCI version first. The
> > prepare statement returns an error. Here is the code snippet I'm
> > trying:
> >
> >
> > strSql.Format("CREATE TABLE test (something TEXT, something_else TEXT,
> > primary key(something))");
> >
> >    sqlite3_stmt *pStmt;
> >    const char *pszTailPointer;
> >    int nRetVal = sqlite3_prepare(db, strSql, strSql.GetLength(),
> > &pStmt, &pszTailPointer);
> >    while (nRetVal == SQLITE_BUSY || nRetVal == SQLITE_ROW) {
> >        Sleep(100);
> >        // Try again.
> >        nRetVal = sqlite3_step(pStmt);
> >        TRACE("ret val was [%i]\n", nRetVal);
> >    }
> >    switch (nRetVal) {
> >        case SQLITE_DONE:
> >            TRACE("Done ok\n");
> >            break;
> >        case SQLITE_ERROR:
> >            TRACE("ERROR!!!!\n");
> >            break;
> >        case SQLITE_MISUSE:
> >            TRACE("MISUSE!!!!\n");
> >            break;
> >        default:
> >            break;
> >    }
> >    sqlite3_finalize(pStmt);
> >    return true;
> >
> > Any hints?
> >
> > Thanks,
> > Mark
> >
> >
> > On 8/13/06, Cory Nelson <[EMAIL PROTECTED]> wrote:
> >
> >> On 8/13/06, Mark Wyszomierski <[EMAIL PROTECTED]> wrote:
> >> > Hi,
> >> >
> >> > I have been using sqlite on windows for a few months, it is great. I > >> > need to switch over to unicode support now though, and I am confused
> >> > how to do this with sqlite.
> >> >
> >> > 1) First, when I compiled the original sqlite project, I have the
> >> > character set to MBCS. Should I switch this to Unicode and recompile?
> >>
> >> I think SQLite explicitly calls CreateFileW but I could be wrong.
> >> Might as well compile as Unicode anyway.
> >>
> >> > 2) I have been using sqlite3_exec() to execute my sql statements, but > >> > I see that there is no sqlite3_exec16() equivalent to take a unicode > >> > string. I think I'm supposed to use sqlite3_prepare16() but I have no
> >> > idea what the last two parameters of that function are?
> >>
> >> Check the docs, they explain how to use prepared statements.
> >>
> >> > 3) To escape my sql statements I was using sqlite3_mprintf() - is
> >> > there a unicode equivalent?
> >>
> >> With prepared statements you put placeholders like "?" into your sql
> >> and bind data to the placeholders - no escaping required.
> >>
> >> > Thanks for any information,
> >> > Mark
> >> >
> >> >
> >> -----------------------------------------------------------------------------
> >>
> >> > To unsubscribe, send email to [EMAIL PROTECTED]
> >> >
> >> -----------------------------------------------------------------------------
> >>
> >> >
> >> >
> >>
> >>
> >> --
> >> Cory Nelson
> >> http://www.int64.org
> >>
> >> -----------------------------------------------------------------------------
> >>
> >> To unsubscribe, send email to [EMAIL PROTECTED]
> >> -----------------------------------------------------------------------------
> >>
> >>
> >>
> >
> > -----------------------------------------------------------------------------
> >
> > To unsubscribe, send email to [EMAIL PROTECTED]
> > -----------------------------------------------------------------------------
> >
> >
> A "prepare" just compiles the statment, and does not get busy.  You do
> it to get ready for your execution loop.
>
> You execute the compiled statement with "step" and then call "reset" to
> check for errors and intialize the compiled statement ready for the next
> "step".  At the end of your processing you "finalize" the statement to
> tidy up and let you close the database.
>
>    open
>    prepare
>    loop
>        step
>        reset
>    repeat
>    finalize
>    close
>
> -----------------------------------------------------------------------------
> To unsubscribe, send email to [EMAIL PROTECTED]
> -----------------------------------------------------------------------------
>
>


-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------



-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------

Reply via email to