You're going to get questions like "why do you want to do this" so you may as well tell us now.
The usual way to do his is to execute the sql yourself using statement prepares and step. It gives you a lot more control over error messages. Why don't you want to do it this way? The 2nd way commonly done is to simply call sqlite3.exe as a system call or a pipe. Less control but easy to understand. Trying to use the ".read" function by linking it in seems like a bad idea as you note. You could've already had the first wo methods done while trying to figure that one out. Here's the 2 methods in an example (please, anybody, feel free to criticize....no pride of authorship here at all). Change popen and pclose to _popen, _pclose for Windows. #include <stdio.h> #include <stdlib.h> #include <iostream> #include <fstream> #include <sstream> #include <sqlite3.h> using namespace std; void dosql(sqlite3 *db,const char *sql) { sqlite3_stmt *stmt; int rc=sqlite3_prepare(db,sql,-1,&stmt,0); if (rc != SQLITE_OK) { cerr << "sqlite3_prepare: " << sqlite3_errmsg(db) << endl; return; } rc=sqlite3_step(stmt); if (rc == SQLITE_ROW) { cerr << "multi row sql not implemented: " << sql << endl; sqlite3_finalize(stmt); return; } if (rc != SQLITE_DONE) { cerr << "sqlite3_step: " << sqlite3_errmsg(db) << endl; } sqlite3_finalize(stmt); } void readfile(char *database,char *sqlfile) { sqlite3 *db; int rc = sqlite3_open(database,&db); if (rc != SQLITE_OK) { cerr << sqlite3_errmsg(db) << endl; exit(1); } ifstream sql; sql.open(sqlfile); if (!sql.is_open()) { perror(sqlfile); exit(1); } string line; while(sql.good()) { getline(sql,line); if (!sql.eof()) { cerr << "X:" << line << endl; dosql(db,line.c_str()); } } sql.close(); sqlite3_close(db); } void sqlite3_readfile(char *database,char *sqlfile) { stringstream ss; ss << "sqlite3 " << database << " <" << sqlfile; FILE *fp=popen(ss.str().c_str(),"r"); if (fp == NULL) { perror("sqlite3"); exit(1); } char buf[65536]; while(fgets(buf,sizeof(buf),fp)) { cout << buf; } pclose(fp); } int main(int argc, char *argv[]) { if (argc != 3) { cerr << "USage: " << argv[0] << " database filename" << endl; exit(1); } #if 0 sqlite3_readfile(argv[1],argv[2]); #else readfile(argv[1],argv[2]); #endif return 0; } Michael D. Black Senior Scientist Advanced Analytics Directorate Advanced GEOINT Solutions Operating Unit Northrop Grumman Information Systems From: sqlite-users-boun...@sqlite.org [sqlite-users-boun...@sqlite.org] on behalf of YAN HONG YE [yanhong...@mpsa.com] Sent: Tuesday, July 24, 2012 5:16 AM To: sqlite-users@sqlite.org Subject: EXT :[sqlite] read sql script file in the shell.c source file ,have a function .read file, and I wanna use it to my c++ code, when I hava a sql script file,such as : create table test (id integer primary key, value text); insert into test (id, value) values(1, 'eenie'); insert into test (id, value) values(2, 'meenie'); insert into test (value) values('miny'); insert into test (value) values('mo'); now I wanna use the shell.c function to run the script, but I don't know how to use c++ code to achieve the target. I only found on line in shell.c: ".read FILENAME Execute SQL in FILENAME\n" in "static char zHelp[]" I suggest it use callback. static int _is_complete(char *zSql, int nSql){ int rc; if( zSql==0 ) return 1; zSql[nSql] = ';'; zSql[nSql+1] = 0; rc = sqlite3_complete(zSql); zSql[nSql] = 0; return rc; } _______________________________________________ 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