Array[] in database, how to create and manipulate?

Asked

Viewed 511 times

-1

I own a array of integers, a int[].

Is it possible to save it directly to the database without having to Serialize? For example:

CREATE TABLE teste(
    numeros integer[]
);

And then use SELECT numeros FROM teste? I researched and did not find much information about this mainly in Java, most are all in PHP.

Is it recommended to do this? Or is it better to serialize?

  • You can use the JSON type and save in this format, save as a string in the format you want, for example, CSV, or create a table numbers and add a row for each number referenced to the original table

  • I think saving me JSON would be valid too, although I was not in the mood to use JSON I think this is also valid, I think I will follow the tip of Maniero and I will serialize even.

  • What a way you went?

  • I chose to save in Text in the database, I used StringJoiner to format in java and then turned into String to save in the database. At the time of getting the data back from the database I use the function split, everything works perfectly without errors and this well optimized.

1 answer

4

Only in Postgresql (among the best known) you can do this and even then you will go through some adaptation before recording or retrieving information. In any other database you will have to create your own recording manipulation and data recovery mechanism in the database.

The most common is to do this via VARCHAR where you will define a format where you will have the data (JSON is just an example), you can use a separator, a standard size or other form, which in the background is a way to serialize, either by text or binary. If you’re going to do binary, you’d better use one BLOB.

  • then the object to type Array of java.sql is only good for Postgresql? I was taking a look at the Java documentation and found this, but looking at the Mysql documentation I found nothing talking about. (so I decided to create the topic)

  • To create the Array connection.createArrayOf(typeName, elements) and to rescue Array resultSet.getArray(columnLabel)

  • I don’t know what it is java.sql and your question does not quote you.

  • java.sql is the class package Array

Browser other questions tagged

You are not signed in. Login or sign up in order to post.