Having Errors While Trying to Open a SQLite3 Database - c++

I'm working in Visual Studio Community 2017, and what I'm trying to do is open and read the information of a database in C++.
#include <stdio.h>
#include <string>
using std::string;
#include <sstream>
using std::stringstream;
#include "C:\Users\santiago.corso\Desktop\sqlite-amalgamation-3240000 (1)\sqlite-amalgamation-3240000\sqlite3.h"
bool find_employee(int _id)
{
bool found = false;
sqlite3* db;
sqlite3_stmt* stmt;
stringstream ss;
// create sql statement string
// if _id is not 0, search for id, otherwise print all IDs
if (_id) { ss << "select * from employees where id = " << _id << ";"; }
else { ss << "select * from employees;"; }
string sql(ss.str());
//the resulting sql statement
printf("sql: %s\n", sql.c_str());
//get link to database object
if (sqlite3_open("C:\ProgramData\PROISER\ISASPSUS\datastore\dsfile.db", &db) != SQLITE_OK) {
printf("ERROR: can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return found;
}
// compile sql statement to binary
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, NULL) != SQLITE_OK) {
printf("ERROR: while compiling sql: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
sqlite3_finalize(stmt);
return found;
}
// execute sql statement, and while there are rows returned, print ID
int ret_code = 0;
while ((ret_code = sqlite3_step(stmt)) == SQLITE_ROW) {
printf("TEST: ID = %d\n", sqlite3_column_int(stmt, 0));
found = true;
}
if (ret_code != SQLITE_DONE) {
//this error handling could be done better, but it works
printf("ERROR: while performing sql: %s\n", sqlite3_errmsg(db));
printf("ret_code = %d\n", ret_code);
}
printf("entry %s\n", found ? "found" : "not found");
//release resources
sqlite3_finalize(stmt);
sqlite3_close(db);
return found;
}
The errors that are being returned are from the category Compiler Error C4129.
I cant reach a solution. If you could help me I would appreciate it.

I could correct above mistake by puting "\\" in all the parts of the path in sqlite3_open. Beside that, another error poped up about a entry point that must be defined, refering to
if (sqlite3_open("C:\ProgramData\PROISER\ISASPSUS\datastore\dsfile.db", &db) != SQLITE_OK) {
printf("ERROR: can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return found;`
Also what i want to know is if with this piece of code is enough for opening and reading the database. Im starting with sqlite3 and what i want to do next is exporting its content to an excel file.
Someone recommend me using the exact app for the cause that is native to sqlite3 and is like a cmd.However, i would rather build a script for this purpose instead of using a cmd.

Related

sqlite_prepare_v2 gives error #26(File Not A Database) even when database is opened successfully

I am trying to use sqlite3_prepare_v2() in VC++ but it is giving me error #26 which is "File is not a database." which is confusing because I have opened database successfully.
sqlite3 *testDb;
sqlite3_initialize();
std::string location = uribase+"testDB.sqlite3";
auto rc = sqlite3_open_v2(location.c_str(), &testDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if (rc != SQLITE_OK)
{
logError(rc);
sqlite3_close(testDb);
sqlite3_shutdown();
return -1;
}
else
{
std::cout << "Database opened successfully.";
}
std::string tableName = "test";
std::string strstmt = "SELECT * FROM " + tableName + ";";
sqlite3_stmt *pstmt = NULL;
rc = sqlite3_prepare_v2(testDb, strstmt.c_str(), -1, &pstmt, NULL);
if (rc != SQLITE_OK)
{
logError(rc);
sqlite3_close(testDb);
sqlite3_shutdown();
return -1;
}
Output
SQLite will not access the file until it actually needs to. (And in any case, it would be possible for another application to corrupt the file after it has been opened.)
The problem is that this file indeed is not a database file (or encrypted).

Insert entry into SQLite3 table in a conditional C++ loop statement

I am using SQLite3 header files in my C++ program and trying to create a table and insert data onto it, it works fine on a regular input.
It shows error when I use it in a C++ loop with changing variables.
I am using the database to insert my reading from RS-232.
Here is my code:
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
std::string sql_str;
std::ostringstream temp;
std::string command;
/* Open database */
rc = sqlite3_open("test_1.db", &db);
if (rc){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}
else{
fprintf(stderr, "Opened database successfully\n");
}
std::string str;
std::ostringstream oss;
oss << id_count; // stornig the primary id int values into a string
str = "INSERT INTO M_DATA (ID, DETAILS) VALUES(";
str += oss.str(); //copying the int primary id
str += ", '";
std::string str_t1(szBuffer); //Copying character aray to a string
str += str_t1; //concatening the string
str += "');";
//printing what the database takes
//output_file << std::endl << str << std::endl;
char * writable = new char[str.size() + 1];
std::copy(str.begin(), str.end(), writable);
writable[str.size()] = '\0'; // don't forget the terminating 0
sql = writable;
output_file << std::endl << "## SQL COMMAND : " << sql << "#" << std::endl;
// don't forget to free the string after finished using it
delete[] writable;
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if (rc != SQLITE_OK){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
output_file << std::endl << "** SQL ERROR : " << zErrMsg << "*" << std::endl;
sqlite3_free(zErrMsg);
}
else{
fprintf(stdout, "Records created successfully\n");
}
// _sleep(3000);
sqlite3_close(db);
My issue is I have a szBuffer which changes everytime, and I have to insert it as a new entry into the table.
Is there a way to increment the Primary Key and store my string into it?
The sz buffer at a single line will give data like: For Ex:
szBuffer : ersion = 1 [SPI]: MinorVersion = 2 [SPI]: Real Time
= 1434260351 [SPI]: SR # = SBB-ST1000090
The SQL command in the string I pass is like this:
SQL COMMAND : INSERT INTO M_DATA (ID, DETAILS) VALUES(9,
'ersion = 1 [SPI]: MinorVersion = 2 [SPI]: Real Time = 1434260351
[SPI]: SR # = SBB-ST1000090');
The Error which I get is like:
SQL ERROR : near "¸”_": syntax error
I am not sure if I am doing this right or wrong.
Can we use the insert statement in a loop? Am I passing the string the right way? (It looks correct to me when I print it out.)
But why do I get an error?
Is there any better way to enter my data?
I am very new to this so I tried search the internet, but no one is doing it the way I did it.
Please help.
Many Thanks.
(Almost) never build a SQL statement via string concatenation. Use a prepared statement and bind the parameter values.
// Prepare the statement
sqlite3_stmt* stmt;
int result = sqlite3_prepare_v2(db, "INSERT INTO M_DATA (ID, DETAILS) VALUES(?, ?);", -1, &stmt, nullptr);
// TODO: Handle when result != SQLITE_OK
while(/* whatever you wanted to loop on */)
{
// Bind in the parameter values
result = sqlite3_bind_int(stmt, 1, id_count);
// TODO: Handle when result != SQLITE_OK
result = sqlite3_bind_text(stmt, 2, szBuffer, -1, SQLITE_STATIC);
// TODO: Handle when result != SQLITE_OK
// Invoke the statement
result = sqlite3_step(stmt);
// TODO: Handle when result != SQLITE_OK
// Reset the statement to allow binding variables on the next iteration
result = sqlite3_reset(stmt);
}
// Release the statement
sqlite3_finalize(stmt);

.txt to SQlite3 with C++

I want to read data from txt file and insert it into SQlite database table with C++. I prepared a code but it doesn't work.
Example line from my txt file is follows;
',1417392060.000000','1.245430','1.2456','1.24469','1.245000'
And code;
sqlite3 *db;
char *zErrMsgt = 0;
int rct;
int rch;
int rchi;
rct = sqlite3_open("final.db", &db);
if( rct ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}else{
fprintf(stdout, "Opened database successfully\n");
}
string resline;
ifstream res("Res.txt");
while(getline(res,resline)){
string sqlli = "INSERT into FOREXNEW(DATE,OPEN,HIGH,LOW,CLOSE) VALUES ("getline(res,resline)");";
rchi = sqlite3_exec(db, sqlli.c_str(), callback, 0, &zErrMsgt);
if( rchi != SQLITE_OK ){
cout << "SQL error:" << zErrMsgt;
sqlite3_free(zErrMsgt);
}}
sqlite3_close(db);
Any alternate suggestions and solutions will be appreciated as well:) Thank you.
Getline doesn't return a string; it copies it to the variable passed in the second parameter. Change the erroneous line:
string sqlli = "INSERT into FOREXNEW(DATE,OPEN,HIGH,LOW,CLOSE) VALUES ("getline(res,resline)");";
to:
string sqlli = "INSERT into FOREXNEW(DATE,OPEN,HIGH,LOW,CLOSE) VALUES ("+resline+");";
And check if it works.

Howto implement SFTP with Qt/QNetworkAccessManager (C++)

I'm new to Qt and I would like to implement FTP and SFTP support for my software.
As I googled I discovered that there doesn't exist a sftp library for Qt but it should be possible with QNetworkAccessManager.
I tried then to discover on how to build a custom protocol or something like that but didn't figure out how to do it.
Does someone know how I could do that?
Thanks,
Michael
There is no support for SFTP in Qt SDK but Qt Creator implements SFTP.
I have isolated the library that contains SSH and SFTP and I have created a new project named QSsh in Github. The aim of the project is to provide SSH and SFTP support for any Qt Application.
I have written an example on how to upload a file using SFTP. Take a look at examples/SecureUploader/
I hope it might be helpful
You need a custom implementation for each protocol. But we can create a class like QHttp which will do that. There are several protocols that has similar semantic, but not all. So, if you want write it, tell me and I help you.
There's no current SSH wrapper implementation in the Qt SDK. You have 3 choices here:
Roll your own custom SSH/SFTP client implementation using the IETF RFC and standard drafts like RFC4253. This might not be what you're looking for.
Use any of the ssh implementation libraries like openssh/libssh directly or writing your own Qt/C++ wrapper for future-reuse. Any reasonably decent project with ssh needs usually links to a an already established ssh library and uses it programatically. Like Qt Creator does, if you dig inside it long enough you'll find what user Paglian mentioned earlier. Relying on a library is less risky and more future-proof then rolling your own.
Use openssh tooling at the command line interface directly, using QProcess just like you'd use it at the shell. This is is the fastest method if you're working on a proof-of-concept project and don't need any complex ftp operations, as it might get a bit difficult to devise a robust wrapper around the CLI tooling.
I do this using libssh. Very straight forward.
https://api.libssh.org/stable/libssh_tutor_sftp.html
Don't forget to add your sftp server into known hosts in your system.
ssh-keyscan -H mysftpserver.com >> ~/.ssh/known_hosts
Example code:
#include "sftpuploader.h"
#include <QtDebug>
#include <QFileInfo>
#include <libssh/libssh.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <libssh/sftp.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <QFile>
int verify_knownhost(ssh_session session)
{
int state, hlen;
unsigned char *hash = NULL;
char *hexa;
char buf[10];
state = ssh_is_server_known(session);
hlen = ssh_get_pubkey_hash(session, &hash);
if (hlen < 0)
return -1;
switch (state)
{
case SSH_SERVER_KNOWN_OK:
break; /* ok */
case SSH_SERVER_KNOWN_CHANGED:
fprintf(stderr, "Host key for server changed: it is now:\n");
ssh_print_hexa("Public key hash", hash, hlen);
fprintf(stderr, "For security reasons, connection will be stopped\n");
free(hash);
return -1;
case SSH_SERVER_FOUND_OTHER:
fprintf(stderr, "The host key for this server was not found but an other"
"type of key exists.\n");
fprintf(stderr, "An attacker might change the default server key to"
"confuse your client into thinking the key does not exist\n");
free(hash);
return -1;
case SSH_SERVER_FILE_NOT_FOUND:
fprintf(stderr, "Could not find known host file.\n");
fprintf(stderr, "If you accept the host key here, the file will be"
"automatically created.\n");
/* fallback to SSH_SERVER_NOT_KNOWN behavior */
case SSH_SERVER_NOT_KNOWN:
hexa = ssh_get_hexa(hash, hlen);
fprintf(stderr,"The server is unknown. Do you trust the host key?\n");
fprintf(stderr, "Public key hash: %s\n", hexa);
free(hexa);
if (fgets(buf, sizeof(buf), stdin) == NULL)
{
free(hash);
return -1;
}
if (strncasecmp(buf, "yes", 3) != 0)
{
free(hash);
return -1;
}
if (ssh_write_knownhost(session) < 0)
{
fprintf(stderr, "Error %s\n", strerror(errno));
free(hash);
return -1;
}
break;
case SSH_SERVER_ERROR:
fprintf(stderr, "Error %s", ssh_get_error(session));
free(hash);
return -1;
}
free(hash);
return 0;
}
bool upload(const QString &localFile,
const QString &dest,
const QString &host,
const QString &username,
const QString &passwd)
{
bool retVal = false;
QFileInfo info(localFile);
m_localFilename = info.canonicalFilePath();
m_remoteFilename = dest + "/" + info.fileName();
int verbosity = SSH_LOG_PROTOCOL;
int port = 22;
int rc;
sftp_session sftp;
sftp_file file;
int access_type;
int nwritten;
QByteArray dataToWrite;
ssh_session my_ssh_session;
QFile myfile(m_localFilename);
if(!myfile.exists())
{
qDebug() << "SFTPUploader: File doesn't exist " << m_localFilename;
return retVal;
}
my_ssh_session = ssh_new();
if(my_ssh_session == NULL)
{
return retVal;
}
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host.toUtf8());
ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
rc = ssh_connect(my_ssh_session);
if (rc != SSH_OK)
{
qDebug() << "SFTPUploader: Error connecting to localhost: " << ssh_get_error(my_ssh_session);
ssh_free(my_ssh_session);
return retVal;
}
else
{
qDebug() << "SFTPUploader: SSH connected";
}
// Verify the server's identity
// For the source code of verify_knowhost(), check previous example
if (verify_knownhost(my_ssh_session) < 0)
{
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
qDebug() << "SFTPUploader: verify_knownhost failed";
return retVal;
}
rc = ssh_userauth_password(my_ssh_session, username.toUtf8(), passwd.toUtf8());
if (rc != SSH_AUTH_SUCCESS)
{
qDebug() << "SFTPUploader: Error authenticating with password: " << ssh_get_error(my_ssh_session);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return retVal;
}
else
{
qDebug() << "SFTPUploader: Authentication sucess";
}
sftp = sftp_new(my_ssh_session);
if (sftp == NULL)
{
qDebug() << "SFTPUploader: Error allocating SFTP session:" << ssh_get_error(my_ssh_session);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return retVal;
}
rc = sftp_init(sftp);
if (rc != SSH_OK)
{
qDebug() << "SFTPUploader: Error initializing SFTP session:", sftp_get_error(sftp);
sftp_free(sftp);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return retVal;
}
access_type = O_WRONLY | O_CREAT | O_TRUNC;
file = sftp_open(sftp, dest.toUtf8(), access_type, S_IRWXU);
if (file == NULL)
{
qDebug() << "SFTPUploader: Can't open file for writing:", ssh_get_error(my_ssh_session);
sftp_free(sftp);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return retVal;
}
if(myfile.open(QFile::ReadOnly))
{
dataToWrite = myfile.readAll();
}
nwritten = sftp_write(file, dataToWrite, dataToWrite.size());
if (nwritten != dataToWrite.size())
{
qDebug() << "SFTPUploader: Can't write data to file: ", ssh_get_error(my_ssh_session);
sftp_close(file);
sftp_free(sftp);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return retVal;
}
rc = sftp_close(file);
if (rc != SSH_OK)
{
qDebug() << "SFTPUploader: Can't close the written file:" << ssh_get_error(my_ssh_session);
sftp_free(sftp);
ssh_disconnect(my_ssh_session);
ssh_free(my_ssh_session);
return retVal;
}
else
{
qDebug() << "SFTPUploader: Success";
retVal = true;
}
return retVal;
}

C++ MySQL Sql syntax error while syntax is correct

my C++ program reads a file with sql query and tries to execute it. When I execute the query using phpmyadmin, it works, but when executed in my program, it ends up with the following error:
Code:
ifstream create_file ("create.sql");
if (create_file.is_open())
{
char * create;
int length;
create_file.seekg (0, ios::end);
length = create_file.tellg();
create_file.seekg (0, ios::beg);
create = new char [length];
create_file.read (create,length);
create_file.close();
cout << "Executing query: " << endl;
cout.write (create,length);
cout << "EOF query" << endl;
if(mysql_query(mysql, "CREATE DATABASE grant_db")) {
fprintf(stderr, "Failed to create database: Error: %s\n",
mysql_error(mysql));
}
if(mysql_select_db(mysql, "grant_db")) {
fprintf(stderr, "Failed to select database: Error: %s\n",
mysql_error(mysql));
}
if(mysql_query(mysql, create)) {
fprintf(stderr, "Failed to create table: Error: %s\n",
mysql_error(mysql));
mysql_query(mysql, "DROP DATABASE grant_db");
}
delete[] create;
} else cout << "Unable to open file 'create.sql'.";
Thanks for your help!
You can't put multiple statements in a single mysql_query call. You need to execute them one at a time, by default at least. See the mysql_query docs.
My bet would be that it doesn't handle the /* ... */ comments. Try using -- comments instead.