Thank you very much for the response. Unless I'm doing something foolish (always a possibility) that doesn't seem to work so... here's the whole test program.
#include "stdafx.h" #include "sqlite3.h" #include "stdio.h" #include "string.h" #include "string" #include "iostream" using namespace std; int _tmain(int argc, _TCHAR* argv[]) //default project main { int rc, i, ncols; sqlite3 *db; sqlite3_stmt *stmt; //char *sql; //replaced by string sql; const char *tail; rc = sqlite3_open("foods.db", &db); if(rc) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return(1); } //====== my additions to get thie definitive guide example working ============ //sql = "create table episodes (id integer primary key, season int, name text);"; ////rc = sqlite3_prepare(db, sql, strlen(sql), &stmt, &tail); ////replaced by //rc = sqlite3_prepare(db, sql.c_str(), strlen( sql.c_str() ), &stmt, &tail); //rc = sqlite3_step(stmt); // //sql = "insert into episodes(id, season, name) Values(1,2,'bill');"; ////rc = sqlite3_prepare(db, sql, strlen(sql), &stmt, &tail); ////replaced by //rc = sqlite3_prepare(db, sql.c_str(), strlen( sql.c_str() ), &stmt, &tail); //rc = sqlite3_step(stmt); // //sql = "insert into episodes(id, season, name) Values(2,3,'bob')"; ////rc = sqlite3_prepare(db, sql, strlen(sql), &stmt, &tail); ////replaced by //rc = sqlite3_prepare(db, sql.c_str(), strlen( sql.c_str() ), &stmt, &tail); //rc = sqlite3_step(stmt); sql = "BEGIN;"; //you need to add newline here sql += "create table episodes (id integer primary key, season int, name text);"; sql += "insert into episodes(id, season, name) Values(1,2,'bill');"; sql += "insert into episodes(id, season, name) Values(2,3,'bob');"; sql += "COMMIT;"; rc = sqlite3_prepare(db, sql.c_str(), strlen( sql.c_str() ), &stmt, &tail); rc = sqlite3_step(stmt); //============================================================================ sql = "select * from episodes;"; // rc = sqlite3_prepare(db, sql, strlen(sql), &stmt, &tail); //replaced by rc = sqlite3_prepare(db, sql.c_str(), strlen(sql.c_str()), &stmt, &tail); if(rc != SQLITE_OK) { fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); } rc = sqlite3_step(stmt); ncols = sqlite3_column_count(stmt); while(rc == SQLITE_ROW) { for(i=0; i < ncols; i++) { fprintf(stderr, "'%s' ", sqlite3_column_text(stmt, i)); } fprintf(stderr, "\n"); rc = sqlite3_step(stmt); } sqlite3_finalize(stmt); sqlite3_close(db); //if( remove( "foods.db" ) != 0 ) perror( "Error deleting file" ); else puts( "File successfully deleted" ); getchar(); return 0; } On 21 June 2011 12:32, Black, Michael (IS) <michael.bla...@ngc.com> wrote: > I believe this will work if you put the SQL-required semi-colons at the end > of your statements. > > > > sql = "BEGIN;"; //you need to add newline here > sql += "create table episodes (id integer primary key, season int, name > text);"; > sql += "insert into episodes(id, season, name) Values(1,2,'bill');"; > sql += "insert into episodes(id, season, name) Values(2,3,'bob');"; > sql += "COMMIT;"; > > I would say most people don't do this as any error returns won't tell you > much. You'd normally prepare each seperately. > > > > > > Michael D. Black > > Senior Scientist > > NG Information Systems > > Advanced Analytics Directorate > > > > ________________________________ > From: sqlite-users-boun...@sqlite.org [sqlite-users-boun...@sqlite.org] on > behalf of e-mail mgbg25171 [mgbg25...@blueyonder.co.uk] > Sent: Tuesday, June 21, 2011 6:22 AM > To: sqlite-users@sqlite.org > Subject: EXT :[sqlite] Howto...multi-sqlite command string through > sqlite3_prepare_v2() to create SINGLE statement > > The commented out lines work. > I'm wondering... > a) is it possible to do what's not commented out > b) what's the syntax re the "sql =..." and "sql +=..." lines > Any help much appreciated! > [code] > //sql = "create table episodes (id integer primary key, season int, name > text);"; > ////rc = sqlite3_prepare(db, sql, strlen(sql), &stmt, &tail); > ////replaced by > //rc = sqlite3_prepare(db, sql.c_str(), strlen( sql.c_str() ), &stmt, > &tail); > //rc = sqlite3_step(stmt); > // > //sql = "insert into episodes(id, season, name) Values(1,2,'bill');"; > ////rc = sqlite3_prepare(db, sql, strlen(sql), &stmt, &tail); > ////replaced by > //rc = sqlite3_prepare(db, sql.c_str(), strlen( sql.c_str() ), &stmt, > &tail); > //rc = sqlite3_step(stmt); > // > //sql = "insert into episodes(id, season, name) Values(2,3,'bob')"; > ////rc = sqlite3_prepare(db, sql, strlen(sql), &stmt, &tail); > ////replaced by > //rc = sqlite3_prepare(db, sql.c_str(), strlen( sql.c_str() ), &stmt, > &tail); > //rc = sqlite3_step(stmt); > > sql = "BEGIN"; //you need to add newline here > sql += "create table episodes (id integer primary key, season int, name > text)"; > sql += "insert into episodes(id, season, name) Values(1,2,'bill')"; > sql += "insert into episodes(id, season, name) Values(2,3,'bob')"; > sql += "COMMIT"; > rc = sqlite3_prepare(db, sql.c_str(), strlen( sql.c_str() ), &stmt, &tail); > rc = sqlite3_step(stmt); > [code] > _______________________________________________ > 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 > _______________________________________________ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users