无论在iex>或使用mix run -e "My.code"当我运行使用外生混合项目中,外生的调试机制显示像下面一堆sql语句的
16:42:12.870 [debug] SELECT a0.`id` FROM `account` AS a0 WHERE (a0.`account_name` = ?) ["71000000313"] (39.6ms)`
...
Run Code Online (Sandbox Code Playgroud)
当我不再需要调试输出时,如何关闭它,我找不到任何关于如何更改ecto日志级别的东西.
提前致谢.
我想使用Elixir Ecto中的关键字"between"创建一个SQL.
我知道如何使用创建一个SQL like
where: like(t.descript, ^some_description)
但是,当我尝试以同样的方式做到这一点 like
where: between(t.start_date, ^start_date, ^end_date),
我收到了"无效"错误信息
** (Ecto.Query.CompileError) `between(t.start_date(), ^start_date, ^end_date)` is not a valid query expression.**
Run Code Online (Sandbox Code Playgroud)
我怎么能以正确的方式做到这一点?
提前致谢!!
我想使用HTTPoison Library在Elixir中执行以下命令.
$ curl -X DELETE -H "expired: 1442395800" -H "signature: ******************" -d '{"api_key":"***************","delete_path":"*******","expired":"****"}' https://cdn.idcfcloud.com/api/v0/caches
{"status":"success","messages":"We accept the cache deleted successfully."}
Run Code Online (Sandbox Code Playgroud)
当我检查的文件如何DELETE在HTTPoison
def delete!(url, headers \\ [], options \\ []), do: request!(:delete, url, "", headers, options)
Run Code Online (Sandbox Code Playgroud)
只有url和header需要.那么我应该把请求体(json体)放在curl哪里?
在Elixir,我试过了
req_body = "{\"api_key\":\"#{api_key}\",\"delete_path\":\"#{delete_path}\",\"expired\":\"#{expired}\"}"
url = "https://cdn.idcfcloud.com/api/v0/caches"
response = HTTPoison.delete!(url, header, [req_body])
Run Code Online (Sandbox Code Playgroud)
但似乎行不通.有人能说出正确的方法吗?
当我在Elixir中使用ecto创建查询时,我不确定如何在'where'子句中比较时间.
在模式部分我声明create_at为:datetime
schema "tenant" do
field :id, :integer
field :created_at, :datetime
# timestamps([{:inserted_at,:created_at}])
end
Run Code Online (Sandbox Code Playgroud)
而查询部分就像
def sample_query do
query = from t in Tenant,
where: t.id == 123,
where: t.created_at == %Ecto.DateTime{{2015, 4, 27}, {10, 8, 42}},
select: t
end
Run Code Online (Sandbox Code Playgroud)
好像是
where: t.created_at <= %Ecto.DateTime{{2015, 4, 27}, {10, 8, 42, 0}},
部分形式错误.有人可以告诉我如何以正确的方式做到这一点吗?
PS:关于如何定义字段create_at,下面的链接给了我答案
我想使用HTTPoison库在Elixir中创建一个Github令牌,但我不知道如何发送HTTPoison参数.
使用时curl,它会是这样的
$ curl -i -u "ColdFreak" -H "X-GitHub-OTP: 123456" -d '{"scopes": ["repo", "user"], "note"
: "getting-started"}' https://api.github.com/authorizations
Run Code Online (Sandbox Code Playgroud)
当我使用HTTPoison库时,我无法弄清楚如何发布它.
url = "https://api.github.com/authorizations"
HTTPoison.post!(url, [scopes: ["repo", "user"], note: "getting-started"], %{"X-GitHub-OTP" => "12345"})
Run Code Online (Sandbox Code Playgroud)
然后它给出了类似的错误
** (ArgumentError) argument error
:erlang.iolist_to_binary([{"scopes", ["repo", "user"]}, {"note", "getting-started"}])
(hackney) src/hackney_client/hackney_request.erl:338: :hackney_request.handle_body/4
(hackney) src/hackney_client/hackney_request.erl:79: :hackney_request.perform/2
Run Code Online (Sandbox Code Playgroud)
有人能告诉我如何以正确的方式做到这一点
HTTPoison的文档在这里
我尝试使用Elixir生成一个签名,它与PHP具有相同的值.
例如,PHP中的代码是
$signature = base64_encode(hash_hmac("sha256", "abc", "def"));
Run Code Online (Sandbox Code Playgroud)
并且输出将是
Mzk3ZjQ2NzM0MWU0ZDc4YzQ3NDg2N2VmMzI2MWNkYjQ2YzBlMTAzNTFlOWE5ODk5NjNlNmNiMmRjZTQwZWU1ZA==
我应该如何生成Elixir中具有相同值的签名.我试过下面的东西
iex(9)> :crypto.hmac(:sha256, "abc", "def") |> Base.encode64 ?
"IOvA8JNERwE081BA9j6pix2OQUISlJ7lxQBCnRXqsIE="
iex(10)> :crypto.hash(:sha256, :crypto.hmac(:sha256, "abc", "def")) |> Base.encode64 ?
"dxGiPN6KqBJrtS2wlC4tnJXwUsWf4u1LPDtDFK+VT5A="
Run Code Online (Sandbox Code Playgroud)
或I开关的位置abc和def
iex(11)> :crypto.hash(:sha256, :crypto.hmac(:sha256, "def", "abc")) |> Base.encode64 ?
"b+3P5oHu8e6HIlJe2MzcGhKm7tCcF/NE5wPIbEhrFGU="
iex(12)> :crypto.hmac(:sha256, "def", "abc") |> Base.encode64 ?
"OX9Gc0Hk14xHSGfvMmHNtGwOEDUempiZY+bLLc5A7l0="
Run Code Online (Sandbox Code Playgroud)
但它们都没有相同的价值.谁能告诉我如何以正确的方式做到这一点?
在Elixir中,使用Ecto,可以连接属于不同两个数据库的两个不同的表(在同一主机中).
有两个数据库称为cloud和cloud_usage在此查询
当我执行查询时,我应该使用哪个Repo?
Billing.CloudUsage.Repo.all(query)
要么
Billing.Cloud.Repo.all(query)
query = from cucu in "cloud_usage.cloud_usage",
inner_join: cv in "cloud.volumes", on: cucu.usage_id == cv.id,
where: cucu.account_id == ^account_id,
where: cucu.usage_id == 6,
where: like(cucu.description, ^vol_description),
where: cucu.start_date >= ^start_datetime,
where: cucu.start_date <= ^end_datetime,
group_by: cucu.usage_id,
group_by: cucu.zone_id,
select: {cucu.usage_id, cucu.zone_id, cucu.size, sum(cucu.raw_usage)}
???result = Billing.CloudUsage.Repo.all(query)
Run Code Online (Sandbox Code Playgroud)
当我调用该函数时,我收到了错误
** (Mariaex.Error) (1146): Table 'cloud_usage.cloud_usage.cloud_usage' doesn't exist
Run Code Online (Sandbox Code Playgroud)
我知道为什么会这样.但如果我使用Billing.Cloud.Repo.all(query),我想我几乎无法检索cloud_usage.cloud_usage表中的数据.反之亦然
参考:
有人可以告诉我如何在Elixir的SQL语句中添加单个反斜杠
iex(1)> sql = "select * from user limit 1 \G;"
"select * from user limit 1 G;"
iex(2)> sql = "select * from user limit 1 \\G;"
"select * from user limit 1 \\G;"
我只需要在我的sql语句中使用'\ G'
$ elixir -v
Elixir 1.1.0-dev
事实上,我想使用mariaex库,但我仍然无法使其工作
defmodule Customer do
def main(args) do
sql = "SELECT name FROM user limit 3 \\G;"
{:ok, p} = Mariaex.Connection.start_link(username: "root", password: "password", database: "user")
res = Mariaex.Connection.query(p, sql )
IO.inspect res
end
end
Run Code Online (Sandbox Code Playgroud)
当我执行代码时它告诉我在'\ G'周围有一个语法错误
$ escript …Run Code Online (Sandbox Code Playgroud) 在下面的链接做了一些研究
https://github.com/elixir-lang/ecto/tree/master/examples/simple
关于如何在灵药中使用ecto,我有点困惑.
有总是声明如下架构
defmodule Weather do
use Ecto.Model
schema "weather" do
field :city, :string
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp, :float, default: 0.0
timestamps
end
end
Run Code Online (Sandbox Code Playgroud)
然后在"查询"部分
def sample_query do
query = from w in Weather,
where: w.prcp > 0.0 or is_nil(w.prcp),
select: w
Simple.Repo.all(query)
end
end
Run Code Online (Sandbox Code Playgroud)
ecto gona使用Weather中声明的模式形成查询
我的问题是我只想连接到现有数据库'TESTDB'并做一些SELECT,我不需要任何新的schmema来完成我的工作.可以在外地做吗?
当我创建自己的查询时
query = from w in tenant
Run Code Online (Sandbox Code Playgroud)
输入命令后 $ mix do deps.get, compile
错误告诉我 function tenant/0 undefined
tenant不是功能,它只是一张表TESTDB,我没有在任何地方声明 …