简体   繁体   中英

Failing to fetch data from MySQLdb database

[enter image description here][1]I am currently making a project for school and the time for reaching the wall has come.

I am trying to fetch data from the USB port on Raspberry Pi 3+. I have connected an Arduino Nano and I am sending a string(UID number in decimal of an RFID Card) from it to the Pi via the USB port. Everything works fine here, I can print out the string(ID) without a problem. I am comparing the ID from the card with the one in my database, and if I put a static number ( commented below in the code) it prints the data. However if I try with the serial line, nothing happens. It seems like it doesn't fetch the data at all. The outlook of my database is underneath and the python code as well. Thanks in Advance !!

card_id serial_no  LastName    FirstName 
   1 | 2136106133 | Hansen   | Peter     |
   2 | 117254270  | Larsen   | Thompson  |

#!/usr/bin/env python

 import MySQLdb
 import serial


 ser = serial.Serial('/dev/ttyUSB0',9600)
 db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                 user="root",         # your username
                 passwd="root",  # your password
                 db="RFID")        # name of the data base
 cur=db.cursor()

 CardID = 0
 LastName = "" 
 FirstName = ""

while True:
    CardID=ser.readline() 

    print "pweasda"
    print CardID
    print "pewpew"
  # CardID = 117254270 - this works. The problem is that I have many RFID cards/tags with different IDs of course. With this statement it prints everything correctly.

    cur.execute('SELECT * FROM cards WHERE serial_no=%s',(CardID))
    results = cur.fetchall()
    for row in results:
       FirstName = row[3]
       LastName = row [2]
       serial_no = row [1]
       card_id = row [0]
      #print the fetched results
       print "FirstName=%s,LastName=%s,serial_no=%s,card_id=%s" % \
         (FirstName, LastName, serial_no, card_id )
       db.commit()
       print "Data committed"

output image (no errors): [1]: http://postimg.org/image/jf2doogrv/

Possible solution could be:

import sqlite3
conn = sqlite3.connect("users.db")#path to your sqlite db file!
cursor = conn.cursor()
CardID=ser.readline().strip()


sql = "SELECT * FROM cards WHERE serial_no=?"
cursor.execute(sql, [(CardID)])
try:
    results = cursor.fetchall()[0]# just fetching the first row only, you can loop through all rows here!
    FirstName = results[3]
    LastName = results[2]
    serial_no = results[1]
    card_id  = results[0]
    print "FirstName=%s,LastName=%s,serial_no=%s,card_id=%s" % \
     (FirstName, LastName, serial_no, card_id )

except IndexError as e:
    print 'CardID Not Exist'+str(e)
except Exception as e2:
    print(str(e2)) 

In above code I am assuming the database is in sqlite DB, and also handled the exceptions so you can figure out the runtime error, if any!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM