I would like to generate records randomly and insert into postgresql using LibPQ in julia.
users table ddl:
CREATE TABLE IF NOT EXISTS practice.users(
id bigserial PRIMARY KEY,
created_at timestamp,
name text,
email text,
address text,
city text,
state text,
zip text,
birth_date text,
latitude float,
longitude float,
password text,
source text
);
Code in file gen_users.jl:
using LibPQ, DataStreams, DataFrames, Dates, Random;
global fnames = ["Oliver", "Jake", "Noah"];
global lnames=["Smith", "Murphy", "Smith"];
global esps = ["gmail", "yahoo", "outlook"];
global tlds = ["com"];
global cities = ["Montgomery", "Juneau", "Phoenix"];
global states = ["Alabama", "Alaska", "Arizona"];
host="localhost";
port="5432";
db="practice";
user="ydba";
pwd="ydba123";
function insert_user()
conn = LibPQ.Connection("host=$host dbname=$db user=$user password=$pwd");
for i in 1:1
###user_id::Int64
created_at_val = rand(DateTime("2019-01-01"):Second(1):DateTime("2020-01-20"));
fname = fnames[rand(1 : size(fnames)[1])];
lname = lnames[rand(1 : size(lnames)[1])];
name_val = fname * " " * lname;
email_val = fname * "." * lname * "#" * esps[rand(1 : size(esps)[1])] * "." * tlds[rand(1 : size(tlds)[1])];
address_val = "D No." * randstring('1':'9', 3) * ", " * rand(["Left", "Right", "A", "B", "C", "D"]) * " Wing, " * rand(["I", "II", "III"]) * " Floor, " * rand(["Daffodil", "Lotus", "Jasmine", "Rose", "Lily"]) * "Building, " * rand(["I", "II", "III"]) * " Avenue, " * randstring('A':'Z') * " Block, " * randstring('A':'Z') * " Sector, ";
city_val = cities[rand(1 : size(cities)[1])];
state_val = states[rand(1 : size(states)[1])];
zip_val = randstring('1':'9', 9);
birth_date_val = rand(DateTime("1919-01-01"):Day(1):DateTime("2000-05-30"));
latitude_val = rand(-90.0:90.0);
longtitude_val = rand(-180.0:180.0);
password_val = randstring(['A':'Z'; 'a':'z'; '0':'9'], 8);
source_val = randstring(['A':'Z'; 'a':'z'; '0':'9'], 8);
insert_query = "INSERT INTO practice.users (id, created_at, name, email, address, city, state, zip, birth_date, latitude, longitude, password, source) VALUES(nextval('practice.users_id_seq'), \$1, \$2, \$3, \$4, \$5, \$6, \$7, \$8, \$9, \$10, \$11, \$12)";
LibPQ.load!((created_at = created_at_val, name = name_val, email = email_val, address = address_val, city = city_val, state = state_val, zip = zip_val, birth_date = birth_date_val, latitude = latitude_val, longtitude = longtitude_val, password = password_val, source = source_val), conn, insert_query);
execute(conn, "COMMIT");
end
close(conn);
end
insert_user();
Error Logs:
[error | LibPQ]: ERROR: bind message supplies 1 parameters, but prepared statement "__libpq_stmt_0__" requires 12
ERROR: LoadError: ERROR: bind message supplies 1 parameters, but prepared statement "__libpq_stmt_0__" requires 12
Stacktrace:
[1] error(::Memento.Logger, ::String) at /opt/julia/julia-1.1.0/share/julia/stdlib/v1.1/packages/Memento/UgJr2/src/loggers.jl:429
[2] #handle_result#45(::Bool, ::Function, ::LibPQ.Result) at /opt/julia/julia-1.1.0/share/julia/stdlib/v1.1/packages/LibPQ/dhdTB/src/results.jl:175
[3] #handle_result at ./none:0 [inlined]
[4] #execute#62(::Bool, ::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::LibPQ.Statement, ::Array{Union{Missing, String},1}) at /opt/julia/julia-1.1.0/share/julia/stdlib/v1.1/packages/LibPQ/dhdTB/src/statements.jl:130
[5] (::getfield(LibPQ, Symbol("#kw##execute")))(::NamedTuple{(:throw_error,),Tuple{Bool}}, ::typeof(execute), ::LibPQ.Statement, ::Array{Union{Missing, String},1}) at ./none:0
[6] load!(::NamedTuple{(:created_at, :name, :email, :address, :city, :state, :zip, :birth_date, :latitude, :longtitude, :password, :source),Tuple{DateTime,String,String,String,String,String,String,DateTime,Float64,Float64,String,String}}, ::LibPQ.Connection, ::String) at /opt/julia/julia-1.1.0/share/julia/stdlib/v1.1/packages/LibPQ/dhdTB/src/tables.jl:166
[7] insert_user() at /root/gen_users.jl:169
[8] top-level scope at none:0
[9] include at ./boot.jl:326 [inlined]
[10] include_relative(::Module, ::String) at ./loading.jl:1038
[11] include(::Module, ::String) at ./sysimg.jl:29
[12] include(::String) at ./client.jl:403
[13] top-level scope at none:0
in expression starting at /root/gen_users.jl:175
Please help me in resolving the issue!
enclose the assigning values in brackets : I inserted brackets in your code and it worked pretty well for me.
LibPQ.load!((created_at = [created_at_val], .....
Related
We have an old but very large Silex application using a fork of Silex 2.2.5 and Symfony 3.4. We intend to migrate away from Silex, but the application is still under heavy development from multiple engineers, so in the meantime, we are looking get onto PHP 8.1 before the 7.4 EOL in November '22. We discovered we were not actually using the final version of Silex (2.3.0) which requires Symfony 4, and Symfony 4.4 supports PHP 8.1.
We have upgraded our dependencies to suit Silex 2.3.0, no problem there. All Symfony components are upgraded to 4.4. But when we load a page in the application, we get this error:
PHP Fatal error: Uncaught Error: Call to a member function has() on null in /var/task/vendor/symfony/http-kernel/EventListener/SessionListener.php:41
This emanates from a method called onKernelRequest. Which was introduced in Symfony 4.4. Debugging the page load, it seems that $this->container is null. I also found that the constructor for SessionListener is not called prior to hitting this error.
class SessionListener extends AbstractSessionListener
{
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function onKernelRequest(GetResponseEvent $event)
{
parent::onKernelRequest($event);
// ERROR IS HIT ON THE FOLLOWING LINE
if (!$event->isMasterRequest() || !$this->container->has('session')) {
return;
}
if ($this->container->has('session_storage')
&& ($storage = $this->container->get('session_storage')) instanceof NativeSessionStorage
&& ($masterRequest = $this->container->get('request_stack')->getMasterRequest())
&& $masterRequest->isSecure()
) {
$storage->setOptions(['cookie_secure' => true]);
}
}
Edit: Interestingly, if I remove this onKernelRequest method from Symfony's SessionListener, the site seems to work just fine. Not that this is a solution since it's in a Symonfy lib.
I've scoured the docs left behind on Silex to see if 2.3.0 required something more to work with Symfony 4, but since Symfony 4.3 and below didn't have this method, perhaps they had Silex working with an earlier version of Symfony 4.
Googling the error, I do see a bit of chatter about doing something with services.yml to inject the container dependency. But this project doesn't have a services.yml file so I'm unsure what the starting point would be for that.
Not expecting anyone to have answers to this issue, but ideas? Did anyone ever have Silex 2.3.0 working with Symfony 4.4 perhaps?
Full stacktrace of the error:
result = {array} [9]
0 = {array} [7]
file = "/var/task/vendor/symfony/event-dispatcher/EventDispatcher.php"
line = {int} 264
function = "onKernelRequest"
class = "Symfony\Component\HttpKernel\EventListener\SessionListener"
object = {OurCompany\Session\SessionListener} [3]
type = "->"
args = {array} [3]
1 = {array} [7]
file = "/var/task/vendor/symfony/event-dispatcher/EventDispatcher.php"
line = {int} 239
function = "doDispatch"
class = "Symfony\Component\EventDispatcher\EventDispatcher"
object = {Symfony\Component\EventDispatcher\EventDispatcher} [3]
type = "->"
args = {array} [3]
2 = {array} [7]
file = "/var/task/vendor/symfony/event-dispatcher/EventDispatcher.php"
line = {int} 73
function = "callListeners"
class = "Symfony\Component\EventDispatcher\EventDispatcher"
object = {Symfony\Component\EventDispatcher\EventDispatcher} [3]
type = "->"
args = {array} [3]
3 = {array} [7]
file = "/var/task/vendor/symfony/http-kernel/HttpKernel.php"
line = {int} 135
function = "dispatch"
class = "Symfony\Component\EventDispatcher\EventDispatcher"
object = {Symfony\Component\EventDispatcher\EventDispatcher} [3]
type = "->"
args = {array} [2]
4 = {array} [7]
file = "/var/task/vendor/symfony/http-kernel/HttpKernel.php"
line = {int} 81
function = "handleRaw"
class = "Symfony\Component\HttpKernel\HttpKernel"
object = {Symfony\Component\HttpKernel\HttpKernel} [4]
type = "->"
args = {array} [2]
5 = {array} [7]
file = "/var/task/vendor/silex/silex/src/Silex/Application.php"
line = {int} 496
function = "handle"
class = "Symfony\Component\HttpKernel\HttpKernel"
object = {Symfony\Component\HttpKernel\HttpKernel} [4]
type = "->"
args = {array} [3]
6 = {array} [7]
file = "/var/task/vendor/silex/silex/src/Silex/Application.php"
line = {int} 477
function = "handle"
class = "Silex\Application"
object = {OurCompany\Application} [9]
type = "->"
args = {array} [1]
7 = {array} [7]
file = "/var/task/lib/Application.php"
line = {int} 37
function = "run"
class = "Silex\Application"
object = {OurCompany\Application} [9]
type = "->"
args = {array} [1]
8 = {array} [7]
file = "/var/task/web/index.php"
line = {int} 43
function = "run"
class = "OurCompany\Application"
object = {OurCompany\Application} [9]
type = "->"
args = {array} [1]
You can't inject ContainerInterface anymore.
As I can see, you want to read the session data. To accomplish this, you can inject SessionInterface(for Symfony 4) or RequestStack (for Symfony 5+) instead of ContainerInterface.
Symfony 4 : https://symfony.com/doc/4.4/session.html
Symfony 5+ : https://symfony.com/doc/5.4/session.html
My working environment:
R version: 3.6.3 (64 bits)
OS: Windows 10 (64 bits)
I was working on the following exercice from Hadley Wickham's advanced R book:
Create a bank account R6 class that stores a balance and allows you to
deposit and withdraw money.
Here is the class that I created
library(R6)
BankAccount <- R6Class(
classname = "BankAccount",
public = list(
initialize = function(first_name,
last_name,
email,
balance
) {
stopifnot(balance >= 0)
self$balance <- balance
self$email <- email
self$first_name <- first_name
self$last_name <- last_name
},
deposit = function(amount) {
stopifnot(amount > 0)
self$balance <- self$balance + amount
invisible(self)
},
withdraw = function(amount) {
stopifnot(amount > 0, self$balance - amount > 0)
self$balance = self$balance - amount
invisible(self)
},
print = function() {
cat(
"first name: ",
self$first_name,
"\n",
"last name: ",
self$last_name,
"\n",
"email : ",
self$email,
"\n",
"current balance : ",
self$balance,
"\n"
)
invisible(self)
}
)
)
my_bank_account <- BankAccount$new(
first_name = "test_firstname",
last_name = "test_lastname",
email = "testemail#somedomain.com",
balance = 140
)
The above code raises the following error upon execution:
Error in self$balance <- balance (from #10) :
cannot add bindings to a locked environment
>
I cannot understand the problem in the initialize method of my class. What's wrong with the initialize function in my code?
Well, I had to read more carefully the R6Class documentation:
If the public or private lists contain any items that have reference
semantics (for example, an environment), those items will be shared
across all instances of the class. To avoid this, add an entry for that
item with a 'NULL' initial value, and then in the 'initialize' method,
instantiate the object and assign it.
The problem with my code was that I had declared fields only inside my constructor but apparently they had to be declared first, out of the initializer function, assigned by NULL, and then inside the initializer be assigned by the corresponding function arguments. So here is the correct and new version of my class
library(R6)
BankAccount <- R6Class(
classname = "BankAccount",
public = list(
## I had forgotten to write the
## following 4 lines in the previous
## version and apparently this caused
## the problem.
balance = NULL,
email = NULL,
first_name = NULL,
last_name = NULL,
##
##
initialize = function(first_name,
last_name,
email,
balance
) {
stopifnot(balance >= 0)
self$balance <- balance
self$email <- email
self$first_name <- first_name
self$last_name <- last_name
},
deposit = function(amount) {
stopifnot(amount > 0)
self$balance <- self$balance + amount
invisible(self)
},
withdraw = function(amount) {
stopifnot(amount > 0, self$balance - amount > 0)
self$balance = self$balance - amount
invisible(self)
},
print = function() {
cat(
"first name: ",
self$first_name,
"\n",
"last name: ",
self$last_name,
"\n",
"email : ",
self$email,
"\n",
"current balance : ",
self$balance
)
invisible(self)
}
)
)
my_bank_account <- BankAccount$new(
first_name = "test_firstname",
last_name = "test_lastname",
email = "testemail#somedomain.com",
balance = 140
)
print(my_bank_account)
And this time the code runs without any problem.
I'm using the httr to access the API for stockfighter, a CTF style trading game.
The GET function is working without any problems, but when I try and authenticate using an API key in the headers it doesn't appear to be working. Here's my place_order function
place_order <- function(acct, exchange, stock, price, qty,
direction = c("buy", "sell"),
type = c("limit", "market", "fill-or-kill",
"immediate-or-cancel")) {
# Place a stock order
if (!exists("key")) stop("No authorisation key defined.")
direction <- match.arg(direction)
type <- match.arg(type)
bdy <- list("account" = acct, "venue" = exchange, "symbol" = stock,
"price" = price, "qty" = qty, "direction" = direction,
"orderType" = type)
rurl <- paste(burl, "/venues/", exchange, "/stocks/", stock, "/orders",
sep = "")
r <- POST(rurl, body = bdy, add_headers(`X-Starfighter-Authorization` = key))
return(content(r))
}
This is what I get in return:
$ok
[1] FALSE
$error
[1] "invalid character '-' in numeric literal"
It appears that the JSON is not escaping the dashes correctly.
This is the response I get when I post to httpbin instead of the API:
$args
named list()
$data
[1] ""
$files
named list()
$form
$form$account
[1] "RB34256134"
$form$direction
[1] "buy"
$form$orderType
[1] "limit"
$form$price
[1] "12400"
$form$qty
[1] "100"
$form$symbol
[1] "FOOBAR"
$form$venue
[1] "TESTEX"
$headers
$headers$Accept
[1] "application/json, text/xml, application/xml, */*"
$headers$`Accept-Encoding`
[1] "gzip, deflate"
$headers$`Content-Length`
[1] "751"
$headers$`Content-Type`
[1] "multipart/form-data; boundary=------------------------49a2e51c0c6926dd"
$headers$Host
[1] "httpbin.org"
$headers$`User-Agent`
[1] "libcurl/7.43.0 r-curl/0.9.4 httr/1.0.0.9000"
$headers$`X-Starfighter-Authorization`
[1] "OBFUSCATED KEY HERE"
$json
NULL
$origin
[1] "1.125.48.185"
$url
[1] "http://httpbin.org/post"
I feel like this is probably a really stupid simple error but I can't work it out.
EDIT:
Here's the python method using requests and json that works perfectly.
def sf_post(path, data, key, **kwargs):
base_url = "https://api.stockfighter.io/ob/api/"
r = requests.post("%s/%s" % (base_url, path), data = data, headers = {'X-Starfighter-Authorization': key}, **kwargs)
return(r)
def order(self, price, qty, direction, order_type):
data = dict(account = self.account, venue = self.exchange, symbol = self.symbol, price = price, qty = qty,
direction = direction, orderType = order_type)
r = sf_post("%s/orders" % self.rurl, data = json.dumps(data), key = self.key)
return r.json()
cph = Stock("CPH", "EXMBEX", account = "ACCOUNTCODEHERE", key = os.environ.get("SF_KEY"))
cph.order(5000, qty = 100, direction = "buy", order_type = "limit")
{u'direction': u'buy', u'ok': True, u'ts': u'2016-01-24T00:35:21.148877285Z', u'fills': [{u'price': 4694, u'ts': u'2016-01-24T00:35:21.148881279Z', u'qty': 100}], u'originalQty': 100, u'orderType': u'limit', u'symbol': u'CPH', u'venue': u'EXMBEX', u'account': u'SSM90915021', u'qty': 0, u'id': 754, u'totalFilled': 100, u'open': False, u'price': 5000}
I thought I was probably missing something stupid, and as #hadley pointed out in the comments I was. I needed to add encode = "json" to my POST call. For posterity here's the updated function code:
place_order <- function(acct, exchange, stock, price, qty,
direction = c("buy", "sell"),
type = c("limit", "market", "fill-or-kill",
"immediate-or-cancel")) {
if (!exists("key")) stop("No authorisation key defined.")
direction <- match.arg(direction)
type <- match.arg(type)
bdy <- list("account" = acct, "venue" = exchange, "symbol" = stock,
"price" = price, "qty" = qty, "direction" = direction,
"orderType" = type)
rurl <- paste(burl, "venues/", exchange, "/stocks/", stock, "/orders",
sep = "")
r <- POST(rurl, body = bdy,
add_headers(`X-Starfighter-Authorization` = key),
encode = "json")
return(content(r))
}
Trying to display a users Lastname, Firstname --- Website
And I need to insert a comma and space after Lastname to a GridView.
I am trying to add a CASE statement in SQL and having trouble figuring it out.
Perhaps I need to use #parameter (scalar variable?) to abstract the
memory read from CASE statement; or my syntax is wrong and I just don't
understand.
SELECT
CASE
WHEN IsNull(people_Table.firstName, '') = ''
THEN CONCAT(people_Table.lastName, ', ', people_Table.firstName)
ELSE people_Table.lastName
END as fullName,
people_Table.website
FROM
people_Table
INNER JOIN
membership_Table on people_Table.ID = membership_Table.personID
WHERE
rectype = 'Master'
AND membershipType = 'Business'
AND expirationDate > GetDate()
ORDER BY
people_Table.lastName
Getting SQL Server error:
Msg 208, Level 16, State 1, Line 1
Invalid object name 'people_Table'.
Otherwise I suppose I should use an asp databoundevent in the template.
What is better for performance and security?
SELECT ISNULL(people_Table.lastName + ', ', '')
+ ISNULL(people_Table.firstName , '') as fullName
, people_Table.website
FROM people_Table INNER JOIN membership_Table on people_Table.ID =
membership_Table.personID
WHERE rectype = 'Master'
AND membershipType = 'Business'
AND expirationDate > GetDate()
ORDER BY people_Table.lastName
OR
SELECT COALESCE(people_Table.lastName + ', ', '')
+ COALESCE(people_Table.firstName , '') as fullName
, people_Table.website
FROM people_Table INNER JOIN membership_Table on people_Table.ID =
membership_Table.personID
WHERE rectype = 'Master'
AND membershipType = 'Business'
AND expirationDate > GetDate()
ORDER BY people_Table.lastName
I've a Table table_news containing these fields :
_ID = INTEGER PRIMARY KEY AUTOINCREMENT
ID = TEXT NOT NULL
CONTENT = TEXT NOT NULL
ZONE TEXT = NOT NULL
AREA TEXT = NOT NULL
TITLE TEXT = NOT NULL
DATE TEXT = NOT NULL
AUTHOR TEXT = NOT NULL
This table is filled like below:
Column Row 1 Value Row 2 Value
------------------------------------------------------
_ID 1 2
ID "5" "5"
CONTENT "Discover New York." "Discover New York."
ZONE "New York" "New York"
AREA "New York" "New York"
TITLE "Let's discover the world." "Let's discover the world."
DATE "2012-07-04" "2012-07-04"
AUTHOR "Henry Brakman" "Henry Brakman"
So I just want to make an SQL statement in order to delete one of these duplicated rows.
I tried this (line breaks added for ease of reading, command is written as single string):
public SQLiteStatement removeSameContentNews() {
return bdd.compileStatement("DELETE FROM table_news "
+ "WHERE _ID NOT IN (SELECT MIN( _ID) "
+ "FROM table_news "
+ "GROUP BY ID, CONTENT, ZONE, AREA, TITLE, DATE, AUTHOR)");
}
But this deleted nothing..
I finally used uniqueness with UNIQUE keyword and it's working properly:
private static final String CREATE_TABLE_NEWS =
"CREATE TABLE " + TABLE_NEWS + " (" + COLUMN_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_ID
+ " TEXT NOT NULL UNIQUE, " + COL_CONTENT
+ " TEXT NOT NULL, " + COL_ZONE + " TEXT NOT NULL, "
+ COL_AREA + " TEXT NOT NULL, " + COL_TITLE
+ " TEXT NOT NULL, " + COL_DATE + " TEXT NOT NULL, "
+ COL_AUTHOR + " TEXT NOT NULL);";
You can use this example of query:
DELETE FROM table WHERE _ID NOT IN (
SELECT MIN( _ID)
FROM table
GROUP BY ID, CONTENT, ZONE, AREA, TITLE, DATE, AUTHOR
)
EDIT
Alternatively you could use :
DELETE FROM table_news WHERE EXISTS
(SELECT 1 FROM table_news t
WHERE t.ID=table_news.ID AND
t.ID=table_news.CONTENT AND
t.ID=table_news.ZONE AND
t.ID=table_news.AREA AND
t.ID=table_news.TITLE AND
t.ID=table_news.DATE AND
t.ID=table_news.AUTHOR AND
t.rowid > table_news.rowid)