如何将select结果保存在数组中的最佳方法

时间:2021-12-16 20:09:15

I have a question about keeping query result in array. For example I execute a query

我有一个关于将查询结果保存在数组中的问题。例如,我执行一个查询

SELECT * FROM some_table

SELECT * FROM some_table

Then I want to save it to array and create records. The table contains these columns:

然后我想将它保存到数组并创建记录。该表包含以下列:

  • id
  • ID
  • user_name
  • 用户名
  • last_name

The result array can be:

结果数组可以是:

[[1, "First user name", "First last name"],
 [2, "Second user name", "Second last name"]
 ...
]. 

Can you recommend me which array or data type should I use?

你能推荐一下我应该使用哪种数组或数据类型吗?

2 个解决方案

#1


0  

You do that like this:

你这样做:

  1. Create a bean class for User
  2. 为User创建一个bean类
public class User {
   private int id;
   private String firstName;
   private String lastName;

   // getter and setter
   ...

}

And then query all the data from table, and create a User object and set the data.

然后查询表中的所有数据,并创建一个User对象并设置数据。

List<User> users = new ArrayList<>();
while(someValue) {
   ...
   int id = ...
   String firstName= ...
   String lastName = ...
   User user = new User();
   user.setId(id);
   user.setFirstName(firstName);
   user.setLastName(lastName);
   users .add(user);
}

// after do what you want with the list

#2


0  

I extend your question to "the best way to keep select result" (with or without array).

我将您的问题扩展到“保持选择结果的最佳方式”(有或没有数组)。

It depends on:

这取决于:

  • how many results

    有多少结果

  • how many fields

    有多少个字段

  • what do you want to do after ?

    你想做什么?

  • do you want to modify, put in your database again ?

    你想修改,再次放入你的数据库?

So, several propositions:

所以,几个命题:

  • just arrays: String[] fields1; String[] fields2, ...

    只是数组:String [] fields1; String [] fields2,...

  • array of arrays: String[][];

    数组数组:String [] [];

  • better collections: Vector, List or Set: do you want them to be sorted ?, how do you pick them after ? Or Map, (if you want to keep index => data)

    更好的集合:矢量,列表或集合:你想要它们被排序吗?,你如何选择它们?或Map,(如果你想保留index => data)

  • or Object you create yourself. For this, you even have tools to map object-database.

    或您自己创建的对象。为此,您甚至可以使用工具来映射对象数据库。

you should take a look at these features, and what you want to do .

你应该看看这些功能,以及你想做什么。

Hope it helps.

希望能帮助到你。

#1


0  

You do that like this:

你这样做:

  1. Create a bean class for User
  2. 为User创建一个bean类
public class User {
   private int id;
   private String firstName;
   private String lastName;

   // getter and setter
   ...

}

And then query all the data from table, and create a User object and set the data.

然后查询表中的所有数据,并创建一个User对象并设置数据。

List<User> users = new ArrayList<>();
while(someValue) {
   ...
   int id = ...
   String firstName= ...
   String lastName = ...
   User user = new User();
   user.setId(id);
   user.setFirstName(firstName);
   user.setLastName(lastName);
   users .add(user);
}

// after do what you want with the list

#2


0  

I extend your question to "the best way to keep select result" (with or without array).

我将您的问题扩展到“保持选择结果的最佳方式”(有或没有数组)。

It depends on:

这取决于:

  • how many results

    有多少结果

  • how many fields

    有多少个字段

  • what do you want to do after ?

    你想做什么?

  • do you want to modify, put in your database again ?

    你想修改,再次放入你的数据库?

So, several propositions:

所以,几个命题:

  • just arrays: String[] fields1; String[] fields2, ...

    只是数组:String [] fields1; String [] fields2,...

  • array of arrays: String[][];

    数组数组:String [] [];

  • better collections: Vector, List or Set: do you want them to be sorted ?, how do you pick them after ? Or Map, (if you want to keep index => data)

    更好的集合:矢量,列表或集合:你想要它们被排序吗?,你如何选择它们?或Map,(如果你想保留index => data)

  • or Object you create yourself. For this, you even have tools to map object-database.

    或您自己创建的对象。为此,您甚至可以使用工具来映射对象数据库。

you should take a look at these features, and what you want to do .

你应该看看这些功能,以及你想做什么。

Hope it helps.

希望能帮助到你。