# how to read data from a database created with python using arduino

**URL:** <https://forum.arduino.cc/t/how-to-read-data-from-a-database-created-with-python-using-arduino/1094821>\
**Category:** Programming\
**Created:** [February 25, 2023, 6:21pm UTC](https://forum.arduino.cc/t/how-to-read-data-from-a-database-created-with-python-using-arduino/1094821 "2023-02-25T18:21:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brenopitbull](https://avatars.discourse-cdn.com/v4/letter/b/858c86/32.png) [@brenopitbull](https://forum.arduino.cc/u/brenopitbull)\
**Post date:** [February 25, 2023, 6:21pm UTC](https://forum.arduino.cc/t/how-to-read-data-from-a-database-created-with-python-using-arduino/1094821/1 "2023-02-25T18:21:44Z")

</div>

Hello, I recently made a program that verifies the RFID, it receives the data using c++ and sends it by serial, using python it treats and creates a database with sqlite, but I need to make a led light up after verifying if the user is registered and not I found a way to read data using arduino (I'm not using an arduino internet device so I'm using sqlite3 with python)

**ARDUINO C++ CODE:**

````cpp
#include <MFRC522.h> 
#include <SPI.h>
#include <Wire.h>

#define RST_PIN 9
#define SS_PIN 10
#define LED 4
String IDtag = "";// Vai guardar o id da tag
MFRC522 LeitorRFID(SS_PIN, RST_PIN);// Cria uma instancia que usa como parametros o SS_PIN E O RST_PIN
void setup() {
  Serial.begin(9600); //Vai iniciar a comunicação atraves do usb usando 9600 bites
  SPI.begin(); //Inicia a comunicação SPI
  LeitorRFID.PCD_Init(); //Starta o leitor RFID
  pinMode(LED, OUTPUT);//DECLARA O PINO COMO SAIDA

}

void loop() {
  Leitura();//Chama a função responsavel por ler os ID do RFID
  char c = Serial.read();

  if (c == "b") {
    Serial.println("fooooooi");

    digitalWrite(LED, HIGH);

    delay(3000);

    digitalWrite(LED, LOW);

    delay(3000);
    
  }

}

void Leitura() 
{
  char sim = Serial.read();
  IDtag = "";

  //VERIFICA SE EXISTE UMA TAG ID
  if ( !LeitorRFID.PICC_IsNewCardPresent()) 
  {
    delay(50);
    return;
  }

  //SELECIONA O CARTÃO
  if (!LeitorRFID.PICC_ReadCardSerial())
  {
    delay(50);
    return;
  }

  //VAI PEGAR A TAG ID E ARMAZENAR NA VARIAVEL
   for (byte i = 0; i < LeitorRFID.uid.size; i++) 
   { 
    IDtag.concat(String(LeitorRFID.uid.uidByte[i], HEX));
   }
   Serial.println(sim);
   Serial.println(IDtag);
   delay(1000);
}```

````

**PYTHON CODE:**

import serial.tools.list\_ports  
import serial  
import sqlite3

connection = sqlite3.connect("condominio.db")

cursor = connection.cursor()  
cursor.execute("CREATE TABLE IF NOT EXISTS clientes (id TEXT NOT NULL);")

list\_ports = []

ports = serial.tools.list\_ports.comports()  
for p in ports:  
print(p.device)  
list\_ports.append(p.device)  
print(len(ports), 'ports found')

serial\_port = serial.Serial(str("COM5"))

while True:  
serial\_data = serial\_port.read\_until()  
data\_str = str(serial\_data.decode("utf-8"))  
data\_id = data\_str.strip()

```
cursor.execute(f"SELECT * FROM clientes WHERE id LIKE '%{data_id}%'")
rows = cursor.fetchone()

if rows == None:
    print("ID NÃO CADASTRADO")

else:
    print("ACESSO LIBERADO(ACENDE LED 3 sec)")

```

```cpp

```

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [February 25, 2023, 9:40pm UTC](https://forum.arduino.cc/t/how-to-read-data-from-a-database-created-with-python-using-arduino/1094821/2 "2023-02-25T21:40:36Z")

</div>

Hi @brenopitbull

welcome to the arduino forum  
Well done posting code as a code-section

You have to describe with much more **precision** what your code shall do

I give an example:  
the arduino is reading a RFID-card  
the arduino sends the ID-number over serial-connection to a windows-computer  
on the windows computer is running a python-script  
the python-script does .....  
and then ....  
....  
best regards Stefan

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 24, 2023, 9:41pm UTC](https://forum.arduino.cc/t/how-to-read-data-from-a-database-created-with-python-using-arduino/1094821/3 "2023-08-24T21:41:27Z")

</div>

Closed on August 24, 2023, 9:41pm UTC

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