How to make a query in the database with Javascript?

Asked

Viewed 3,545 times

-1

I would like to know how to select a table in the database with javascript

  • 1

    Select in a database with pure Javascript, you will hardly ever do. You did not confuse with Java no?

  • 3

    only if you use javascript on the "server side" with nodejs for example

  • Actually I didn’t confuse with Java, I know the difference, but it was a doubt that arose whether it would be possible.

  • But I’m grateful for the clarification

1 answer

2


Your question is a little broad, but come on, it’s a perfectly valid question for a beginner.

It is not the language that accesses the database, it is some library or framework made for that language. Some languages have access to some database types in the standard library itself, others do not.

In the case of Javascript, it will always be necessary to install a third-party library to access any database.

In Javascript there is an additional complicator: the environment in which it runs. The two most common environments are browser and Node.js. As far as I know every library for database access is made for the Node.js environment. You will install via NPM.

The browser environment is much more restricted, I don’t think it even offers conditions for a library of this type. For example: to access a Mysql database you need to make a TCP/IP connection, but in the browser this is prohibited for security reasons.

But let’s say your need is that an application running in the browser needs to access the bank. You will have to do this indirectly, with the browser making REST, Graphql or other requests to a Web server, and this is making the query to a database.

It is not recommended in any way pass SQL queries directly from the browser to the server using this intermediation scheme, because then any attacker could send "bad" SQL queries of type DROP DATABASE.

Browser other questions tagged

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