Swprintf and wchar_t?

well, you should ask this question from the original author of the library "sqlard.h" (Mustafa Kemal GILOR), I am just trying to use his library!

and finally here is the code which works fine :

// how to compile :
// 1. download "arduino-mssql-master.zip" from https://github.com/mustafakemalgilor/arduino-mssql [By Mustafa Kemal GILOR]
// 2. extract, copy, paste "arduino-mssql-master" to C:\Users\[YOUR_USERNAME]\Documents\Arduino\libraries
// 3. delete "sqlard-test.cpp"
// 4. open "sqlard.h" and delete line 3 (#define UIPETHERNET)
// 5. find out your sql server ip, database name, username, password, server name and then set them in the code
// tested with sql server 2012, 2014, 2016
// more info in German! : https://forum.arduino.cc/t/arduino-daten-an-ms-sql/661791
// this code is shared here : https://forum.arduino.cc/t/swprintf-and-wchar-t/868881/21

#include "sqlard.h"

static byte server_IP[] = {1, 1, 1, 1};   // sql server ip
EthernetClient client;
SQLard MSSQL(server_IP, 1433, &client);

void InsertValue(int value)
{
  int i;

  wchar_t str1[]  = L"INSERT INTO [dbo].[myTable] ([data]) VALUES (";   // insert query
  int len1 = sizeof(str1) / sizeof(wchar_t);

  int len2 = log10(value) + 1;
  wchar_t str2[len2];

  wchar_t str3[]  = L")";
  int len3 = sizeof(str3) / sizeof(wchar_t);

  int len = len1 + len2 + len3 - 1;
  wchar_t str[len];

  for (i = 0 ; i < len1 - 1 ; i++)
    str[i] = str1[i];
  int index = i;

  for (i = len2 - 1; i >= 0; --i)
  {
    str2[i] = (value % 10) + '0';
    value /= 10;
  }

  for (i = 0 ; i < len2 ; i++)
    str[index + i] = str2[i];
  index = index + i;

  for (i = 0 ; i < len3 ; i++)
    str[index + i] = str3[i];

  MSSQL.executeNonQuery(str);
  delay(1000);

  for (i = 0 ; i < len ; i++)
    Serial.write(str[i]);
  Serial.println("");
}

void setup()
{
  Serial.begin(9600);
  while (!Serial);
  Serial.println("serial started");
  delay(1000);

  byte Mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  IPAddress IP(192, 168, 1, 10);
  IPAddress Dns(192, 168, 1, 1);
  IPAddress Gateway(192, 168, 1, 1);
  IPAddress Subnet(255, 255, 255, 0);
  Ethernet.begin(Mac, IP, Dns, Gateway, Subnet);
  Serial.println("ethernet connected");
  delay(1000);

  if (MSSQL.connect())
  {
    Serial.print("connected to sql server ip : ");
    char buff[10];
    sprintf(buff, "%d.%d.%d.%d", server_IP[0], server_IP[1], server_IP[2], server_IP[3]);
    Serial.println(buff);
    delay(1000);

    MSSQL.setCredentials(L"database", L"username", L"password", L"server name");  // database name, username, password, server name
    MSSQL.login();
    Serial.println("login ok");
    delay(1000);
  }
  else
  {
    Serial.println("error! wrong sql server ip?");
  }
}

void loop()
{
  for (int i = 777 ; i < 7777 ; i++)
  {
    delay(10000);
    InsertValue(i);
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.