MySql connection threw c++ in ubuntu - mysql

I have installed necessary packages
sudo apt-get install mysql-server
sudo apt-get install libmysqlcppconn-dev
here is my code:
/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>
/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#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(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:11840", "root", "n");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message"); // replace with your statement
while (res->next()) {
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
/* Access column fata by numeric offset, 1 is the first column */
cout << res->getString(1) << endl;
}``
delete res;
delete stmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
//cout << "(" << __FUNCTION__ << ") on line " »
// << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout << endl;
return EXIT_SUCCESS;
}
I compiled using:
sudo g++ -Wall -I/usr/include/cppconn -o testapp mysql_connect1.cpp -L/usr/lib -lmysqlcppconn
it got compiled successfully
when I run:
./testapp
Running 'SELECT 'Hello World!' AS _message'...
# ERR: SQLException in mysql_connect1.cpp# ERR:
Can't connect to MySQL server on '127.0.0.1' (111)
(MySQL error code: 2003, SQLState: HY000 )
I get the above error
I made the below changes as suggested here:
run the command vim /etc/mysql/my.cnf
comment bind-address = 127.0.0.1 using the # symbol
restart your mysql server once.
But it still didn't work
output of:
root#knils-HP:/home/knils# sudo netstat -tap | grep mysql
tcp 0 0 *:mysql *:* LISTEN 11840/mysqld
root#knils-HP:/home/knils#
Please can someone help me with this?

Are you sure your server runs on 11840 and the user account you are using is allowed to connect from localhost?
con = driver->connect("tcp://127.0.0.1:11840", "root", "n");
Your console output that more looked like a processID or internal port for me.
MySQL usually runs on 3306 except you changed that on your own.
Can you try:
con = driver->connect("tcp://127.0.0.1:3306", "root", "n");
?

Related

how to detect "​" (combination of unicode) in c++ string

I am trying to detect some of the combination of Unicode character (like ​) to cleanup the string, For a single Unicode character it is detecting but combination of Unicode is not detecting.
These string I am using to make HTML page from another HTML page which need to be cleanup. I want to clean only string which have these kind of unicode that not even visible in html page in browser.
below is the sample code:
void detect_Unicode(string& str) {
if(!str.empty() && str.find_first_not_of(" \t\n\r\f\v\u00A0\u00C2\u00E2\u20AC\u2039")==string::npos)
str.assign(" ");
return;
}
Input string:
1. " ​ ​ " ;
2. "are   there is something    ​ combination ​"
3. " Â Â "
4. "​   ​"
5 . "Â Â â â"
Expected Output:
1. " "
2. "are   there is something    ​ combination ​"
3. " "
4. " "
5. " "
Please let me know other ways too.
OK, following on from the comments above, I think it's highly likely that the input string is in UTF-8 (after all, in an HTML context, what else would it be?).
On that basis, I humbly submit this:
#include <string>
#include <codecvt>
#include <locale>
std::string narrow (const std::wstring& ws)
{
std::wstring_convert <std::codecvt_utf8 <wchar_t>, wchar_t> convert;
return convert.to_bytes (ws);
}
std::wstring widen (const std::string& s)
{
std::wstring_convert <std::codecvt_utf8 <wchar_t>, wchar_t> convert;
return convert.from_bytes (s);
}
std::string detect_Unicode (const std::string& s)
{
std::wstring ws = widen (s);
if (ws.empty() || ws.find_first_not_of (L" \t\n\r\f\v\u00A0\u00C2\u00E2\u20AC\u2039") != std::wstring::npos)
return " ";
return s;
}
#include <iostream>
int main ()
{
std::cout << narrow (L"\u00A0 \u00C2 \u00E2 \u20AC \u2039\n\n");
std::cout << "0.\t\"" << detect_Unicode (u8"abcde") << "\"\n";
std::cout << "1.\t\"" << detect_Unicode (u8" ​ ​ ") << "\"\n";
std::cout << "2.\t\"" << detect_Unicode (u8"are   there is something    ​ combination ​") << "\"\n";
std::cout << "3.\t\"" << detect_Unicode (u8" Â Â ") << "\"\n";
std::cout << "4.\t\"" << detect_Unicode (u8"​   ​") << "\"\n";
std::cout << "5.\t\"" << detect_Unicode (u8"Â Â â â") << "\"\n";
}
Output:
 ⠀ ‹
0. " "
1. " ​ ​ "
2. " "
3. " Â Â "
4. "​   ​"
5. "Â Â â â"
Now this is not the output the OP expects, but I think that's simply because the logic (as opposed to the implementation) of detect_Unicode() looks flawed. The point here is that converting the input string to a wide string means that you can use standard basic_string operations on it reliably, because there are no multibyte issues now.
An alternative, slightly radical, implementation of detect_Unicode() might be:
for (auto wide_char : ws)
{
if (wide_char > 0xff)
return " ";
}
return s;
But really, now you have a wide string to hand in detect_Unicode, anything is possible, so go wild OP.
Other notes:
std::codecvt is deprecated in C++17, but since there is no other obvious choice you might as well run with it. You can always change the implementations of narrow and widen if it comes to it.
Depending on platform, std::wstring might not be the best choice but it's probably fine. You could also look at std::u16string and std::u32string.
Live demo.
Inspiration taken from here.

uboot a hello world kernel

For a Hello world uboot project I am using this tutorial.
I have used GNU ARM toolchain
Downloaded Uboot source : u-boot-2017.09
Compiled it
make vexpress_ca9x4_defconfig ARCH=arm CROSS_COMPILE=../gcc-arm-none-eabi-6-2017-q2-update/bin/arm-none-eabi-
make all ARCH=arm CROSS_COMPILE=../gcc-arm-none-eabi-6-2017-q2-update/bin/arm-none-eabi-
And the hello world project looks like
test.c
volatile unsigned int * const UART0DR = (unsigned int *)0x101f1000;
void print_uart0(const char *s) {
while(*s != '\0') { /* Loop until end of string */
*UART0DR = (unsigned int)(*s); /* Transmit char */
s++; /* Next char */
}
}
void c_entry() {
print_uart0("Hello world!\n");
}
startup.s
.global _Reset
_Reset:
LDR sp, =stack_top
BL c_entry
B .
test.ld
ENTRY(_Reset)
SECTIONS
{
. = 0x100000; /*initial address*/
.startup . : { startup.o(.text) }
.text : { *(.text) }
.data : { *(.data) }
.bss : { *(.bss COMMON) }
. = ALIGN(8);
. = . + 0x1000; /* 4kB of stack memory */
stack_top = .;
}
And the compilation
../gcc-arm-none-eabi-6-2017-q2-update/bin/arm-none-eabi-gcc -c -mcpu=arm926ej-s test.c -o test.o
../gcc-arm-none-eabi-6-2017-q2-update/bin/arm-none-eabi-as -mcpu=arm926ej-s startup.s -o startup.o
../gcc-arm-none-eabi-6-2017-q2-update/bin/arm-none-eabi-ld -T test.ld -Map=test.map test.o startup.o -o test.elf
../gcc-arm-none-eabi-6-2017-q2-update/bin/arm-none-eabi-objcopy -O binary test.elf test.bin
Create Image :
mkimage -A arm -C none -O linux -T kernel -d test.bin -a 0x00100000 -e 0x00100000 test.uimg
Output :
Image Name:
Created: Mon Oct 23 20:58:01 2017
Image Type: ARM Linux Kernel Image (uncompressed)
Data Size: 146 Bytes = 0.14 kB = 0.00 MB
Load Address: 00100000
Entry Point: 00100000
Create a single binary :
cat ../u-boot-2017.09/u-boot test.uimg > flash.bin
Calculated uboot binary size
printf "bootm 0x%X\n" $(expr $(stat -c%s u-boot.bin) + 65536)
bootm 0x218F20
Then run :
qemu-system-arm -M vexpress-a9 -kernel flash.bin -m 128M -nographic
Interrupted it and then run bootm 0x218F20
But it says
Wrong Image Format for bootm command
ERROR: can't get kernel image!
Any suggestion?
Either try go command with same built image (go 0x218F20) Or to use bootm
refer [1] (5.12.3. Processor cache considerations), the mkimage command seems to be wrong.
[1] https://www.denx.de/wiki/DULG/UBootStandalone

Can't build very simple tcl application

I'm trying to write a very simple Tcl application in C++:
#include <tcl.h>
#include <iostream>
int main (int argc, char *argv[])
{
std::cout << "Calling Tcl_FindExecutable." << std::endl;
Tcl_FindExecutable (argv[0]);
std::cout << "Calling Tcl_CreateInterp." << std::endl;
Tcl_Interp *pInterp = Tcl_CreateInterp ();
if (Tcl_Eval (pInterp, "puts stdout {Hello, World!}") != TCL_OK)
{
std::cerr << "Error: " << Tcl_GetStringResult (pInterp) << std::endl;
return (0);
}
if (Tcl_Eval (pInterp, "puts stdout [info nameofexecutable]") != TCL_OK)
{
std::cerr << "Error: " << Tcl_GetStringResult (pInterp) << std::endl;
return (0);
}
return (1);
}
I can compile it via g++ -c Wall -I/opt/ActiveTcl-8.6/include noddy.cpp -o noddy.o
but when I link it, with g++ -L/opt/ActiveTcl-8.6/lib -ltcl8.6 -o noddy noddy.o
I get errors saying that all the Tcl library procedures are undefined.
What am I doing wrong, please?
Edit
The actual commands were
$ g++ -c -Wall -I/opt/ActiveTcl-8.6/include noddy.cpp -o noddy.o
$ g++ -L/opt/ActiveTcl-8.6/lib -ltcl8.6 -o noddy noddy.o
noddy.o: In function 'main':
noddy.cpp:(.text+0x37): undefined reference to 'Tcl_FindExecutable'
noddy.cpp:(.text+0x60): undefined reference to 'Tcl_CreateInterp'
noddy.cpp:(.text+0x78): undefined reference to 'Tcl_Eval'
noddy.cpp:(.text+0x8d): undefined reference to 'Tcl_GetStringResult'
noddy.cpp:(.text+0xda): undefined reference to 'Tcl_Eval'
noddy.cpp:(.text+0xef): undefined reference to 'Tcl_GetStringResult'
collect2: ld returned 1 exit status
In link statements the order of object modules and libraries is significant. You should include the object first and then the libraries (redone as C to avoid installing g++):
> gcc -Wall -I/opt/tcl/include -c noddy.c
> gcc -o noddy.exe noddy.o -L/opt/tcl/lib -ltcl86
> noddy
Calling Tcl_FindExecutable.
Calling Tcl_CreateInterp.
Hello, World!
C:/Code/noddy.exe
But:
> gcc -o noddy.exe -L/opt/tcl/lib -ltcl86 noddy.o
noddy.o:noddy.c:(.text+0x23): undefined reference to '_imp__Tcl_FindExecutable'
noddy.o:noddy.c:(.text+0x36): undefined reference to '_imp__Tcl_CreateInterp'
noddy.o:noddy.c:(.text+0x50): undefined reference to '_imp__Tcl_Eval'
noddy.o:noddy.c:(.text+0x62): undefined reference to '_imp__Tcl_GetStringResult'
noddy.o:noddy.c:(.text+0x9b): undefined reference to '_imp__Tcl_Eval'
noddy.o:noddy.c:(.text+0xad): undefined reference to '_imp__Tcl_GetStringResult'
e:/opt/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: noddy.o: bad reloc address 0x20 in section '.eh_frame'
e:/opt/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status

Print Universal Characters (html) stored in a string c++

I am trying to print a string with universal characters stored in it. If i initialized the string with the following:
string test = "\u000D\u000A\u000D\u000Aclass Solution {\u000D\u000Apublic:\u000D\u000A
cout << test << endl;
it would print out the output I want:
class Solution {
but if I get the same string from Curl result, like following:
curl_easy_setopt(curl, CURLOPT_WRITEDATA, addressof(test));
res = curl_easy_perform(curl);
cout << test << endl;
it would print out:
\u000D\u000A\u000D\u000Aclass Solution {\u000D\u000Apublic:\u000D\u000A
I tried to turn the test into const char * by c_str and then print it out, but it is still not working. I tried to google it for a few hours but unfortunately I cannot find the answer.
I am using Mac and compile with
clang++ -std=c++11 -Wall -Wextra -lcurl
May anyone help me on this?
Thanks a lot!
Just in case anyone facing the same issue, I get this done by using ICU library with UnicodeString Converter.

Qt isOpen method false value MySQL

I have a MySQL database, which i created in Workbench. I can easily connect to it using php code:
<?php
$host = "localhost";
$port = 3306;
$socket = "";
$user = "hate";
$password = "88005553535";
$dbname = "galleries_db";
$con = new mysqli($host, $user, $password, $dbname, $port, $socket)
or die ('Could not connect to the database server' . mysqli_connect_error());
echo "DONE!";
?>
But when I'm trying to connect to my DB with Qt like this:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setPort(3306);
db.setDatabaseName("galleries_db");
db.setUserName("hate");
db.setPassword("88005553535");
db.open();
if(!db.isOpen()) {
ui->label->setText("-");
qDebug() << "Error: "<< db.lastError() << " " << db.lastError().text();
}
else
ui->label->setText("+");
I always get false value returned by isOpen method. And in stream I have:
Error: QSqlError("", "", "") " "
May be problem in MySQL driver for Qt ? Or there is something else ?
Will appriciate any help or advice.
There should be:
if(db.isOpen())
Not:
if(!db.isOpen())
Actually it is very silly mistake. isOpen always returned me true value, and I made it false by myself.