I have an array of 128 ints. Which I want as a column in my mysql table. (or if someone has a better Idea that is welcome)
Basically I don't know 2 things:
1) What is the syntax for this process in C++ i.e serializing the data and inserting it into the table.
int x = 1;
int myarray[128];
serialize(myarray);//?
mysql_query(con, "INSERT INTO db VALUES('"x"', myarray)");
//int myarray[128]; also dont know the syntax for the int value insertion.
2) What is the syntax for the table creation process with this serialized data i.e
mysql_query(con, "CREATE TABLE table(Id INT not null, (serialized int array[128]))");
Any help would be greatly appreciated!
My understanding of the MySql INSERT INTO syntax is:
INSERT INTO db
("X")
VALUES ((1), (2), (3));
You don't want to serialize an array for this. You want the textual representation of each value in the array.
I suggest using std::ostringstream and a for loop to build a string containing the textual representations of the array contents.
You could do as I did and search the web for examples of the correct syntax:
Wikipedia - SQL Syntax for INSERT into, including multiple rows
Edit 1:
Also search the web for "MySql Load File":
Google results of "MySql load file"
Related
I just started working with the sql conenctor in C++ (and sql).
I need to write a bunch of header files from objects out of a database. (MariaDB but would be great if it works on all sql dbs)
My Solution so far is getting the tablenames with
res = stmt->executeQuery("SHOW TABLES from " + dbname);
where dbname is a string entered by the user.
I am storing the data for later use in a vector called tablenames and use it as follows:
for(std::string table :tablenames){
delete stmt; //freeing memory and attach new information to it
delete res;
std::string query;
query = "SELECT * from " + table + ";";
stmt = con->createStatement();
res = stmt ->executeQuery(query);
std::cout << query << "\n";
//using metadata for getting information or passing it to another method
}
It is not ready but will work, but I take much more information out of the database, than I need. I would like to take just 1 row out of the database (or maybe just the tableinformation) and access the metadata to retrieve the information (like columnlabel, columntypename and maybe the displaysize) i need for the headerfile.
So my problem is, that i generate to much traffic with my solution, especially if i run this against big databases.
I found some solutions that used something like
WHERE id = 1
but i can't guarantee that there is an id (or something else) in the table.
I hope you can help me to find a better solution.
Use the limit keyword. "select * from table LIMIT 1" This will do as it says and limit the result to just one.
How can i read varchar from sql server using native C++ code. Do you have any advice? What is the best way to do it?
My application(native c++) allows users to write Sql queries external and use that file to fetch some data from database which can be used inside our application.
Now the problem is customers can write queries which includes both small(int for example) and large data type(For Ex varchar and varbinary) columns ordered randomly.
I am using SQLBindCol to bind to application variables.I cannot use this method to bind large data types unless I allocate the buffer large enough to store the data(Length unknown while Binding). Data can be of any size(less than max size allowed by sql server column), I have memory concerns in allocating large buffers. So I thought I can use SQLGetData, but i see the below from microsoft.
The SQL Server Native Client ODBC driver does not support using SQLGetData to retrieve data in random column order. All unbound columns processed with SQLGetData must have higher column ordinals than the bound columns in the result set.
sqlgetdata
And also from this link it is said
For generic application, you may use SQLFetch + SQLGetData to obtain the maximum length and data. Actually, for a very long column (image datatype),
it is recommended to use SQLGetData since you can get the data in chunk, instead of pre-allocating a large buffer. However, if the amount of data is
not so large, you may use the datatype "varbinary(2048)
I can reorder the select columns in the query and use both sqlcolBind and sqlgetdata. But i am not very much convinced to do this.
From this stack overflow link pre-determining-the-length
It is suggested to either allocate more memory than you need or I had to issue two SELECT statements, one is query having larger datatype and other query to get the actual size of column using DATALENGTH, something like
select name, description from users where username = 'johnce'
select DATALENGTH(description) from description where username = 'johnce'
I am very much confused with all these information. I could find any example showing my scenario. Could someone advice which is the best approach in my scenario.
Thanks in advance.
I´m working for the first time with OCI so this may be a basic question.... I´m coming from MySql word.... Using VS2012 with C++.
I wish to do a simple SELECT statement with some variations on WHERE and LIMIT clause. The SQL query is build dynamically from a C++ written processor and the statement comes ready from this module. So I may have something like:
SELECT * FROM MYTABLE3; or
SELECT F1, F2, F3 FROM MYTABLE1; or even
SELECT F1, F3, F4 FROM MYTABLE2 WHERE ID > 10;
No big deal here.
My problem is that I DON´T KNOW IN ADVANCE THE TABLE FORMAT, so I cannot bind variables to it before executing the statement and fetching the table structure. In MySql that´s easy, because I execute the statement and I get the resultSet. From the resultSet I can check the number of columns retrieved, the name, data format and size of each column. After reading that data I build a dynamic matrix with the table structure and its data, my final goal. Something as:
sql::ResultSetMetaData *resultMeta = resultSet->getMetaData();
while (resultSet->next())
{
for (unsigned int i = 1; i <= resultMeta->getColumnCount(); i++)
{
std::string label = resultMeta->getColumnLabel(i);
std::string type = resultMeta->getColumnTypeName(i);
// ... Get the resultset attributes and data
}
retData.push_back(data);
}
From what I´ve seen in Oracle, I need to bind the variables that are going to be returned before issuing the execute/fetch operations. In my case I cannot do it because I don´t know the table structure in advance...
I´m pretty sure Oracle can do that, I just don´t know how to do it. I´ve read the Oracle Docs and did not find references to it....
Help is very much appreciated and code examples also. I´m stuck with that for 2 days now... Thanks for helping.
Can you please try the following on your statement handle ( stmhp). This will give you column count on your oracle statement.
err = OCIAttrGet ((dvoid *)stmhp, (ub4)OCI_HTYPE_STMT, (dvoid *)
&parmcnt, (ub4 *) 0, (ub4)OCI_ATTR_PARAM_COUNT, errhp);
Please check this link also which will help you to find out data type of every column in the resultset.
Retrieving data type information for columns in an Oracle OCCI ResultSet
So I want to make a table in MYSQL (using c++) with about 128 columns each on representing an INT.
I don't know the syntax to make a 129 column row (1 for id 128 for each int)
Kinda like an array: int myArray[128];
CREATE TABLE SIFTFEATUES(ID INT not null, myArray[128] INT) would be Ideal or something close
where I don't have to write out each column name.
To have a table with 128 columns defined, you need to "write out" each column name. Each column in the table gets a name, a datatype, and other optional attributes.
In order to retrieve data from the column, it has to be referenced by name; the same is true for inserting and updating the contents of the column.
(I put "write out" in double quotes, because it's very simple task to generate a text file of 128 lines of a table definition that vary only by name. (It's not necessary to type each line.)
, col001 int
, col002 int
, ...
, col128 int
Absent any other information about your use case, what you are trying to accomplish, it's nearly impossible to make any sort of recommendation.
A table should be use for data persistence, thus not to be used like an array (with obvious exceptions). With that said I see two scenarios:
1) You want to use it like a temporary data structure, which I strongly do not recommend, unless for medical reasons.
2) You want to keep that table, for which I'd use a text editor or even MS Excel and create a macro to create the CREATE TABLEstatement with your 128 columns.
In the application I develop, the database table having a column of 'image' datatype. Now I want to insert the values into that table directly from my program, which is in VC++ 6.0, using a direct ExecuteQuery() of INSERT(due to performance considerations). The image column is been mapped to the structurre of the following declaration
typedef struct _DB_BLOB
{
int size;
char *data;
}DB_BLOB;
The data part will be dynamically filled.
If you can construct a hex representation of a string, this insert statement seems to work okay:
create table #test
(
value image
)
insert #test (value)
values (0x48656C6C6F207468657265)
But there may be size limitations (on the size of the query).
It would be really really useful to be able to use managed code and ado.net. to set parameters.