I wonder if there is query length limitation for sqlQuery function from RODBC package? I can't find any information in documentation but that's my hypothesis.
The thing is that when I use Hive editor to write a query I get correct output for my queries (including the long one). But when I connect through the sqlQuery() the long query (~250) doesn't work. I get error:
[RODBC] ERROR: Could not SQLExecDirect 'my long query here'
I can't think about different reason why the query works in Hive editor and does not work with sqlQuery().
I know 250 lines is a lot for SQL query but unfortunately I don't have permission to create temporary table in database I work with, so I do it in my code and that's why the code is so long. There's no way I can shorten the code.
Any help appreciated.
Related
I often use RODBC to work with MS Access files in R. For removing existing tables sqlDrop works fine, e.g.:
db <- odbcConnectAccess(choose.files(caption="select database"))
sqlDrop(db, "existing_dummy_table")
What I need to do now is to delete an existing query that is stored in the Access database. sqlDrop does only seem to work with tables, not with querys.
sqlDrop(db, "existing_dummy_query")
brings up:
Error in odbcTableExists(channel, sqtable, abort = errors) :
‘existing_dummy_query’: table not found on channel
Is there any solution how to delete/remove existing queries?
Thank you!
After a lot of testing I found a soultion myself:
sqlQuery(db, "DROP TABLE existing_dummy_query";)
Maybe its helpful for others. DROP VIEW did not work. I don't know why.
first time poster here. I’m using R to ‘automate’ some Teradata (TD) SQL scripts. The ones that return data are working great. But I have an UPDATE SQL statement that only returns a message in TD like ‘xxx rows updated’. I’m using the RODBC package in R for my connection. When I use ‘sqlQuery’ and send the ‘update’ SQL statement to TD from R, I get nothing back, whether successful or not. I know only data returning is normal here, but what I want is to get that message back from TD to R. Then I can continue to ‘automate’ things based on the message. Is there a way, either in putting something in the SQL code at the end, or afterwards with R, to get this ‘successful’ message back?
Package RODBC has “odbcGetErrMsg”, but it doesn’t work on the success message. The only workaround I can think of is to do a count() before the update, then send the update statement, then count() after to get a number of rows changed. This may work, but I’d like to get the message instead. I’ve searched SO & Googled this with no luck. Any ideas of how to get a successful TD message from an update statement sent from R returned back in R please?
I'm running into an error trying to use a SQLite prepared statement:
create table RawRecord (?, ?, ?);
Calling sqlite3_prepare16_v2 gives me this error: SQLITE_ERROR: SQLITE_ERROR[1]: near "?": syntax error
I don't run into problems with prepared statements anywhere else (and have been using SQLite for many years). I have tried to find whether prepared statements are simply not allowed for CREATE TABLE, but haven't found anyone saying that's the case.
If I build the create string manually and embed my column names, it works. I prefer to use prepared statements simply because it makes things like quotes cleaner, and in this case the column names come from user data, so I don't know what they will be.
I can certainly work around this, but was hoping to understand why this is an error.
Help?
I'm using RJDBC to connect to a local database. This allows me to make SELECT queries easily using dbGetQuery, and CREATE TABLE using dbWriteTable.
However, I cannot figure out a method to DROP TABLE or DELETE or SELECT INTO directly from my R console. These things work when I do it directly in SQL Developer, but not when I pass the query onto the database from R.
How do I perform database record manipulations which are not SELECT statements using R?
I'd try using a different type instead.
dbGetQuery bases itself on finding and iterating over the DB rather than manipulating it's records.
Similar questions were asked before;
I couldn't find a nice R example, but if it helps, A nice java example could be found here:
EDIT:
I found the type I was talking about! Took me a while, anyhow - sqlQuery allows you to run pretty much any query, that is - a change in the DB records. Example I modified from this source:
res <- sqlQuery(con1,"DELETE TABLE TESTDATA", errors=FALSE)
# res will now hold the result of the query.
# -1 means error, otherwise iteration is sucessful, and it will hold the number of rows affected.
if (res == -1){ #if something messed up
cat ("An error has occurred.\n")
msg <- odbcGetErrMsg(con1) #Use your connection for this.
print (msg)
} else {
cat ("Table was deleted successfully.\n")
}
EDIT 2:
I got it confused with RODBC, however there's no reason to worry, since I found the RJDBC alternative as well! It's called, dbSendUpdate. Example:
# Assuming you have the connection saved as conn; these example shows how to use dbSendUpdate to create tables and insert values.
# You could use it with every non-selective query, that is, which manipulates the record (update,delete,insert,drop etc.)
# create table, with dbSendUpdate:
dbSendUpdate(conn, "CREATE TABLE foo(a INT,b VARCHAR(100))")
# insert value, bind parameters to placeholders in statement:
dbSendUpdate(conn, "INSERT INTO foo VALUES(?,?)", 42, "bar")
# feel free to modify the query itself, these are just example values.
this is similar to another answered question here
basically dbGetQuery() as it name implies is used to send queries and recive their result.
if you want to send a general statement to the db like 'drop table' etc.
you can use:
dbSendUpdate(connection_object, "drop table table_name")
Is there a way to determine if a MATCH query sent to an fts3 table in sqlite is valid? Currently, I don't find out if the expression is invalid until I try to run it, which makes it a little tricky. I'd like to report to the user that the syntax is invalid before even trying to run it.
I'm using sqlite with the C api.
Additionally, doing a search with the expression "NOT " will fail with a "SQLlite logic/database error". Googling seems to indicate that such a query is invalid. Is there a correct syntax to do the operation? I'm essentially trying to find entries that do NOT contain that term. Or do I have to drop back down to using LIKE and do a sequential scan and not use FTS?
Looks like the simplest way right now is to create a separate FTS table with no rows, and execute the query against it. It will be immediate since there's no data in the table, and will report and error if the query syntax is incorrect.