I get a std::vector< std::vectorstd::string > and want to stream this to the database using pqxx::stream_to (pretty huge amount of data) e.g:
pqxx::work insert_tx(C);
pqxx::stream_to stream{
insert_tx,"CompTable",std::vector<std::string>{"TKey", "AKey"}};
for (auto&& row : vector_of_vectors)
{
auto val = std::make_tuple(row.at(3),row.at(2));
stream<<val;
}
stream.complete();
insert_tx.commit();
This works fine as long there is no "DATE" format needed.
I know i can do it with SQL statements e.g (".... VALUES ($1::date)",std::string) but this doesn't work with pqxx::stream_to
So does anyone know which c++ datatype or struct or whatever is compatible with the SQL data format?
Thank you for your time and ideas :)
It just works using the string format "yyyy/MM/dd" or "yyyy-MM-dd" or some other format, maybe the count of second (you can see it in java.sql.date).
Related
I am using JSON format to save data in my c++ program , i want to send it to MySql database (the table tab has one column with type : TEXT) but the query failed (tested also VARCHAR and CHAR )
this is a part of the code since we are not interrested in the rest
string json_example = "{\"array\":[\"item1\",\"item2\"], \"not an array\": \"asdf\"}";
mysql_init(&mysql); //initialize database connection
string player="INSERT INTO tab values (\"";
player+= json_example;
player += "\")";
connection = mysql_real_connect(&mysql,HOST,USER,PASSWD,DB,0,NULL,0);
// save data to database
query_state=mysql_query(connection, player.c_str()); // use player.c_str()
to show the final query that will be used : cout << player gives :
INSERT INTO tab values ("{"array":["item1","item2"], "not an
array": "asdf"}")
using for example string json_example = "some text"; is working
but with the json format it is not working , maybe the problem came from the use of curly bracket {} or double quotes "" but i haven't find a way to solve it .
i'm using :
mysql Ver 14.14 Distrib 5.5.44, for debian-linux-gnu (armv7l) under raspberry pi 2
Any help will be appreciated , thanks .
Use a prepared statement. See prepared statements documentation in the MySQL reference manual.
Prepared statements are more correct, safer, possibly faster, and keep your code cleaner. You get all those benefits and don't need to escape anything. There is hardly a reason not to use them.
Something like this might work. But take it with a grain of salt, because I have not tested or compiled it. It should just give you the general idea:
MYSQL_STMT* const statement = mysql_stmt_init(&mysql);
std::string const query = "INSERT INTO tab values(?)";
mysql_stmt_prepare(statement, query, query.size());
MYSQL_BIND bind[1] = {};
bind[0].buffer_type = MYSQL_TYPE_STRING;
bind[0].buffer = json_example.c_str();
bind[0].buffer_length = json_example.size();
mysql_stmt_bind_param(statement, bind);
mysql_stmt_execute(statement);
I want to select all the binary data from a column of a SQL database (SQL Server Enterprise) using C++ query. I'm not sure what is in the binary data, and all it says is .
I tried this (it's been passed onto me to study off from) and I honestly don't 100% understand the code at some parts, as I commented):
SqlConnection^ cn = gcnew SqlConnection();
SqlCommand^ cmd;
SqlDataAdapter^ da;
DataTable^ dt;
cn->ConnectionString = "Server = localhost; Database=portable; User ID = glitch; Pwd = 1234";
cn->Open();
cmd=gcnew SqlCommand("SELECT BinaryColumn FROM RawData", cn);
da = gcnew SqlDataAdapter(cmd);
dt = gcnew DataTable("BinaryTemp"); //I'm confused about this piece of code, is it supposed to create a new table in the database or a temp one in the code?
da->Fill(dt);
for(int i = 0; i < dt->Rows->Count-1; i++)
{
String^ value_string;
value_string=dt->Rows[i]->ToString();
Console::WriteLine(value_string);
}
cn->Close();
Console::ReadLine();
but it only returns a lot of "System.Data.DataRow".
Can someone help me?
(I need to put it into a matrix form after I extract the binary data, so if anyone could provide help for that part as well, it'd be highly appreciated!)
dt->Rows[i] is indeed a DataRow ^. To extract a specific field from it, use its indexer:
array<char> ^blob=dt->Rows[i][0];
This extracts the first column (since you have only one) and returns an array representation of it.
To answer the question in your code, the way SqlDataAdapter works is like this:
you build a DataTable to hold the data to retrieve. You can fill in its columns, but you're not required to. Neither are you required to give it a name.
you build the adapter object, giving it a query and a connection object
you call the Fill method on the adapter, giving it the previously created DataTable to fill with whatever your query returns.
and you're done with the adapter. At this point you can dispose of it (for example inside a using statement if you're using C#).
I am programming in c++,i have a requirement as to get the date only ,rest of the components are not needed,
i have contents in my file in the following way:-
2012-03-26T15:05:24.844Z - DEBUG: Logging_Test:test3 : Testing file logger
2012-03-26T15:05:24.844Z->this part is returned from a function GetDateTime(),which has a stringstream sDateStream and returing to the function GetDateTime() a string
My objective is just to get the date from this part and compare it with the system's date and for the system date i m using:-
void FileLogger::date()
{
SYSTEMTIME time;//variable time to get the system time
GetLocalTime(&time);//reference to the timek
int hour = time.wHour;
if (hour > 12) hour -= 12;
int year = time.wYear;
int month = time.wMonth;
int day = time.wDay;
//not returning anything just storing values of day ,month
//& year
}
now i need to make a comparision b/w two dates and if the date in the .txt is smaller than today's date a new file gets created
please help guys i know it is very simple for the masters but for a starter like me it is creating troubles , please provide help in form of some code so that i can understand it
If you've got the line as a string, and you're sure of the format, the
simplest solution would be:
std::string dateString(
line.begin(), std::find( line.begin(), line.end(), 'T' ) );
(I'd still do some extra checking; e.g. that the dateString is exactly
10 characters long, that it only contains digits or '-', etc.)
This is the ISO date format; it has been especially designed to support
comparison directly as a string, so you might want to consider getting
the system date as a string in ISO format as well. (This is very easy
if you use the standard functions time, localtime and strftime.)
I searched a bit but couldn't find the answer.
I want to get current date and format to YYYYMMDD
I cannot use EXSLT as per my requirements.
A very simple Inline C# Script Functoid could look like this:
public string MyDateFormat(string dateValue)
{
string result = String.Empty;
string outputFormat = "{0:yyyyMMdd}";
DateTime parsed;
if (DateTime.TryParse(dateValue, out parsed))
{
result = String.Format(outputFormat, parsed);
}
else
{
result = String.Format(outputFormat, DateTime.MinValue);
}
return result;
}
For a similar problem I created a External Assembly which will allow to specify CultureInfo for parsing the input DateTime string and also submit the output format string as a functoid input parameter.
You want the substring to act on the date.
To get the date:
substring-before($dateTime, 'T')
To get the year you want to work on the above result:
substring-before($previousResult, '-')
Then concatenate the values you got from the string manipulations.
This explains the whole thing and wraps it on a template: Format a date in XML via XSLT
etc
Hope this helps.
I have a vector string of dates in the from "dd-mmm-yyyy" so for example
todays date would be:
std::string today("07-Sep-2010");
I'd like to use the date class in boost but to create a date object the
constructor for date needs to be called as follows:
date test(2010,Sep,07);
Is there any easy/elegant way of passing dates in the format "dd-mmm-yyyy"?
My first thought was to use substr and then cast it? But I've read that there's also
the possibility of using 'date facets'?
Thanks!
include "boost/date_time/gregorian/parsers.hpp"
date test = boost::gregorian::from_us_string("07-Sep-2010")
There is a builtin parser for this form of date in Boost itself, check out the docs here:
http://www.boost.org/doc/libs/1_44_0/doc/html/date_time/date_time_io.html#date_time.io_objects
date_type parse_date(...)
Parameters:
string_type input
string_type format
special_values_parser
Parse a date from the given input using the given format.
string inp("2005-Apr-15");
string format("%Y-%b-%d");
date d;
d = parser.parse_date(inp,
format,
svp);
// d == 2005-Apr-15
with inp adjusted for your needs.