How i can save the char data directly to blob db? - c++

I need to save an wxBitmap to SQLite to a blob type without save to disk or on some media.
i have mybitmap as wxBitmap;
First must convert to wxImage and get data.
Ok until now i hope, but how i can save the char data directly to blob db?
In sql must have
update mytable SET blob_column='??????' WHERE id='1'
or something like that. How must construct query?
wxImage img=mybitmap.ConvertToImage();
unsigned char* data=img.GetData();
int datalength=img.GetWidth()*img.GetHeight()*3;
char *myBuffer = (char *)data;
The code bellow is for wxSQLite3 ...
wxMemoryOutputStream mo;
img.SaveFile( mo, wxBITMAP_TYPE_PNG );
wxSQLite3Statement stmt = db.PrepareStatement("UPDATE mytable SET blob_column='?' WHERE id='1'");
stmt.Bind(0, (unsigned char *)mo.GetOutputStreamBuffer()->GetBufferStart(), mo.GetOutputStreamBuffer()->GetBufferSize());
stmt.ExecuteUpdate();
But how to translate for SQLite?

Related

How can i print data from table in cpp using sqlite3 db?

I have created a student table in .cpp.
static const char student[] = R"sql(
CREATE TABLE if not exists student(
id VARCHAR(44) NOT NULL,
name varchar NOT NULL,
);
)sql";
I am adding some data in this table from another class. I know data has been added to this table, but I want to print all value from this table for verification.
then I write this statement,
static const char print_student[]="select * from student";
how can I print student table' data in CPP? I am using sqlite3 db.

How to convert string format of time type field in DolphinDB c ++ API

Suppose I have the following table,
t=table(1..3 as id,(now() - 2)..now() as ts)
share(t, `sharedT);
I query the value of the ts column in the API, the code is as follows,
DBConnection conn;
TableSP table = conn.run("select * from sharedT");
ConstantSP col1=table->getcolumn(1);
cout<<col1->getString(0)<<endl;
The displayed result is like 2020.02.20T01:44:58, but the format I want to get is 02/20/2020T01:44:58.
How to convert this is more convenient?
It is better off to format the datetime column on the server end.
DBConnection conn;
TableSP table = conn.run("select id,format(ts,'MM/dd/yyyyTHH:mm:ss') as ts from sharedT");
ConstantSP col1=table->getcolumn(1);
cout<<col1->getString(0)<<endl;

Getting real data type using ODBC, Data is converted to higher precision

I have a table having real datatype column in sql server.
CREATE TABLE Table_1(f_int int, f_string nchar(10), f_real real);
INSERT INTO Table_1 (f_int, f_string, f_real) VALUES (42000000, 'myString2', 40003.16);
when i execute select f_real from Table_1; using ODBC API(From c++ application)
I get value 40003.160156250000 instead of 40003.16
I have binded the result column like below.
struct ColValInfo
{
ColValInfo(){
pValue = NULL;
}
SQLPOINTER pValue;
SQLINTEGER StrLen_or_Ind;
};
ColValInfo* m_pColValInfo;
m_pColValInfo = new ColValInfo[numColumns];
m_pColValInfo[0].pValue = (SQLPOINTER) new char[50];
SQLBindCol(hstmt, 1, SQL_C_DOUBLE, m_pColValInfo[0].pValue,
siz, &(m_pColValInfo[0].StrLen_or_Ind));
Note : SQLPOINTER is a typedef of void * and SQLINTEGER is typedef of long.
I am getting data by doing below
double data = *(double *)m_pColValInfo[0].pValue;
I am using odbc driver 13 for communicating with database. As driver takes care of converting data from sql type to native type which is specified in SQLBindCol ODBC call, i want to know how i am getting 40003.160156250000 when i have 40003.16 in my database.
I have changed precision and scale of the data base column to decimal(16,2), to see if that makes any difference, i see no difference it makes.
Could some please let me know where the conversion is going wrong.

Convert blob images to Mat images

I am using mysql database for my face recognition project.Here I store images into the table and the images are stored as blob images.when select these images back, i need to write these blob images to separate image files using a file pointer and it will be stored into a folder in which the program resides.And for using those images, i need to read it again from that folder.Then only i can use that images to another function. But i need to use those blob images directly from the database(when we select it from the DB, i need to pass it to another function). So that i can reduce operations like reading it again from the folder.How can i convert bl .So for passing DB images directly to another function, i think we need to convert it's type or something.
void SelectImage()
{
MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW row;
char temp[900];
char filename[50];
unsigned long *lengths;
FILE *fp;
my_ulonglong numRows;
unsigned int numFields;
int mysqlStatus = 0;
MYSQL_RES *mysqlResult = NULL;
conn = mysql_init(NULL);
mysql_real_connect(conn, "localhost", "root", "athira#iot", "Athira", 0, NULL, 0);
int state=mysql_query(conn, "SELECT * FROM ima1");
mysqlResult=mysql_store_result(conn);
if(mysqlResult)
{
numRows=mysql_num_rows(mysqlResult);
}
cout<<"rows:"<<numRows<<endl;
for(int i=1;i<=numRows;i++){
sprintf(temp,"SELECT data FROM ima1 WHERE id=%d",i);
sprintf(filename,"Gen_Image%d.jpeg",i);
fp = fopen(filename, "wb");// open a file for writing.
cout<<temp<<" to "<<filename<<endl;
mysql_query(conn, temp);//select an image with id
result = mysql_store_result(conn);
row = mysql_fetch_row(result);//row contains row data
lengths = mysql_fetch_lengths(result);//this is the length of th image
fwrite(row[0], lengths[0], 1, fp);//writing image to a file
cout<<"selected..."<<endl;
img.create(100,100,CV_16UC1);
memcpy(img.data,row.data,lengths);
mysql_free_result(result);
fclose(fp);
}
mysql_close(conn);
}
I tried to convert it to Mat type but it's showing error..
error is this,
error: request for member ‘data’ in ‘row’, which is of non-class type ‘MYSQL_ROW {aka char**}’
If your images are stored using some image format (JPEG, PNG, BMP, ...) and not as raw pixel data, then you should be looking at the imdecode function in OpenCV.
If your data is stored as raw pixels, then you'd have to make sure that the memory layout of your BLOB data can be expressed using the step/stride functionality OpenCV uses to layout its matrices.

Trying to modify an sqlite3 insert to return the PK. Wasn't sure how to do it with sqlite3 specifically

I was trying to insert into a table some data, but I wasn't sure what flag allows me to return the primary key. I believe I recall MSSQL using RETURNING, and some others tack on RETURNS at the end.
could someone help out with the appending of it?
I'm trying to return TABLEA.a, and my query and design would look something like this:
sqlite3 *db;
sqlite3_open("...",&db);
std::string query;
query = "insert into TABLEA (b,c,d,e) values (#b,\"#c\",#d,#e);";
//^--this needs to be modified.
sqlite3_stmt *sqlstmt;
int rc;
rc = sqlite3_prepare_v2(db, query.c_str(), 01, &sqlstmt, 0);
sqlite3_step(sqlstmt);
int ID;
ID = sqlite3_column_integer(sqlstmt,0);
Have you tried sqlite3_last_insert_rowid ?