I don't usually do other people's homework but
it's Sunday night so why not be generous?

At the terminal command line type:

sqlite3 motorcycles.db

At the resulting sqlite3 command line type:

create table motorcycles (
  model       text;
  description text
);

insert into motorcycles
 values ('FLSTCI', 'That is a Heritage Softail Classic');

insert into motorcycles
 values ('WXYZ', 'That is NOT a Heritage Softail Classic');

.quit

In your text editor create a C main function
that will call a function like this one after
getting input from user.

#include <string.h>
#include "sqlite3.h"

int
getMotorCycleDescription (char     *Model,
                          char     *Description)

{
  int             Result = 0;
  char           *DbFileName;
  char           *SqlText;
  sqlite3        *hDB;
  sqlite3_stmt   *hStmt;


  DbFileName = "motorcycles.db";

  Result = sqlite3_open_v2(DbFileName, &hDB,
                            SQLITE_OPEN_READWRITE, NULL);

  // Configure database activity features
  // relevant to bigger applications.
  if (Result == SQLITE_OK) {
      // force referential integrity
    Result = sqlite3_exec(hDB, "PRAGMA foreign_keys = 1",
                           NULL, NULL, NULL);
      // allow delays for concurrent access
    sqlite3_busy_timeout(hDB, 500);
  }

  if (Result == SQLITE_OK) {
    // Use prepare, bind and step rather than exec
    // to avoid sql injection attacks.
    SqlText = "select description from motorcycle "
              " where model like :model";
  
    Result = sqlite3_prepare_v2(hDB, SqlText, strlen(SqlText),
                                &hStmt, NULL);
  }

  if (Result == SQLITE_OK) {
    Result = sqlite3_bind_text(hStmt, 1, Model, -1, SQLITE_STATIC);

    if (Result == SQLITE_OK) {

      Result = sqlite3_step(hStmt);

      if (Result == SQLITE_ROW) {
        strcpy(Description, (char *) sqlite3_column_text(hStmt, 0));

        Result = SQLITE_OK;
      }
    }
    sqlite3_finalize(hStmt);
  }
  sqlite3_close(hDB);

  return Result;
} // getMotorCycleDescription


Add suitable display routine.

Compile, link and test.

Ypu might want to use the strlcpy and strlcat functions
in place of strcpy and strcat to reduce the risk of buffer
overflow errors.


On Sun, 2012-01-29 at 14:26 -0800, Bruce Steele wrote:
> I’m a very new “C” programmer working on a program to look up information 
> from a SQLite created data base.
> The data base is made up of information on models of Harley Davidson 
> motorcycles. I want to do a search of the data base using input from the 
> “user”. For instance the user wants to know what model of motorcycle is a 
> “FLSTCI”. They would type that in and my “C” program will then access the 
> data base and return “That is a Heritage Softail Classic”.
> 
> My problem is can’t see a way to pass the user input to my select statement.
> Can this be done?
> Is it SQL or C or a combination?
> Or I going about this all wrong?
> 
> Like I said I’m very new and just trying to learn. Thanks for your help. Just 
> point me in the right direction and I’ll figure it out, Thanks again.
> _______________________________________________
> sqlite-users mailing list
> sqlite-users@sqlite.org
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to