I've created a connection, and tried to execute a little sql into it ; im getting error (1046): no database selected
using DBInterface
using MySQL
con = DBInterface.connect(MySQL.Connection, "12345.rds.amazonaws.com", "admin", "54321")
sql = "CREATE TABLE friends2(last_name VARCHAR(10), first_name VARCHAR(20));"
DBInterface.execute(con, sql)
>> (1046): No database selected
When i run DBInterface.connect(MySQL.Connection, "12345.amazonaws.com", "admin", "54321") i get :
MySQL.Connection(host="12345.rds.amazonaws.com", user="admin", port="3306", db="")
but if i enter the db when making the MySQL.Connection, i get (1049): Unknown database 'pg1'
I also notice that even though i have run add MySQL, when i try to use some thing like mysql_connect, it doesnt work :
conn = mysql_connect( "12345.us-east-2.rds.amazonaws.com", "admin", "54321", "db")
>>> connect not defined
not sure where i messed up!
I'm pretty sure you have to pass ; db="my_db" to DBInterface.connect
Related
I am building a shiny application which will allow CRUD operations by a user on a table which exists in an sqlite3 database. I am using the input$table_rows_selected() function in DT to get the index of the rows selected by the user. I am then trying to delete the rows (using an action button deleteRows) from the database which have a matching timestamp (the epoch time stored as the primary key). The following code runs without any error but does not delete the selected rows.
observeEvent(input$deleteRows, {
if(!is.null(input$responsesTable_rows_selected)){
s=input$responsesTable_rows_selected
conn <- poolCheckout(pool)
lapply(length(s), function(i){
timestamp = rvsTL$data[s[i],8]
query <- glue::glue_sql("DELETE FROM TonnageListChartering
WHERE TonnageListChartering.timestamp = {timestamp}
", .con = conn)
dbExecute(conn, sqlInterpolate(ANSI(), query))
})
poolReturn(conn)
# Show a modal when the button is pressed
shinyalert("Success!", "The selected rows have been deleted. Refresh
the table by pressing F5", type = "success")
}
})
pool is a handler at the global level for connecting to the database.
pool <- pool::dbPool(drv = RSQLite::SQLite(),
dbname="data/compfleet.db")
Why does this not work? And if it did, is there any way of refreshing the datatable output without having to reload the application?
As pointed out by #RomanLustrik there was definitely something 'funky' going on with timestamp. I am not well versed with sqlite but running PRAGMA table_info(TonnageListChartering); revealed this:
0|vesselName||0||0
1|empStatus||0||0
2|openPort||0||0
3|openDate||0||0
4|source||0||0
5|comments||0||0
6|updatedBy||0||0
7|timestamp||0||1
8|VesselDetails||0||0
9|Name||0||0
10|VslType||0||0
11|Cubic||0||0
12|DWT||0||0
13|IceClass||0||0
14|IMO||0||0
15|Built||0||0
16|Owner||0||0
I guess none of the variables have a data type defined and I am not sure if that's possible to do it now. Anyway, I changed the query to ensure that the timestamp is in quotes.
query <- glue::glue_sql("DELETE FROM TonnageListChartering
WHERE TonnageListChartering.timestamp = '{timestamp}'
", .con = conn)
This deletes the user selected rows.
However, when I am left with only one row, I am unable to delete it. No idea why. Maybe because of a primary key that I have defined while creating the table?
I am using the in_schema() function from dbplyr package to create a table in a named schema of a postgresql database from R.
It is not a new piece of code and it used to work as expected = creating a table called 'my_table' in schema 'my_schema'.
con <- dbConnect(odbc::odbc(),
driver = "PostgreSQL Unicode",
server = "server",
port = 5432,
uid = "user name",
password = "password",
database = "dbase")
dbWriteTable(con,
in_schema('my_schema', 'my_table'),
value = whatever) # assume that 'whatever' is a data frame...
This piece of code has now developed issues and unexpectedly started to create a table called 'my_scheme.my_table' in the default public scheme of my database, instead of the expected my_schema.my_table.
Has anybody else noticed such behaviour, and is there a solution (except using the default postgresql scheme, which is not practical in my case)?
for that, I would recommend using copy_to() instead of dbWriteTable(): copy_to(con, iris, in_schema("production", "iris"))
I'm running some queries, that print runtime stats from their execution.
It's done through
print('message')
used within the sql script.
I would want to see these messages while calling the procedures/scripts through pymssql.
conn = pymssql.connect(server, user, password, "tempdb")
cursor = conn.cursor()
cursor.execute("print('message')")
conn.commit()
Above script doesn't return anything, and I can't find any tips on how to get that print to show up in the console output.
Found a solution that let's me still use pymssql and get the print messages.
pymssql.Connection actually uses _mssql.MSSQLConnection internally.
This means that you can use this example by accessing that internal object.
connection = pymssql.connect(server='server_address', database='db_name')
connection._conn.set_msghandler(my_msg_handler) # Install our custom handler
where the my_msg_handler is the same type of object as in pmssql wiki.
Accessing internal objects is not ideal, but it's the only way I've found if you don't want to use a different library and need to get the SQL prints.
I don't believe there is a way, but you can refactor your SQL. For example:
DECLARE #my_var AS VARCHAR(200)
PRINT 'Setting a variable...'
SET #my_var = 'this'
PRINT 'I am some output'
SELECT * FROM my_table WHERE this = #my_var
Could be refactored to be something like this:
DECLARE #my_var AS VARCHAR(200)
DECLARE #messages AS VARCHAR(MAX)
SET #messages = #messages + 'Setting a variable...'
SET #my_var = 'this'
SET #messages = #messages + 'I am some output'
SELECT #messages, * FROM my_table WHERE this = #my_var
Good luck!
In order to print something into the console in pymssql, you don't need to put the print inside the execute function. you can simply use
print("message")
so your code will be
conn = pymssql.connect(server, user, password, "tempdb")
cursor = conn.cursor()
print("message")
conn.commit()
In monetdb I have set up a schema main and my tables are created into this schema.
For example, the department table is main.department.
With dplyr I try to query the table:
mdb <- src_monetdb(dbname="model", user="monetdb", password="monetdb")
tbl(mdb, "department")
But I get
Error in .local(conn, statement, ...) :
Unable to execute statement 'PREPARE SELECT * FROM "department"'.
Server says 'SELECT: no such table 'department'' [#42S02].
I tried to use "main.department" and other similar combinations with no luck.
What is the appropriate syntax?
There is a somewhat hacky workaround for this: We can manually set the default schema for the connection. I have a database testing, in there is a schema foo with a table called bar.
mdb <- src_monetdb("testing")
dbSendQuery(mdb$con, "SET SCHEMA foo");
t <- tbl(mdb, "bar")
The dbplyr package (a backend of dplyr for database connections) has a in_schema() function for these cases:
conn <- dbConnect(
MonetDB.R(),
host = "localhost",
dbname = "model",
user = "monetdb",
password = "monetdb",
timeout = 86400L
)
department = tbl(conn, dbplyr::in_schema("main", "department"))
How can I get the server name using query in Teradata?
That is, if I am writing a query on the 'dev' server, it should return the dev server name.
for example, in Sybase : we will be using select ##servername.
There's nothing like ##servername in TD.
You might create a SQL UDF on each server returning the name, e.g.
REPLACE FUNCTION syslib.#servername ()
RETURNS VARCHAR(30)
LANGUAGE SQL
CONTAINS SQL
DETERMINISTIC
RETURNS NULL ON NULL INPUT
SQL SECURITY DEFINER
COLLATION INVOKER
INLINE TYPE 1
RETURN 'dev'
If it's created in syslib it can be accessed without qualifying it like this:
SELECT #servername();
SELECT CASE
WHEN LogonSource LIKE '%UAT%' THEN 'UAT'
WHEN LogonSource LIKE '%PROD%' THEN 'Prod'
ELSE 'Unknown'
END DatabaseName
FROM DBC.SessionInfoV
WHERE UserName = 'myname';
This will give you information close to ##servername.
select ClientTdHostName, ServerIPAddrByServer, ServerPortByServer
from DBC.SessionInfo where SessionNo=Session;
Validated against 17.xx TD.