SQLServer library for Arduino Devices
Important tip about using database with Arduino
3 May, 2019 by
SQLServer library for Arduino Devices
Serzh Ohanyan

Introduction

In this post, will be shown some comments about the SQLServer library arduino-mssql.

This library is a connecting MSSQL via industrial Arduino. It is a basic implementation which you can use to run queries like SELECT, INSERT, UPDATE and DELETE. 

To see how it works, we have an example. In this example, we connect to the SQL Server with a client, and M-Duino for example, and send some queries. 


Link

Here you can find the link to the library:

Link Arduino MSSQL >>> 

You will see that there are two sample files: sqlard-test.ino and sqlard-test.ccp


Comments

First of all, you have to connect to a server. For that, we can connect to the server using Ethernet. We define a client and this client will connect to the server. Once is connected, we can send sentences to the database. To make our queries we have the function executeNonQuery(), you can see that in the example.  

Here you have a code example with some comments next in bold type. 


#include <Ethernet.h>
#include "sqlard.h"


uint8_t Ethernet_MacAddr[6] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; /* MAC ADRESİ */
static byte Static_IPAddr[] = { 172,16,1,2 };            
static byte Gateway_IPAddr[] = { 172,16,1,1 };
static byte Subnet_Mask[] = { 255,255,0,0 };
EthernetClient client; // Create a client

SQLard MSSQL(Gateway_IPAddr, 1433, &client); // Create the connection with the Server Data Base
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial); 
  Ethernet.begin(Ethernet_MacAddr, Static_IPAddr, Gateway_IPAddr, Gateway_IPAddr, Subnet_Mask); // Establish the connection of the client with the server 
  if(MSSQL.connect()) // Check if the connection with the Data Base is correct
  {
      MSSQL.setCredentials(L"arduino", L"ard_login",L"ard_password",L"hostx"); // Login to the Data Base
      MSSQL.login(); // Now we are able to send queries
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  static int loop_count = 0;
  delay(5000);

  long affected_rows = MSSQL.executeNonQuery(L"INSERT INTO [dbo].[test]([data]) VALUES('deger1234') "); // Make an INSERT queri.     
// [dbo].[test]([data]) would be the table
long yeni = MSSQL.executeNonQuery(L"DROP TABLE [dbo].[test23]"); // Make a DROP query. [dbo].[test23] would be the table Serial.print(affected_rows); // When you make an executeNonQuery it returns the number of affected rows Serial.println(" row(s) affected."); if (!client) { Serial.println("disconnecting."); client.stop(); } if(++loop_count == 10) { MSSQL.executeNonQuery(L"DELETE FROM [dbo].[test]"); // Make a DELETE query to delete all rows, but not the table. Like a Reset after 10 queries
// [dbo].[test] would be the table
loop_count = 0; } }

Hardware Solutions - Programmable Logic Controllers

Open Source based solutions for automation, monitoring and control

PLC Based on Arduino, Raspberry Pi and ESP 32



SQLServer library for Arduino Devices
Serzh Ohanyan
3 May, 2019
Share this post
Archive

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>

Do you want more information?

Just fill the form!

Tell me more!