Thank you Michael for your prompt response.
I have been able to duplicate the error message. I think this could be a bug
in SQLite3.
If it is a bug, how do I go about reporting it?
//== my_sqlite3.hpp ===
#ifndef JME_MY_SQLITE3_HPP
#define JME_MY_SQLITE3_HPP
#include <iostream>
#include"sqlite3.h"
namespace jme {
class mySQLite3 {
private:
sqlite3 *db; // Data Base
sqlite3_stmt* stmt; // SQL statement
bool rc; // SQL return code
std::string databese_name; // The name of the database
std::string sql_param_tblName; // Databese table Name parameters
std::string stmtName; // SQL statement name
std::string apstr; // All Purpose String
int apint; // All Purpose Integer
std::string sqlite3_version;
private:
//! Initalization method, instead of using the 'tor initialize
//! all varialbles and set all necessary parameters, we use an Init
void Init();
public:
//Constructors
mySQLite3();
mySQLite3(const std::string&);
//Destructor
~mySQLite3();
//! Pass to this method the name of the database to be openned
void createDatabase(const std::string&)throw(std::exception) ;
//! Pass to this method the SQL statement for the table creation
void createTable(const std::string& )throw(std::exception) ;
};
}
#endif
=== my_sqlite3.cpp===
#ifndef JME_MY_SQLITE3_HPP
#include "my_sqlite3.hpp"
#endif
jme::mySQLite3::mySQLite3(const std::string& s) {
this->Init();
this->databese_name = s;
this->createDatabase(databese_name);
}
jme::mySQLite3::mySQLite3() {
this->Init();
}
void jme::mySQLite3::Init() {
databese_name = sql_param_tblName = stmtName = ".";
sqlite3_version = "SQLite version: ";
sqlite3_version += sqlite3_libversion();
}
void jme::mySQLite3::createTable(const std::string& s) throw
(std::exception) {
rc = sqlite3_prepare_v2( db, s.c_str(), -1, &stmt, NULL);
if(rc != SQLITE_OK) {
sqlite3_close(db);
sqlite3_finalize(stmt);
std::string error("Error prepare_v2: ");
error += sqlite3_errmsg(db);
std::cout << "Error: " << rc << " " << error << std::endl;
}
rc = sqlite3_step(stmt);
std::cout << "Error: " << rc << std::endl;
if(rc != SQLITE_DONE) {
sqlite3_close(db);
sqlite3_finalize(stmt);
std::string error("error sqlite3_step: ");
error += sqlite3_errmsg(db);
std::cout << error << std::endl;
}
sqlite3_finalize(stmt);
}
void jme::mySQLite3::createDatabase(const std::string&
s)throw(std::exception) {
rc = sqlite3_open_v2(s.c_str(),
&db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
NULL);
if(rc != SQLITE_OK) {
sqlite3_close(db);
apstr = "Error opening database: ";
apstr += databese_name;
apstr += "\nError value: ";
apstr += sqlite3_errmsg(db);
std::cout << apstr << std::endl;
}
}
jme::mySQLite3::~mySQLite3() {
sqlite3_close(db);
}
=== main.cpp ==
//STL
#include<iostream>
//JME
#include"my_sqlite3.hpp"
int main() {
std::string s1("CREATE TABLE name(n_id INTEGER PRIMARY KEY, title TEXT,
fname TEXT, mname TEXT, lname TEXT)");
std::string s2("INSERT INTO name (n_id, title, fname, mname, lname)
VALUES (?, ?, ?, ?, ?)");
jme::mySQLite3 myDB("database.db3");
myDB.createTable(s1);
std::cin.get();
return 0;
}
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Black, Michael (IS)
Sent: Monday, July 23, 2012 3:53 PM
To: General Discussion of SQLite Database
Subject: Re: [sqlite] C++ - Creating Table
Just as a sanity check your code does work OK. I made it a standalone
program.
#include <iostream>
#include "sqlite3.h"
using namespace std;
class mySQLite3Class {
private:
//SQLite3
sqlite3* db; //SQLite3
string dbName; // Database name
string apstr; // All Purpose String
string sql_param_tblName; // Databese table Name parameters
string stmtName; // SQL statement name
public:
void createDB();
void create_tblName();
void createDatabase(const string& s);
void createTable(const string& s);
mySQLite3Class(const string& s) {
createDatabase(s);
}
};
void mySQLite3Class::createDatabase(const string& s) {
int rc = sqlite3_open_v2(s.c_str(),
&db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
NULL);
if(rc != SQLITE_OK) {
std::cout << rc << std::endl;
}
}
void mySQLite3Class::createTable(const string& s) {
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, s.c_str(), s.length(), &stmt, NULL );
if(rc != SQLITE_OK) {
std::cout << rc << std::endl;// error = 1
std::cout << sqlite3_errmsg(db)
<< std::endl; // er-msg = library routine called out of
sequence
}
rc = sqlite3_step(stmt);
if(rc != SQLITE_DONE) {
std::cout << rc << endl;
}
sqlite3_finalize(stmt);
}
int main(int argc,char *argv[])
{
string dbName = "001Database.sql";
string sql_param_tblName = "CREATE TABLE name(n_id INTEGER PRIMARY KEY,
title TEXT, fname TEXT, mname TEXT, lname TEXT)";
mySQLite3Class *myDB = new mySQLite3Class(dbName);
myDB->createTable(sql_param_tblName);
return 0;
}
Michael D. Black
Senior Scientist
Advanced Analytics Directorate
Advanced GEOINT Solutions Operating Unit Northrop Grumman Information
Systems
From: [email protected] [[email protected]] on
behalf of Arbol One [[email protected]]
Sent: Monday, July 23, 2012 1:54 PM
To: SqLite
Subject: EXT :[sqlite] C++ - Creating Table
Using SQLite version 3.7.8 amalgamation, under Win7 with MinGW, I compile
the bellow program, but for some strange reason I am getting a runtime error
when creating the table. I hope that one of you would be able to tell me
what I am doing wrong.
TIA
===
class mySQLite3Class {
private:
//SQLite3
sqlite3* db; //SQLite3
Glib::ustring dbName; // Database name
Glib::ustring apstr; // All Purpose String
Glib::ustring sql_param_tblName; // Databese table Name parameters
Glib::ustring stmtName; // SQL statement name
public:
void createDB();
void create_tblName();
mySQLite3Class(const Glib::ustring& s){ createDatabase(s);}
};
void mySQLite3Class::createDatabase(const Glib::ustring& s) {
rc = sqlite3_open_v2(s.c_str(),
&db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
NULL);
if(rc != SQLITE_OK) {
std::cout << rc << std::endl;
}
}
void mySQLite3Class::createTable(const Glib::ustring& s){
rc = sqlite3_prepare_v2(db, s.c_str(), s.length(), &stmt, NULL );
if(rc != SQLITE_OK) {
std::cout << rc << std::endl;// error = 1
std::cout << sqlite3_errmsg(db)
<< std::endl; // er-msg = library routine called out
of sequence
}
rc = sqlite3_step(stmt);
if(rc != SQLITE_DONE) {
std::cout << rc << stdl;
}
sqlite3_finalize(stmt);
}
myClass{
private:
mySQLite3Class* myDB;
Glib::ustring sql_param_tblName;
Glib::ustring dbName;
public:
myClass();
}
myClass::myClass(){
dbName = "001Database.sql";
sql_param_tblName = "CREATE TABLE name(n_id INTEGER PRIMARY KEY, title
TEXT, fname TEXT, mname TEXT, lname TEXT)";
myDB = new mySQLite3Class(dbName);
myDB->createTable(sql_param_tblName); ==> // problem
}
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users