After creating a database, I, now, would like to create a table called
'friend', but for some reason 'sqlite3_column()' reports zero.
What elese do I have to do to have the table created, this is what I have so
far done.
int main() {
sqlite3 *db; // Data Base
sqlite3_stmt* stmt;
int rc; // return code
// SQL statement
std::string create_table("CREATE TABLE friend (name TEXT, address TEXT, age
INT)");
// 1.) Open database
rc = sqlite3_open_v2(dbName.c_str(),
&db,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
NULL);
if(rc != SQLITE_OK) {
sqlite3_close(db);
std::cout << "error open_v2: " << rc << std::endl;
exit(-1);
} else {
std::cout << "Welcome to a whole new world!" << std::endl;
std::cin.get();
}
// 2.) Convert SQL text into a prepared statement and receive a valid
validated statement
rc = sqlite3_prepare_v2(
db, /* Database handle */
create_table.c_str() , /* SQL statement, UTF-8 encoded */
create_table.length(), /* Maximum length of zSql in bytes. */
&stmt, /* OUT: Statement handle */
NULL /* OUT: Pointer to unused portion of zSql */
);
if(rc != SQLITE_OK) {
sqlite3_close(db);
std::cout << "error prepare_v2: " << rc << std::endl;
exit(-2);
}
//3.) Evaluate the SQL stament passed to sqlite3_prepare_v2()
rc = sqlite3_step(stmt);
if(rc != SQLITE_DONE) {
sqlite3_close(db);
std::cout << "error sqlite3_step: " << rc << std::endl;
exit(-3);
}
//3.1
rc = sqlite3_column_count(stmt);
std::cout << "Number of colums: " << rc << std::endl;
sqlite3_finalize(stmt);
sqlite3_close(db);
std::cout << "Good bye!" << std::endl;
return 0;
}
The documentation does not say this in so many words, but:
You should place your call to sqlite3_column_count(...) after the
prepare and before any sqlite3_step(...) which exhaust the result set.
The documentation for the sqlite3_column_count function says "This
routine returns 0 if pStmt is an SQL statement that does not return
data". If this includes a pStmt for which data will no longer be
returned, (as appears to be the case from the OP's report), this more
stringent requirement should be mentioned.
--
Larry Brasfield
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users