hi i am getting a problem while using tntdb classes in my c++ code only integer values atr updating in database while string values are updating as garbage values i am giving my code below please help me out to solve this issue
#include <tntdb/connection.h>
#include <tntdb/connect.h>
#include <stdio.h>
#include <string>
#include <sys/shm.h>
using namespace std;
int main()
{
string s="create table test2(T1 int not null primary key,NAME varchar(20),STATUS varchar(20));";
try{
tntdb::Connection conn;
conn = tntdb::connect("sqlite:/home/gaian/Desktop/test.db");
string k="abc";
conn.execute(s);
tntdb::Statement st = conn.prepare("insert into test2(T1,NAME,STATUS) values (:T1, :NAME,:STATUS)");
st.set("T1",10)
.set("NAME",k)
.set("STATUS","bye")
.execute();
}
catch(const std::exception& e){
printf("error is %s\n",e.what());
}
return 0;
}
I run your code and it works fine on my plateform.
In order to avoid implicit cast you could use setString instead of set, that is to say :
st.set("T1",10)
.setString("NAME",k)
.setString("STATUS","bye")
.execute();
Related
I am doing kind of password manager, when I try to delete some entry ids are placed wrong
For instance, ids are place like ...19,20,21... >> deleted 20 >> ...19,21,22...
The only way to fix it, that I found was deleting and creating back id column.
The part of deleting a column works fine, but creating it back though doesn't work.
Here is my code
#include <iostream>
#include <mysql/mysql.h>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(){
sql::Driver *myDriver; //connecting an SQL
sql::Connection *myConn;
sql::Statement *myStmt;
sql::ResultSet *myRes;
myDriver = get_driver_instance(); // connecting to db
myConn = myDriver->connect("localhost", "root", "password");
myConn->setSchema("passwords");
myStmt = myConn->createStatement(); // deleting id column
myRes = myStmt->executeQuery("ALTER TABLE password_table DROP id;");
myStmt = myConn->createStatement(); // creating id column, creates an error
myRes = myStmt->executeQuery("ALTER TABLE password_table ADD id INT(200) NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY(id);");
return 0;
}
It should be noted that, if I do the same command in mysql itself, it will work, but not in code.
First, I thought that it's because of quotation marks, but that it did not work. Than I tried to reinstall mysql connector, but it did not work either.
The issue is that you are calling:
myRes = myStmt->executeQuery("ALTER TABLE password_table DROP id;");
for a query that doesn't produce results.
Instead, call
myStmt->execute("ALTER TABLE password_table DROP id;");
which returns bool to check if the command has results.
PS: and don't forget to add a try catch block on this code otherwise your program will crash and you never know why something is failing.
I need your help through This code that is an example of how to use the Crypto++ library to perform a Diffie-Hellman key exchange. The code uses the secp256r1 curve to generate the public and private keys and then uses the ECDH class to perform the key agreement. The code takes in two input strings, one for the private key of the first party (A) and one for the public key of the second party (B). It then converts these strings from hexadecimal to bytes and assigns them to SecByteBlock variables. It then uses the "Agree" method of the ECDH class to compute the shared secret between the two parties and stores it in a "sharedA" variable. Finally, the code converts the shared secret to an Integer and displays it in hexadecimal format., how it can be corrected to produce correct “Agreed shared secret key” which is supposed to be "1ed885b1064ae5e041cef89365eb7a63b87a6e3e59f6b3ec74c117b74c4e89a4"
The code;
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#include <string>
using std::string;
#include <stdexcept>
using std::runtime_error;
#include <cstdlib>
using std::exit;
#include "cryptopp/osrng.h"
using CryptoPP::AutoSeededRandomPool;
using CryptoPP::AutoSeededX917RNG;
#include "cryptopp/aes.h"
using CryptoPP::AES;
#include "cryptopp/eccrypto.h"
using CryptoPP::ECP;
using CryptoPP::ECDH;
#include "cryptopp/secblock.h"
using CryptoPP::SecByteBlock;
#include "cryptopp/oids.h"
using CryptoPP::OID;
// ASN1 is a namespace, not an object
#include "cryptopp/asn.h"
using namespace CryptoPP::ASN1;
#include "cryptopp/integer.h"
using CryptoPP::Integer;
#include <cryptopp/hex.h>
int main( int, char** ) {
OID CURVE = secp256r1();
ECDH < ECP >::Domain dhA( CURVE ), dhB( CURVE );
string privatekeyA = "e8586d5fa27ffb6a37817c171c94189ad20d8fcf29fa58ba0e6cbe4cf2bb1079";
string publickeyB = "4f0609f35a0be01caa1287862680b803ea50fb66af2ad65e990aa4a8944c6191ac0d13d98612e12ba1d9afa86f997aab2827a0cee43bf963e8e995eb95df2fb33";
SecByteBlock privA(dhA.PrivateKeyLength());
SecByteBlock pubB(dhB.PublicKeyLength());
string Prkey, Pukey;
unsigned char privateky[32];
unsigned char publicky[64];
CryptoPP::StringSource ssk(privatekeyA, true ,
new CryptoPP::HexDecoder(
new CryptoPP::StringSink(Prkey)
)
);
CryptoPP::StringSource ssv(publickeyB, true ,
new CryptoPP::HexDecoder(
new CryptoPP::StringSink(Pukey)
)
);
for(int i=0;i<32;i++) {
if (Prkey[i]<0) privateky[i]=Prkey[i]+256;
else privateky[i]=Prkey[i];
}
for(int i=0;i<64;i++) {
if (Pukey[i]<0) publicky[i]=Pukey[i]+256;
else publicky[i]=Pukey[i];
}
privA.Assign(privateky, sizeof(privateky));
pubB.Assign(publicky, sizeof(publicky));
SecByteBlock sharedA(dhA.AgreedValueLength());
dhA.Agree(sharedA, privA, pubB);
Integer a;
a.Decode(sharedA.BytePtr(), sharedA.SizeInBytes());
cout << "Agreed to shared secret: " << std::hex << a << endl;
return 0;
}
The output of this code is wrong. so help.
when i run the code above the output is this;
Agreed to shared secret: 8038e60132560000000000000000000000000000000000000000000000000000h
instead of this;
"1ed885b1064ae5e041cef89365eb7a63b87a6e3e59f6b3ec74c117b74c4e89a4"
I have a project where I import JSON files to setup global variables. There are various possibilities to the JSON object names in the files, so I want to know how can I check if an object is there or not. The way I tried to do it (with an if statement as shown below) causes an error
The program '..\..\build\jacky.exe' has exited with code 3 (0x00000003).
So it does not work at all. Is there a way to do this? Thanks!
#include <iostream>
#include <string>
#include <fstream>
#include "json.hpp" // json library by nlohmann
std::ifstream gJsonFile;
nlohmann::json j;
int var;
int main(int argc, char *argv[])
{
gJsonFile.open(/* whatever json file path */, std::ifstream::in);
if (gJsonFile.is_open())
{
j << gJsonFile;
if (j["Shirt Size"]) // causes exit with code 3 if false
var = (j["Shirt Size"]);
else
var = (j["AB Circumference"]);
}
}
if ( j.find("Shirt Size") != j.end() )
Or (this will create an entry if it does not already exist)
if ( !j["Shirt Size"].is_null() )
main.cpp
#include "sqlConnection.h"
#include <iostream>
int main() {
sqlConnection *sqlC = new sqlConnection();
sqlC->ifSucceed();
}
sqlConnection.h
#include <Windows.h>
#include <stdio.h>
#include <winsock.h>
#include "mysql.h"
#include <iostream>
#pragma once
using namespace std;
class sqlConnection
{
public:
sqlConnection();
~sqlConnection();
void ifSucceed();
MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
};
sqlConnection.cpp
#include "sqlConnection.h"
sqlConnection::sqlConnection()
{
mysql_init(&mysql);
mysql_real_connect(&mysql, "xxxxx", "xxxx", "xxxx", "librarySys", 7726, NULL, 0);
}
void sqlConnection::ifSucceed() {
char *sql = "select * from tb_bookcase";
mysql_query(&mysql, sql);
result = mysql_store_result(&mysql);
if ((row = mysql_fetch_row(result)) != NULL) {
cout << "succeed!" << endl;
} else {
cout << "faiiiiiiiiled" << endl;
}
}
sqlConnection::~sqlConnection() {
}
IF there is a libmysql.dll in source file error the message : There
are untreated exception:0x00007FFED00441E6 (libmysql.dll) (in the
librarySys.exe ) 0xC0000005: Access conflict happened when reading
0x0000000000000010.
And then I have to stop. VS give me choice, to change PDB, the binary file path and retry. But I do not know how to do it.
If I delete the libmysql.dll , the error message is:
The program can not be started for losing libmysql.dll in the
computer.Try to reinstall this program to solve this problem.
It's so confusing! I have tried many ways to connect. There are always error messages.
All mysql_* functions return a value that indicates success or failure. Your code disregards them. You ought to confirm whether calls were successful before proceeding to the next call. I surmise you actually failed to connect to the DB and your MYSQL is remains invalid.
Use your debugger. There's no point in coding in the dark. With your debugger you will quickly see whether your MYSQL object has been properly initialized.
Another thing:
sqlConnection *sqlC = new sqlConnection();
There is no reason for this to be heap allocated.
I looked around and I couldn't find the answer to how exactly to do this. I am trying to use Pantheios for logging and I want to write to an external file (otherwise whats the point). I am following one of the examples provided but It doesn't seem to be making the log file anywhere. Here is the code:
Edit: Also pantheios_be_file_setFilePath is returning -4 (PANTHEIOS_INIT_RC_UNSPECIFIED_FAILURE) so thats.....not helpful
#include "stdafx.h"
#include <pantheios/pantheios.hpp>
#include <pantheios/implicit_link/core.h>
#include <pantheios/implicit_link/fe.simple.h>
#include <pantheios/implicit_link/be.WindowsConsole.h>
#include <pantheios/implicit_link/be.file.h>
#include <pantheios/frontends/fe.simple.h>
#include <pantheios/backends/bec.file.h>
#include <pantheios/inserters/args.hpp>
PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("LogTest");
int _tmain(int argc, _TCHAR* argv[])
{
try
{
pantheios_be_file_setFilePath(PANTHEIOS_LITERAL_STRING("testlogforme.log"), PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BEID_ALL);
pantheios::log(pantheios::debug, "Entering main(", pantheios::args(argc,argv, pantheios::args::arg0FileOnly), ")");
pantheios::log_DEBUG("debug yo");
pantheios::log_INFORMATIONAL("informational fyi");
pantheios::log_NOTICE("notice me!");
pantheios::log_WARNING("warning!!");
pantheios::log_ERROR("error omg");
pantheios::log_CRITICAL("critical!!!");
pantheios::log_ALERT("alert mang");
pantheios::log_EMERGENCY("EMERGENCY!!!!!");
pantheios_be_file_setFilePath(NULL, PANTHEIOS_BEID_ALL);
system("pause");
return EXIT_SUCCESS;
}
catch(std::bad_alloc&)
{
pantheios::log_ALERT("out of memory");
}
catch(std::exception& x)
{
pantheios::log_CRITICAL("Exception: ", x);
}
catch(...)
{
pantheios::puts(pantheios::emergency, "Unexpected unknown error");
}
return EXIT_FAILURE;
}
Maybe I'm not calling a method or maybe its not being saved to a good location?
It turns out that some of the examples out there for pantheios are incorrect. You DO need to call pantheios_init() even if you are in C++. Here Is the example I got to work after deleting all my code and implementing an example that works.
// Headers for main()
#include <pantheios/pantheios.hpp>
#include <pantheios/backends/bec.file.h>
// Headers for implicit linking
#include <pantheios/implicit_link/core.h>
#include <pantheios/implicit_link/fe.simple.h>
#include <pantheios/implicit_link/be.file.h>
PANTHEIOS_EXTERN_C const char PANTHEIOS_FE_PROCESS_IDENTITY[] = "testLOL";
int main()
{
if(pantheios::pantheios_init() < 0)
{
return 1;
}
pantheios::log_NOTICE("log-1"); // save until log file set
pantheios_be_file_setFilePath("mylogfile.log"); // sets log file; write "log-1" stmt
pantheios::log_NOTICE("log-2"); // write "log-2" stmt
pantheios_be_file_setFilePath(NULL); // close "mylogfile"
pantheios::log_NOTICE("log-3"); // save until log file set
pantheios_be_file_setFilePath("mylogfile2.log"); // sets log file; write "log-3" stmt
pantheios::log_NOTICE("log-4"); // write "log-4" stmt
//system("pause");
return 0;
} // closes "mylogfile2" during program closedown
I found the example on a different post on stack overflow but like I said, the built in examples do not work.