mysql c++ connector setBlob() fails - c++

I have a table my_table with fields
id, INT, auto_increment
field_one, varchar(64)
field_two, tinytext
field_three, longblob
when trying to insert I use the following code
sql::PreparedStatement *prepared_statement = this->connection_->prepareStatement("INSERT INTO my_table(field_one,field_two,field_three) VALUES ('one','two',?)");
std::ifstream file_stream("myfile");
prepared_statement->setBlob(3, &file_stream);
I get MySQL_Prepared_Statement::setBlob: invalid 'parameterIndex'
this->connection_ is valid, the prepared_statement is not NULL, and the file stream is open
documentation on the connector classes seems lacking.
any suggestions?
Thank You!

Related

QSqlQuery not giving correct results when selecting TINYINT column

I have a very simple MySQL Table and want to select some of the rows using QSqlQuery.
I am connected to my local development mysql server (Windows; 64-bit).
When I run a SELECT query with other tools like mysql workbench, I always get the correct results (of course!).
Doing the same with QSqlQuery gives me no rows at all!
QString sql = "SELECT `ID`, `Name`, `ModbusID`, `DeviceType` FROM `Devices`;";
qDebug() << sql;
QSqlQuery query(m_db);
if(!query.prepare(sql))
{
qFatal("could not prepare query");
return;
}
if(!query.exec())
{
qFatal("could not execute query");
return;
}
while(query.next())
qDebug() << "result";
qDebug() << "finished.";
SQL-Table Definition
CREATE TABLE IF NOT EXISTS `Devices` (
`ID` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`MeasurementID` INT UNSIGNED NOT NULL,
`Name` VARCHAR(255) NOT NULL,
`ModbusID` TINYINT UNSIGNED NOT NULL,
`DeviceType` INT NOT NULL,
`TimestampCreated` DATETIME NOT NULL,
`TimestampLastModified` DATETIME,
FOREIGN KEY(`MeasurementID`) REFERENCES `Measurements`(`ID`) ON DELETE CASCADE,
UNIQUE (`MeasurementID`, `Name`)
);
In both programs I am connected to the same local mysql instance. There are no other mysql servers on my machine or on the network.
This happens with Oracle's MySQL and MariaDB! So the problem must be in my program.
Am I doing something wrong or what's happening here?
UPDATE After multiple tests the problem only occurs when I select ModbusID. I changed the table definition from
`ModbusID` TINYINT UNSIGNED NOT NULL,
to
`ModbusID` INT UNSIGNED NOT NULL,
and now it works. Looks like a bug in QT. But I dont think that this is a good solution. If you know a way to use TINYINT then please write an answer or comment!

MySQL Syntax error when locally importing dump from Amazon MySQL RDS?

When I create a database dump from Amazon RDS and then I try to import it locally, the result is ERROR 1064 (42000) at line 54.
At line 54 there is the following statement:
CREATE TABLE account_emailconfirmation (
The command used for dump is:
mysqldump -u user -h host.rds.amazonaws.com -p --default-character-set=utf8 --result-file=sync.sql database_name
The command used for import is:
mysql --user=root -p mpl -vv < sync.sql
And here is the output (verbosity increased).
--------------
CREATE TABLE `account_emailconfirmation` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created` datetime(6) NOT NULL,
`sent` datetime(6) DEFAULT NULL,
`key` varchar(64) NOT NULL,
`email_address_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `key` (`key`),
KEY `acc_email_address_id_5bcf9f503c32d4d8_fk_account_emailaddress_id` (`email_address_id`),
CONSTRAINT `acc_email_address_id_5bcf9f503c32d4d8_fk_account_emailaddress_id` FOREIGN KEY (`email_address_id`) REFERENCES `account_emailaddress` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
--------------
ERROR 1064 (42000) at line 54: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL,
`sent` datetime(6) DEFAULT NULL,
`key` varchar(64) NOT NULL,
' at line 3
Bye
The problem is with datetime(6). MySql introduced storing of fractional seconds in v5.6.4. The syntax for indicating fractional seconds in datetime - this is the (6) - is not recognised by previous mysql versions.
The data was exported from a mysql v5.6.4 or later, and was attempted to be imported into an earlier version. Since the error message starts with (6), I believe that this is the problem.
'key' is a reserved word in MySQL, and shouldn't be used as a column name. It's possible that a different version on Amazon RDS allowed it, but your best bet is to change the column name to something different.

Python-MySQLdb: Difficulty In Creating Table

I am facing a problem while creating a table in mysqldb using python like this:
cursor.execute("CREATE TABLE IF NOT EXISTS %s ( tweetId VARCHAR(100) NOT NULL, tweet VARCHAR(180) NOT NULL, PRIMARY KEY(tweetId) )",('Tutulive')) .
I am getting this error:
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Tutulive' ( tweetId VARCHAR(100) NOT NULL, tweet VARCHAR(180) NOT NULL, PRIMARY' at line 1")
SQL parameters should not be used for metadata. Sanitize the value yourself and insert it into the string normally.

SQLite - How to use it at terminal level & C++ application?

i am new to sqlite and I just recently installed it. I am familar with mysql but I need to use sqlite as I am using it for a C++ application that I am going to create.
Question 1:
I type this at my command line terminal
root#ubuntu:/home/baoky/version1.2/Assignment 2# sqlite abeserver.db
then I saw this output
sqlite>
So i type .h and i see a list of help commands
But i wanna create a table
sqlite> .databases
seq name file
--- --------------- ----------------------------------------------------------
0 main /home/baoky/version1.2/Assignment 2/abeserver.db
1 temp /var/tmp/sqlite_hjT3FEefcAHRPhn
in my main database
How do i execute this sql command at terminal level
CREATE TABLE abe_account (
username TEXT,
name TEXT,
department TEXT,
password TEXT
);
Question 2:
How do I insert record into the table abe_account using C++
Question 3:
How do I retrieve records from table abe_account and assign it to a string using C++
Sorry I tried google around and search around stack overflow, I am still confused with the usage, if its mysql, it would be much simple for me.
Question 2:
Question 3:
Let me google it for you, friend: An Introduction To The SQLite C/C++ Interface.
If you are using the sqlite terminal, you can just type SQL there, and it will be executed.
A typical cycle of work from your C++ code will look something like this:
sqlite3 * db;//database
sqlite3_stmt * stmt;//sql statement
sqlite3_open( "database.db", & db );//opening database
sqlite3_prepare( db, "SELECT something FROM something else;", -1, &stmt, NULL );//preparing the statement
sqlite3_step( stmt );//executing the statement
while( sqlite3_column_text( stmt, 0 ) )
{
char * str = (char *) sqlite3_column_text( stmt, 0 );///reading the 1st column of the result
//do your stuff
sqlite3_step( stmt );//moving to the next row of the result
}
sqlite3_finalize(stmt);
sqlite3_close(db);
You can easily google the functions to learn about their arguments and what they do in-detail.
To create a new database, just connect to it:
$ sqlite3 your_database_file
This will create your database in the file your_database_file. If this file already exists, the command will open it.
Then you can execute CREATE TABLE or any other SQL.

Unable to access tables created with sqlite3 from a program using the C API

I generate an sqlite3 database file (call it db.sl3; invoked interactively as $ sqlite3 db.sl3 from a shell) from within the sqlite3 commandline program, for instance
create table people (
id integer,
firstname varchar(20),
lastname varchar(20),
phonenumber char(10)
);
insert into people (id, firstname, lastname, phonenumber) values
(1, 'Fred', 'Flintstone', '5055551234');
insert into people (id, firstname, lastname, phonenumber) values
(2, 'Wilma', 'Flintstone', '5055551234');
insert into people (id, firstname, lastname, phonenumber) values
(3, 'Barny', 'Rubble', '5055554321');
I am trying to use this in a program I have written which uses the sqlite3 C API; however, whenever I attempt to open up the database file in the C program using either
sqlite3* db;
rc = sqlite3_open( "db.sl3", &db );
or
rc = sqlite3_open_v2( "db.sl3", &db, SQLITE_READONLY, 0 );
followed by a query where the SQL is contained in the following string
std::string sqlCmd = "select * from sqlite_master where type='table' order by name";
to the sqlite3_get_table wrapper interface invoked as follows
rc = sqlite3_get_table( db, sqlCmd.c_str(), &result, &numRows, &numCols, &errorMsg );
The return code (rc) is 0 in either case implying that there was no problem with the operation but there are no tables entries recorded in the result variable. I have tried all sorts of pathing issues (e.g., using absolute paths to the db file, relative, only in the invocation directory, etc.) to no avail. Also, when I reopen the database file with the sqlite3 commandline program I can see the tables and their entries. As an aside, if I open, create and insert the lines into the table in the C program I can access them fine.
Any help would be much appreciated. Am I not properly initializing things?
By default sqlite3_open will create the database if it does not exist (equivalent with calling sqlite3_open_v2 with flags=SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) - and rc will be SQLITE_OK.
I've tried this test program against a database created using sqlite3 and each time (existing db with people table: full path, relative; existing 0 sized db: full path, relative; nonexistent db: full path, relative) sqlite3_open_v2 behaved as advertised.
Try to do a 'sqlite3 [full_path_to_db]' then run the query and compare the results with what your program or mine does (again with [full_path_to_db]) just in case your sqlite3_open tests created some 0 sized db.sl3 which are then gadly opened by sqlite3_open_v2 .
Hope this helps.