Node.js Programming Tutorials

Node.js Quickies – Working with MySQL

In this tutorial I’ll give you a quick overview on how to work with MySQL and Node.js.

Firstly, you need to include the mysql package in your package.json file, under “dependencies”. Make sure you run “npm install” to download all your project’s dependencies.

Like with many things in life, you start by establishing a connection:

This is how you can select data:

How about updating data:

Why you so asynchronous?

The main difference between working with MySQL in Node.js and other web application technologies such as PHP, is that in Node these calls are asynchronous. In PHP, once you do a query, the script execution will stop until the database has responded to the query. In Node, on the other hand, the query is an asynchronous call, so the script keeps on going.

If you are doing many queries and the asynchronous nature of it becomes too complex it’s worth looking into using Promises. I’ll cover promises using the Q package in another tutorial.

You can learn more about node-mysql by checking out it’s Github repo.

Node.js online course

Check out Node.js from Zero to Hero on Zenva Academy if you are after a comprehensive Node.js online course that can get you project-ready with this awesome tool.

Published by

Pablo Farias Navarro

Pablo is a web + mobile app developer and entrepreneur. Pablo is the founder of ZENVA. Besides teaching online how to create games, apps and websites to over 85,000 students, Pablo has created content for companies such as Amazon and Intel.ZENVA runs four development communities featuring game, web and mobile app development tutorials: Zenva Academy, GameDev Academy, HTML5 Hive (also known as "the hive"), and for Spanish speakers, De Idea A App.Pablo holds a Master in Information Technology (Management) degree from the University of Queensland (Australia) and a Master of Science in Engineering degree from the Catholic University of Chile. Specialized in web, mobile application and game development.

Share this article

  • Samy Terki

    By the way, this is my code:

    package com.example.lafabriquedumetro.suivichantier;

    import android.content.pm.PackageManager;
    import android.location.Location;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v4.content.ContextCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.location.LocationAvailability;
    import com.google.android.gms.location.LocationServices;

    import entity.ConstructionSiteEntity;
    import entity.ConstructionSitesEntity;
    import fragment.Launcher;
    import service.ConstructionSiteService;

    import android.Manifest;
    import android.widget.Toast;

    public class MainActivity extends AppCompatActivity implements
    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{

    private static final int PERMISSION_ACCESS_COARSE_LOCATION = 1;
    private GoogleApiClient googleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
    != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
    PERMISSION_ACCESS_COARSE_LOCATION);
    }

    googleApiClient = new GoogleApiClient.Builder(this, this, this).addApi(LocationServices.API).build();

    final FragmentManager fragmentManager = getSupportFragmentManager();
    final FragmentTransaction fragmentTransaction =
    fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.activity_main, new Launcher());
    fragmentTransaction.commit();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
    case PERMISSION_ACCESS_COARSE_LOCATION:
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    // All good!
    } else {
    Toast.makeText(this, “Need your location!”, Toast.LENGTH_SHORT).show();
    }

    break;
    }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
    == PackageManager.PERMISSION_GRANTED) {

    LocationAvailability locationAvailability = LocationServices.FusedLocationApi.getLocationAvailability(googleApiClient);
    Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

    double lat = lastLocation.getLatitude(), lon = lastLocation.getLongitude();
    Toast.makeText(this, lat + ” TEST ” + lon, Toast.LENGTH_SHORT).show();
    System.out.println(lat + ” ICI ” + lon);
    ConstructionSiteService constructionSiteService = ConstructionSiteService.getinstance();
    ConstructionSitesEntity constructionSitesEntity = ConstructionSitesEntity.getInstance();
    ConstructionSiteEntity constructionSiteEntity = constructionSiteService.get_near_construction_site(String.valueOf(lat), String.valueOf(lon));
    constructionSitesEntity.addConstructionSite(constructionSiteEntity);
    }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    @Override
    protected void onStart() {
    super.onStart();
    if (googleApiClient != null) {
    googleApiClient.connect();
    }
    }

    @Override
    protected void onStop() {
    googleApiClient.disconnect();
    super.onStop();
    }

    }