在SQL数据库中存储HashMap

时间:2022-10-17 22:37:34

How do you store a HashMap inside of an SQL database? Also, how would you load that HashMap from a SQL database back into an instance of a HashMap?

如何在SQL数据库中存储HashMap?另外,如何将SQL数据库中的HashMap加载回HashMap实例?

Okay, this is what I'm doing. I have a database to store the player data for my game. It has a table containing their usernames and passwords. Each player has a HashMap that stores their properties. I need to store that HashMap in the database along with it's respective user.

好的,这就是我正在做的事情。我有一个数据库来存储我的游戏的玩家数据。它有一个包含用户名和密码的表。每个玩家都有一个存储其属性的HashMap。我需要将HashMap与其各自的用户一起存储在数据库中。

4 个解决方案

#1


11  

You need a 3 column table

你需要一个3列表

user,

key,

value

Would then look like

然后看起来像

"user 1", "property1", "value1"

“user 1”,“property1”,“value1”

"user 1", "property2", "value2"

“用户1”,“property2”,“value2”

"user 2", "property1", "value1"

“user 2”,“property1”,“value1”

"user 3", "property1", "value1"

“用户3”,“property1”,“value1”

"user 3", "property2", "value2"

“用户3”,“property2”,“value2”

You would then just read the data back in and look round the user field to rebuild each has table.

然后,您只需读回数据并查看用户字段以重建每个表。

#2


6  

If you want to stick key-value you can, however, you've to make sure the keys and values are strings and can fit in the column definition. Here is an example:

但是,如果要粘贴键值,则必须确保键和值是字符串,并且可以适合列定义。这是一个例子:

mysql> create table hashmap (k varchar(200), v varchar(200));
Query OK, 0 rows affected (0.24 sec)

mysql> insert into hashmap values ('key','value');
Query OK, 1 row affected (0.02 sec)

mysql> select * from hashmap;
+------+-------+
| k    | v     |
+------+-------+
| key  | value |
+------+-------+
1 row in set (0.00 sec)

Additionally, you can unique index the column k which holds the keys so your lookups are constant just like a hashmap, and you can replace the old value by using ON DUPLICATE KEY UPDATE v = new_value.. I am assuming you're using MySQL for the ON DUPLICATE KEY part.

另外,你可以对保存键的列k进行唯一索引,这样你的查找就像一个hashmap一样是常量,你可以使用ON DUPLICATE KEY UPDATE v = new_value替换旧值。我假设你使用的是MySQL ON DUPLICATE KEY部分。

#3


1  

Each entry in the HashMap is like a row in your table. You would end up having a separate table for each HashMap (probably).

HashMap中的每个条目就像表中的一行。您最终会为每个HashMap创建一个单独的表(可能)。

Your K parameter in your map should be enforced as a unique key on your table, and indexed.

地图中的K参数应作为表中的唯一键强制执行并编制索引。

Keep in mind that storing these objects may require more than one column each. For example, you may have a Point object as your key, so you'd need a column x and column y, and an index on uniqueness for x and y pairs.

请记住,存储这些对象可能需要多个列。例如,您可能将Point对象作为键,因此您需要列x和列y,以及x和y对的唯一性索引。

#4


1  

HashMap is data structure, You can store data residing in it.

HashMap是数据结构,您可以存储驻留在其中的数据。

Create a table emulating key value pair. iterate through keys of map and store it in DB.

创建一个模拟键值对的表。迭代地图的键并将其存储在数据库中。

#1


11  

You need a 3 column table

你需要一个3列表

user,

key,

value

Would then look like

然后看起来像

"user 1", "property1", "value1"

“user 1”,“property1”,“value1”

"user 1", "property2", "value2"

“用户1”,“property2”,“value2”

"user 2", "property1", "value1"

“user 2”,“property1”,“value1”

"user 3", "property1", "value1"

“用户3”,“property1”,“value1”

"user 3", "property2", "value2"

“用户3”,“property2”,“value2”

You would then just read the data back in and look round the user field to rebuild each has table.

然后,您只需读回数据并查看用户字段以重建每个表。

#2


6  

If you want to stick key-value you can, however, you've to make sure the keys and values are strings and can fit in the column definition. Here is an example:

但是,如果要粘贴键值,则必须确保键和值是字符串,并且可以适合列定义。这是一个例子:

mysql> create table hashmap (k varchar(200), v varchar(200));
Query OK, 0 rows affected (0.24 sec)

mysql> insert into hashmap values ('key','value');
Query OK, 1 row affected (0.02 sec)

mysql> select * from hashmap;
+------+-------+
| k    | v     |
+------+-------+
| key  | value |
+------+-------+
1 row in set (0.00 sec)

Additionally, you can unique index the column k which holds the keys so your lookups are constant just like a hashmap, and you can replace the old value by using ON DUPLICATE KEY UPDATE v = new_value.. I am assuming you're using MySQL for the ON DUPLICATE KEY part.

另外,你可以对保存键的列k进行唯一索引,这样你的查找就像一个hashmap一样是常量,你可以使用ON DUPLICATE KEY UPDATE v = new_value替换旧值。我假设你使用的是MySQL ON DUPLICATE KEY部分。

#3


1  

Each entry in the HashMap is like a row in your table. You would end up having a separate table for each HashMap (probably).

HashMap中的每个条目就像表中的一行。您最终会为每个HashMap创建一个单独的表(可能)。

Your K parameter in your map should be enforced as a unique key on your table, and indexed.

地图中的K参数应作为表中的唯一键强制执行并编制索引。

Keep in mind that storing these objects may require more than one column each. For example, you may have a Point object as your key, so you'd need a column x and column y, and an index on uniqueness for x and y pairs.

请记住,存储这些对象可能需要多个列。例如,您可能将Point对象作为键,因此您需要列x和列y,以及x和y对的唯一性索引。

#4


1  

HashMap is data structure, You can store data residing in it.

HashMap是数据结构,您可以存储驻留在其中的数据。

Create a table emulating key value pair. iterate through keys of map and store it in DB.

创建一个模拟键值对的表。迭代地图的键并将其存储在数据库中。