I am new to SQL. is it possible with use of some library in c++to execute sql commands like
std::shared_ptr<database> ptr( new odb::sqlite::database ("database.db",
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
//the above line contains the pointer and the lower line contains the function that executes the query on the databse with the pointer
executesql("select * from tablea where x> y ", ptr);
becasue what is happening with me is that i get sql queries from another machine in the "select from etc..." format and i want to execute it in a similar fashion without going into parsing stuff and writing new code to do so.
I finally found the answer.
its given in a lot of detail at the following link.
http://www.codeproject.com/Tips/378808/Accessing-a-SQLite-Database-with-Cplusplus.
Related
We have a situation where we are dealing with a relational source(Oracle). The system is developed in a way where we have to first execute a package which will enable data read from Oracle and user will be able to get results out of select statement. I am trying to find a way on how to implement this in informatica mapping.
What we tried
1. In PreSQL we tried to execute the package and in SQL query we wrote select statement - data not getting loaded in target.
2. In PreSQL we wrote a block in which we are executing the package and just after that(within same beging...end block) we wrote insert statement on top of select statement - This is inserting data through insert statement however I am not in favor of this solution as both source and target are dummy which will confuse people in future.
Is there any possibility to implement this solution somehow by using 1st option.
Please help and suggest.
Thanks
The stored procedure transformation is there for this purpose configure it to execute source pre load
Pre-Sql and data read are not a part of same session. From what I understand, this needs to be done within the same session as otherwise the read is granted only for the session.
What you can do, is create a stored procedure/package that will grant read access and then return the data. Use it as a SQL Override on your SQ. This way SQ will read the data as usual. The concept:
CREATE PROCEDURE ReadMyData AS
BEGIN
execute immediate 'GiveMeTheReadAccess';
select * from MyTable;
END;
And use the ReadMyData on the Source Qualifier.
I have this PostgreSQL PL/pgSQL function:
CREATE OR REPLACE FUNCTION get_people()
RETURNS SETOF people AS $$
BEGIN
RETURN QUERY SELECT * FROM people;
END;
$$ LANGUAGE plpgsql;
Then I try to read the data in an application using SOCI, with this code:
session sql {"postgresql://dbname=postgres"};
row person {};
procedure proc = (sql.prepare << "get_people()", into(person));
proc.execute(true);
I would expect that person have the data of the first person, but it contains only one column with the name of the stored procedure (i.e., "get_people").
So I don't know what I am doing wrong here, or not doing. Is it the PL/pgSQL code or the SOCI code? Maybe SOCI does not support dynamic binding for stored procedures. Also, this method would allow me to read the first row only, but what about the rest of rows? I know SOCI comes with the rowset class for reading result sets, but the documentation says it only works with queries. Please help.
SELECT get_people() will return a single column, of type people, named after the procedure.
SELECT * FROM get_people() will give you the expected behaviour, decomposing the people records into their constituent fields.
Judging from the source, it looks like the SOCI procedure class (or at least, its Postgres implementation) is hard-wired to run procedures as SELECT ... rather than SELECT * FROM ....
I guess this means you'll need to write your own query, i.e.:
statement stmt = (sql.prepare << "SELECT * FROM get_people()", into(person));
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
I have been searching for a while on how to get the generated auto-increment ID from an "INSERT . INTO ... (...) VALUES (...)". Even on stackoverflow, I only find the answer of using a "SELECT LAST_INSERT_ID()" in a subsequent query. I find this solution unsatisfactory for a number of reasons:
1) This will effectively double the queries sent to the database, especially since it is mostly handling inserts.
2) What will happen if more than one thread access the database at the same time? What if more than one application accesses the database at the same time? It seems to me the values are bound to become erroneous.
It's hard for me to believe that the MySQL C++ Connector wouldn't offer the feature that the Java Connector as well as the PHP Connector offer.
An example taken from http://forums.mysql.com/read.php?167,294960,295250
sql::Statement* stmt = conn->createStatement();
sql::ResultSet* res = stmt->executeQuery("SELECT ##identity AS id");
res->next();
my_ulong retVal = res->getInt64("id");
In nutshell, if your ID column is not an auto_increment column then you can as well use
SELECT ##identity AS id
EDIT:
Not sure what do you mean by second query/round trip. First I thought you are trying to know a different way to get the ID of the last inserted row but it looks like you are more interested in knowing whether you can save the round trip or not?
If that's the case, then I am completely agree with #WhozCraig; you can punch in both your queries in a single statement like inser into tab value ....;select last_inserted_id() which will be a single call
OR
you can have stored procedure like below to do the same and save the round trip
create procedure myproc
as
begin
insert into mytab values ...;
select last_inserted_id();
end
Let me know if this is not what you are trying to achieve.
I am trying to insert a large number of records into a SQLite database. I get the above error if I try to use the sqlite3_exec C-API.
The code looks like this:
ret = sqlite_exec(db_p,".import file.txt table", NULL, NULL, NULL);
I know that the .import is command line, but can there be any way that you can do a extremely large insert of records that takes minimal time. I have read through previous bulk insert code and attempted to make changes but these are not providing the desired results.
Is there not a way to directly insert the string into the tables without having intermediate API's being called?
.import is most probably not available via the API. However there's one crucial thing to speed up inserts: wrap them in a transaction.
BEGIN;
lots of insert statements here;
COMMIT;
Without this, sqlite will need to write to the file after each insert to keep the ACID principle. The transaction let's it write to file later in bulk.
The answer to the syntax error could well be, that your strings are not enclosed in quotes in your SQL statement.