想象一下comments,您的数据库中有一个表.
注释表有列,id,text,show,comment_id_no.
如果用户输入注释,它会在数据库中插入一行
| id | comment_id_no | text | show | inserted_at |
| -- | -------------- | ---- | ---- | ----------- |
| 1 | 1 | hi | true | 1/1/2000 |
Run Code Online (Sandbox Code Playgroud)
如果用户想要更新该注释,则会在数据库中插入新行
| id | comment_id_no | text | show | inserted_at |
| -- | -------------- | ---- | ---- | ----------- |
| 1 | 1 | hi | true | 1/1/2000 | …Run Code Online (Sandbox Code Playgroud)
我正在尝试将 e 中的 a 舍Float入到小数点后两位。
如果我有数字12.555,我希望我的舍入函数返回12.56
我最初认为这Float.round是我想要的,但这个函数并不总是返回我想要的答案。
例如...
iex()> Float.round(12.555, 2)
12.55
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过临时功能完成这项工作,但我认为必须有更好的解决方案。
我目前的解决方案是...
iex()> round(12.555 * 100) / 100
12.56
Run Code Online (Sandbox Code Playgroud)
这可以完成工作,但就像我说的那样,我只是想知道是否有更好的解决方案。
提前致谢
我正在尝试将凤凰应用程序连接到MS SQL.在网上浏览后,我遇到了几个名为mssql_ecto&mssqlex的适配器.
我按照自述文件中的说明将它们添加到项目中,安装了odbc并检查了数据库是否在线,但我现在收到以下错误..
[error] Mssqlex.Protocol (#PID<0.13069.0>) failed to connect: ** (Mssqlex.Error) odbc_not_started
Run Code Online (Sandbox Code Playgroud)
我的应用程序配置如下..
config :my_app, MyApp.Repo,
adapter: MssqlEcto,
username: "<my_username>",
password: "<my_password>",
database: "test",
hostname: "<my_server>.database.windows.net",
pool_size: 10
Run Code Online (Sandbox Code Playgroud)
我的环境如下..
mssql_ecto repo中已经存在一个问题,我已经尝试了那里的建议,但它仍然无效.
如果有人设法将他们的phoenix/elixir应用程序连接到macOS上的MSSQL并且可以提供一些指令,那将非常感激(即使它以与我的方法完全不同的方式完成).
附注:在具有相同数据库的节点中尝试并且能够连接并查询数据库.
我有一个默认的Phoenix应用程序.这个应用程序将有一个page_controller
将加载index.html.eex文件.
该应用程序将知道使用view访问权限templates/page/index.html.eex.
现在说你已经创建了另一个html页面,index.html.eex除了它是法语之外,它在各个方面都是相同的.
因为我们不想创建一个全新的Phoenix应用程序,它将具有所有相同的代码,除了当前的法语翻译之外page/index.html.eex,有没有办法告诉视图或控制器需要加载哪个文件.
是否有可以放置在路由器中的插头来改变render寻找模板的位置?
我正在创建一个宏,该宏将计算两组纬度-经度值之间的距离。
iex()> calc_distance(posA, posB)
2 # distance is in km
Run Code Online (Sandbox Code Playgroud)
目前,它的工作方式类似于常规功能。我希望它成为宏的原因是可以在guard子句中使用它。例如
fn(posA, posB) when calc_distance(posA, posB) < 10 -> "close enough" end
Run Code Online (Sandbox Code Playgroud)
但是,对于要在保护子句中使用的宏,它必须“ 遵循规则 ”。这意味着不允许使用许多功能和运算符。
我最初的宏看起来像这样...
defmacro calc_distance(ll1, ll2) do
quote do
lat1 = elem(unquote(ll1), 0)
long1 = elem(unquote(ll1), 1)
lat2 = elem(unquote(ll2), 0)
long2 = elem(unquote(ll2), 1)
v = :math.pi / 180
r = 6372.8
dlat = :math.sin((lat2 - lat1) * v / 2)
dlong = :math.sin((long2 - long1) * v / 2)
a = dlat * dlat + …Run Code Online (Sandbox Code Playgroud) 我们需要在 PostgreSQL 中存储记录历史,这样当一条记录插入或更新到主表(例如:)时pets,它会自动备份到历史表(pets_history)。
理想情况下,我们需要根据主表的模式生成历史表,无需任何人工干预。
INSERT INTO pets(name, species) VALUES ('Meowth', 'Cat')
Run Code Online (Sandbox Code Playgroud)
pets:
+---+------------+-------------+
|id | name | species |
+---+------------+-------------+
| 1 | Meowth | Cat |
+---+------------+-------------+
Run Code Online (Sandbox Code Playgroud)
ATrigger应该自动将记录插入到pets_history:
pets_history:
+----+--------+-----------+---------+
| id | ref_id | name | species |
+----+--------+-----------+---------+
| 1 | 1 | Meowth | Cat |
+----+--------+-----------+---------+
Run Code Online (Sandbox Code Playgroud)
当对宠物进行更新以将我的猫的名字从 更改Meowth为 时Persian。例如:
pets:
+---+------------+-------------+
|id | name | species |
+---+------------+-------------+
| 1 | Meowth …Run Code Online (Sandbox Code Playgroud) 我使用 erlangs ets 功能缓存了一堆邮政编码和经纬度值。
如下图...
iex()> :ets.new(:postcode_cache, [:named_table])
:postcode_cache
iex()> :ets.insert(:postcode_cache, [{"OX495NU", "latlongvalue"},{"M320JG", "latlongvalue"}])
true
Run Code Online (Sandbox Code Playgroud)
这类似于我在应用程序中创建的 ets 表。我想创建一个函数,仅从缓存中选择条目,其中邮政编码包含用户输入的子字符串。有什么办法可以做到这一点,如果可以的话,我将如何实现这个功能?
(将来我可能只想使用经纬度值选择一定距离内的值,但这超出了这个问题的范围)。
为了清楚起见,该表类似于以下灵丹妙药列表......
iex()> postcode_list = [{"OX495NU","latlong"}, {"M320JG", "latlong"}]
Run Code Online (Sandbox Code Playgroud)
我想用 ets 复制的功能是这样的......
iex()> Enum.filter(list, fn({postcode, _}) -> if String.contains?(postcode, "OX49") end)
[{"OX495NU", "latlong"}]
Run Code Online (Sandbox Code Playgroud) 我想知道是否可以在 ecto 中将两个查询组合在一起。
我想创建一个函数,它接受一个 ecto 查询并根据情况修改它。
例如,假设我们有一个users允许usernames输入重复项的表,并且用户创建了以下查询...
query = from(u in "users", select: u)
select_all_unique_users(query)
Run Code Online (Sandbox Code Playgroud)
我希望该select_all_unique_users函数能够接受query并添加到其中,以便它只选择不同的用户名。
这只是一个例子,我知道我可以
unique_index在桌子上创建一个以在现实中避免这种情况=D
作为query引擎盖下的结构,我想我可以更新distinct密钥来做到这一点。这似乎可行,但缺点是它似乎不太灵活,而且如果我想添加更复杂的逻辑,似乎很难实现。
简而言之,我想做的是创建一个函数,该函数接受一个查询并为其添加更多逻辑......
iex()> query = from(u in "users", select: u)
#Ecto.Query<from u0 in "users", select: u0>
iex()> select_all_unique_users(query)
#Ecto.Query<from u0 in "users", distinct: [asc: u0.username], select: u0>
Run Code Online (Sandbox Code Playgroud) 我正在从csv文件中读取邮政编码,获取该数据并使用ets进行缓存.
邮政编码文件相当大(95MB),因为它包含大约180万条目.
我只缓存目前查找所需的邮政编码(大约200k),因此存储在ets中的数据量应该不是问题.然而,无论插入的数量有多少,该过程占用的内存量实际上都没有变化.如果我插入1行或全部180万,似乎并不重要.
# not logging all functions defs so it is not too long.
# Comment if more into is needed.
defmodule PostcodeCache do
use GenServer
def cache_postcodes do
"path_to_postcode.csv"
|> File.read!()
|> function_to_parse()
|> function_to_filter()
|> function_to_format()
|> Enum.map(&(:ets.insert_new(:cache, &1)))
end
end
Run Code Online (Sandbox Code Playgroud)
我在终端上iex -S mix运行它并运行命令:observer.start.当我进入进程选项卡时,我的postcodeCache内存很大(超过600MB)
即使我过滤文件,所以我最终只在其中存储1个邮政编码:ets仍然超过600MB.
我希望能够将长生不老药字符串(二进制)转换为base10整数。
我已经能够做到以下几点...
<<104, 101, 108, 108, 111>> # equal to string "hello"
|> Base.encode16()
|> Integer.parse(16)
{448378203247, ""}
Run Code Online (Sandbox Code Playgroud)
以上是我所追求的,但感觉有点像黑客。我想要...
elixir ×9
erlang ×3
caching ×2
ets ×2
postgresql ×2
azure ×1
base ×1
distinct ×1
ecto ×1
elixir-iex ×1
encoding ×1
gen-server ×1
html ×1
int ×1
math ×1
sql ×1
sql-server ×1
triggers ×1
where ×1