[Cppcms-users] RE : CppDB import from file
Brought to you by:
artyom-beilis
|
From: SIMON A. <All...@su...> - 2013-01-07 15:49:29
|
Actually in cppcms I would like to make a small website that permits me to do more or less like phpmyadmin, i.e controlling my sqlite3 database from a web interface. So I may receive some string containing several SQL request.
For the ; outside it's tricky because you need to know when you're outside (using regex?)
About the prepared statement, I was going to talk about it, because I've tried to do a simple "split" function like this
int SqliteModel::import_sql_file(
const std::string &sqlFilePath
) {
try {
std::ifstream f(sqlFilePath.c_str());
std::string fileStr(
(std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>()
);
sqliteDb << fileStr << cppdb::exec;
size_t current = 0;
size_t next = -1;
do
{
current = next + 1;
next = fileStr.find_first_of( ";", current );
std::string tmpRequest = fileStr.substr( current, next - current );
sqliteDb << tmpRequest << cppdb::exec;
} while (next != std::string::npos);
} catch(std::exception const &e) {
BOOSTER_ERROR("cppcms") << e.what();
return 1;
}
return 0;
}
and it will segfault (no exception, a old goold segfault) when I try to execute a string containing nothing or a comment or a set of space with nothing less
the backtrace generates
#0 0xb61eee96 in sqlite3_clear_bindings () from /usr/lib/i386-linux-gnu/libsqlite3.so.0
#1 0xb7c6433a in cppdb::sqlite3_backend::statement::reset (this=0x80710c8)
at /home/allan/Desktop/projet/cppdb-0.3.0/drivers/sqlite3_backend.cpp:226
#2 0xb7d81b95 in cppdb::backend::statements_cache::put (this=0x805d37c, p_in=0x80710c8)
at /home/allan/Desktop/projet/cppdb-0.3.0/src/backend.cpp:146
#3 0xb7d81d43 in cppdb::backend::statement::dispose (p=0x80710c8) at /home/allan/Desktop/projet/cppdb-0.3.0/src/backend.cpp:54
#4 0xb7d8bbdb in reset (v=0x0, this=0xbffff43c) at /home/allan/Desktop/projet/cppdb-0.3.0/./cppdb/ref_ptr.h:109
#5 cppdb::statement::~statement (this=0xbffff438, __in_chrg=<optimized out>)
at /home/allan/Desktop/projet/cppdb-0.3.0/src/frontend.cpp:218
#6 0x080536fb in models::SqliteModel::import_sql_file(std::string const&) ()
#7 0x0804f6ec in main ()
which is in line 226 of sqlite3_backend of cppdb
sqlite3_clear_bindings(st_);
st_ being a NULL pointer (0x0)
Regards,
Allan SIMON
Tatoeba Administrator (Sysko)
________________________________
De : Artyom Beilis [art...@ya...]
Date d'envoi : lundi 7 janvier 2013 15:11
À : cpp...@li...
Objet : Re: [Cppcms-users] CppDB import from file
Why wouldn't you run the sqlite executable as is?
Most of SQL clients would do SQL import much better job.
Also note that you can quite easily use ';' as separator. Sqlite3 does not support escape sequences but rather use ANSI SQL
double quote 'can''t' == can't
So if you split the file according to ";" outside the quotes you will be fine.
Also note: don't use prepared statements (or use prepared_uncached ones)...
Artyom Beilis
--------------
CppCMS - C++ Web Framework: http://cppcms.com/
CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/
________________________________
From: SIMON Allan <All...@su...>
To: "cpp...@li..." <cpp...@li...>
Sent: Monday, January 7, 2013 8:55 AM
Subject: [Cppcms-users] CppDB import from file
Hello
I'm currently using CppDB and would like to have a function that takes in parameter a .sql file and that import it into a sqlite database
I've tried to do it like this (see the code below) , but it seems that when CppDB does not like when you try to execute several Sqlrequest with only one cppdb::exec
Of course I can't read the file and split it by lines (some requests are on several lines) and also no by splitting on ";" as my SQL file contains some ; inside string values.
Does anyone already find a solution for this ?
int SqliteModel::import_sql_file(
const std::string &sqlFilePath
) {
try {
std::ifstream f(sqlFilePath.c_str());
std::string fileStr(
(std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>()
);
sqliteDb << fileStr << cppdb::exec;
} catch(std::exception const &e) {
BOOSTER_ERROR("cppcms") << e.what();
return 1;
}
return 0;
}
Regards,
Allan SIMON
Tatoeba.org<http://tatoeba.org/> Administrator (Sysko)
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122412
_______________________________________________
Cppcms-users mailing list
Cpp...@li...<mailto:Cpp...@li...>
https://lists.sourceforge.net/lists/listinfo/cppcms-users
|