Julia Querying from SQLite - julia

Following this tutorial https://www.youtube.com/watch?app=desktop&v=qUrtLJcehE0, I created a database called Movies. Within the database a table called movies was created and next an entry was also added,
using SQLite
db = SQLite.DB("Movies")
SQLite.execute(db,"CREATE TABLE IF NOT EXISTS movies(movie_id REAL,movie_name TEXT, location TEXT)")
SQLite.execute(db,"INSERT INTO movies (movie_id,movie_name,location) VALUES(1,'Avengers','USA')")
However now when I try to Query the entry as follows,
SQLite.Query(db, "SELECT * from movies")
I get the this error, Error: MethodError: no method matching SQLite.Query.(::SQLite.DB,::String).
Any Ideas what I am doing wrong?

I don't know SQL, but I think you want to use SQLite.execute again not SQLite.Query. SQLite.Query is a struct not a function, and it doesn't have any documentation. I don't think you are meant to call it externally. Further documentation is here.
Method error means you are calling something with the wrong arguments. The SQLite.Query struct expects all of the following arguments:
struct Query
stmt::Stmt
status::Base.RefValue{Cint}
names::Vector{Symbol}
types::Vector{Type}
lookup::Dict{Symbol, Int}
end
The SQLite.execute function expects arguments in one of these forms:
SQLite.execute(db::SQLite.DB, sql, [params])
SQLite.execute(stmt::SQLite.Stmt, [params])
By convention in Julia, functions are all lowercase and types are capitalized.

To load a table using SQLite package,
using SQLite
using DataFrames
# Make a connection
db = SQLite.DB("Movies")
# To find out all tables available in schema
tbls = SQLite.tables(db)
# To load a specific table (movies table from Movies.db)
q = "SELECT * FROM movies"
data = SQLite.DBInterface.execute(db,q)
# To get as a dataframe
df = DataFrames.DataFrame(data)

Related

How to migrate Presto `map` function to hive

Presto map() function is quite a bit easier to use than hive. A presto map() invocation takes two lists: first one for the keys second for the values
A hive map() takes a varargs variable length parameter set of alternating key,values.
Here is a query snippet that I need to migrate (backwards?) from presto to hive:
, map(
concat(map_keys(decision_feature_importance), array['id_queue', 'queue_disposition']),
concat(map_values(decision_feature_importance), array[CAST(id_queue AS VARCHAR), queue_disposition])) other_info
The core of it is that the map() accepts two parallel arrays. But hive objects rather strongly to that. What is the pattern to [reverse- ?] migrate the map() ?
There are several questions about zipping lists in hive: e.g hive create map or key/value pair from two arrays They are pretty complicated, may involve UDF's (that I do not have ability to create) or libraries (brickhouse) that I do not have ability to install (shared cluster for hundreds of users). Also they constitute only a portion of the problem here.
The following toy query shows how to build the hive format map entries from two parallel lists. Basically we need to zip the lists manually - since there is no such builtin function for hive.
Hive partial equivalent
with mydata as (
select 1 id, map('key11','val11','key12','val12','key13','val13') as mymap
union all
select 2 id, map('key21','val21','key22','val22','key13','val13') as mymap
)
select split(concat_ws(',',collect_list(concat(key,',',value ))),',') keyval from (
select * from mydata lateral view outer explode (mymap) m
) d;

Send multiple parameters to SQL Server using dbBind in R

I am working on a program that has to send data to Microsoft SQL Server through R. Since SQL Server doesn't have any INSERT OR IGNORE INTO like PostgreSQL, I have to check if the value is already in the table. However, I don't know if I'm doing it right. How do I send multiple parameters (or the same parameter twice) into SQL Server using dbBind?
Here is my code:
statement <- "IF NOT EXISTS (SELECT * FROM dbo.nodes WHERE node_id=?) INSERT INTO dbo.nodes (node_id) VALUES (?)"
insertnew <- dbSendQuery(conn, statement)
test_p1 <- list(5, 6)
test_p2 <- list(test_p1, test_p1)
dbBind(insertnew, params=test_p2)
When I run it it gives me the following error:
Error in result_bind(res#ptr, params, batch_rows) :
RAW() can only be applied to a 'raw', not a 'double'
Using RStudio's Show Traceback feature, it shows this:
result_bind(res#ptr, params, batch_rows)
.local(res, params, ...)
dbBind(insertnew, params = test_p1)
Does anybody know what I'm doing wrong here? If the parameters should be something other than a list of lists, what should they be? Is there a way to name parameters to use twice in the same statement? What is RAW() and what is it trying to apply it to?
dbo.nodes is a table with one column, node_id NVARCHAR(1000) NOT NULL PRIMARY KEY.
conn is a DBIConnection object created earlier using dbConnect(odbc::odbc(), ...). It works for executing other statements so there shouldn't be a problem there.
Adding the argument batch_rows=1 to dbBind doesn't change anything (not that I understand what that is supposed to do).
Thanks in advance for the help!
test_p1 = c(5, 6)
test_p2 = unname(data.frame(I(test_p1), I(test_p1)))
In case this helps someone looking this up later, changing it from a list of lists to a data frame made from vectors worked.

Capture result output of send query in RSQLite

I would like to log the the number of rows changed when doing an update to a table in SQLite using RSQLite.
This:
dbSendQuery(db, "UPDATE FOO SET BAR = 1 WHERE BAZ = 0)
Returns an S4 object:
<SQLiteResult>
SQL UPDATE FOO SET BAR = 1 WHERE BAZ = 0
ROWS Fetched: 0 [Complete]
Changed: 11965
When I store the S4 object, I can call most of the content it seems (for example example_S3#sql provides the pure SQL code), but I can't get at the "Changed" bit (see above), which I'd like to log.
Any help is appreciated, limited SQL experience I'm afraid.
According to the doc you want the dbGetRowsAffected function
The number of rows affected
This function returns the number of rows that were added, deleted, or
updated by data modifying query. For a selection query, this function
returns 0.

Haskell SQLite3 query all

Would like to determine the size of a table (length) to avoid a user entering an incremental ID that already exists (Note: left all Haskell books to get my feet wet). Got a table with id, task, date_in, date_out and need to count the number id's. Using this function:
checkUniqueID = do
conn <- open "taglist.db"
len <- query conn "SELECT * FROM task;" (Only xxx) :: IO [id]
let showLength = length $ len
print showLength
close conn
return ()
However: omitting the 'Only' will result in ghci mentioning too few arguments, but I really cannot figure out what to put at xxx. Too bad all Haskell doc is so fragmented, since it is such a cool language.
Thanks guys!
I am not terribly familiar with these libraries. I check the docs
of Database.PostgreSQL.Simple
and found out that, if you don't have arguments to your query, you can use query_ and omit the argument tuple. The examples at the very beginning of the docs are quite helpful.
That being said, your approach looks wrong. You are pulling the whole table from the DB only to count the entries. This is a waste: you should instead use something like select count(*) from .. so that you query for the length only.
This might work: (again, I've never used that library)
[Only len] <- query_ conn "select count(*) from task" :: IO [Only Int]

Passing Optional Arguments in Classic ASP for Stored Procedure

I'm trying to run some stored queries in an Access database from an ASP page. I'd like to use an ADO Command object to run the procedure instead of simply sending a string to the database to execute. My trouble is occurring when I try to create the parameters to send to the stored procedure. I'm using the 'CreateParameter' method of the Command object. It takes 5 optional arguments, of which I only want to use two; name and value (arguments 1 and 5 respectively).
I've tried the following approaches to set up the parameters:
1) Using named arguments
command.Parameters.Append command.CreateParameter name:="name", value:="value"
2) Using named arguments inside brackets
command.Parameters.Append command.CreateParameter(name:="name", value:="value")
3) Leaving out optional parameters I don't need
command.Parameters.Append command.CreateParameter("name", , , , "value")
What is a simple way to achieve what I'm trying to do, and why does my syntax fail in these cases? I'm clearly missing something here!
VBScript doesn't support named arguments with or without brackets. All parameters required to pass (specific or blank) for CreateParameter method of the Command object.
For the section 3 in the question, have a look:
CreateParameter(
name`[optional], <- fits ("name")
type [optional, default = adEmpty], <- NOT fits (type is not empty)
direction[Optional default = adParamInput], <- fits (blank)
size [optional default = 0], <- NOT fits (at least 5 for "value")
value[optional] <- fits ("value")
)
So, you should specify at least type and size for a text valued parameter. Direction is adParamInput already.
Const adVarChar = 200 ' From adovbs.inc or Ado TypeLib
command.Parameters.Append command.CreateParameter("name", adVarChar, , 5, "value")
here is an example that can be followed.
http://www.webconcerns.co.uk/asp/accessqueries/accessqueries.asp
see also
http://bytes.com/topic/asp-classic/answers/628368-running-access-stored-queries-parameters
Remember access uses stored queries not stored procedures so some differences apply. For additional information search for
ASP with Access stored queries using parameters - .net

Resources