如何使用 python 从树莓派向 Oracle SQL 数据库中插入行?
我目前正在开展一个个人项目,其中涉及:
I'm currently working on a personal project which involves:
- 带有 RFID 读写器模块的 Raspberry Pi Model b
- 一个 Oracle SQL 数据库 (11g)
- Python 脚本
这是我想要做的:我需要 Raspberry Pi 发送我从 RFID 标签读取的 UID(唯一标识号),并使用 UID 在我的 SQL 数据库中插入一行.从RFID标签读取的信息是一串数字,可以存储为String.
Here's what I'm trying to-do: I need the Raspberry Pi to send the UID (unique identification number) which I've read from the RFID tag and insert a row into my SQL database with the UID. The information read from the RFID tag is a bunch of numbers which can be stored as a String.
我目前能够读取标签并将 UID 打印到屏幕上.读数由我从网上找到的源代码修改的 Python 脚本处理.
I am currently able to read tags and print the UID onto the screen. The reading is being processed by a Python script which I've modified from a source code I found online.
我正在努力将 UID 发送到我的 SQL 数据库.我已经研究过 cx_Oracle,但它似乎不适用于 Raspberry Pi 使用的 ARM 架构.我也研究过 pyodbc,但我似乎也无法让它工作.这是我用来读取 RFID 标签的 Python 脚本.
I am struggling to send the UID to my SQL database. I have looked into cx_Oracle but it seems that it doesn't exist for the ARM Architecture which the Raspberry Pi uses. I have also looked into pyodbc but I can't seem to get that working either. Here is my Python script which I am using to read the RFID tags.
额外信息:说到 Python,我是个菜鸟,我有 C、Java、JDBC 和 Oracle SQL 方面的背景.我了解 JDBC 连接的工作原理,但我似乎无法在 Python 中实现相同的理论.对于任何 Python 专家,请随时修改我下面的代码以访问 Oracle SQL 数据库.我的数据库的凭据如下:
Extra Info : I am a noob when it comes to Python, I have a background in C, Java, JDBC and Oracle SQL. I understand how JDBC connections work but I can't seem to implement the same theory in Python. To anyone that is a pro at Python, please feel free to modify my code below to access an Oracle SQL database. The credentials to my database is as follows:
- 地址:本地主机
- 端口:1521
- 用户:学生
- 密码:test
- 方案:学生
表名:EMP
- Address: localhost
- Port: 1521
- User: student
- Password: test
- Scheme: STUDENT
Table Name: EMP
#!/usr/bin/env python
# -*- coding: utf8 -*-
import RPi.GPIO as GPIO
import MFRC522
import signal
continue_reading = True
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
continue_reading = False
GPIO.cleanup()
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
# Welcome message
print "Welcome to the MFRC522 data read example"
print "Press Ctrl-C to stop."
# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
# Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# If a card is found
if status == MIFAREReader.MI_OK:
print "Card detected"
# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
# If we have the UID, continue
if status == MIFAREReader.MI_OK:
# Print UID
print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
推荐答案
要访问 oracle 数据库,您需要 Rpi 上的客户端库.
To access an oracle database then you need client libraries on the Rpi.
正如您似乎已经发现的那样,ARM Linux 架构不存在这些,当然 Raspian O/S 也不存在.
As you seem to have discovered already, these don't exist for the ARM Linux architecture, and certainly not for the Raspian O/S.
您可以考虑为 RPi 使用 ODBC 驱动程序,看看您是否可以使其正常工作.
You could look into using an ODBC driver for RPi to see if you can get this to work.
这篇文章展示了某人拥有的东西已经试过了.
This Post shows what somebody has tried this already.
而且我还发现了这篇文章,有人声称在那里这通过 PHP 为 Rpi 工作.他使用的驱动程序(他为之工作)的公司也有一个 python 驱动程序.但是,在他们的网站上查看该软件是商业的(可免费试用)并且不声称支持 ARM Linux.虽然你可以试试他们的免费试用版.
And I also found this article, where somebody claims to have gotten this working for Rpi via PHP. The company who's driver he used (which he works for) also have a python driver. However, looking on their website the software is commercial (free trial available) and does not claim to support ARM Linux. Although you could try with their free trial.
此处是其他 ODBC 驱动程序的列表,这些驱动程序具有可用的 Python 驱动程序.
Here is a list of other ODBC drivers which have python drivers available.
虽然我从未尝试过,也没有这方面的经验,但有一种方法可以公开 SOAP 网络服务 来自 oracle 数据库.所以你可以试试他的方法,让 DB 公开服务,然后你从 python 访问 web 服务.但由于我还没有这样做,我不知道有任何限制,也不知道这是否真的适合您.
Although I have never tried it or have experience in this area, there is a way to expose SOAP web services from the oracle database. So you could try his approach instead, so the DB exposes the services and then you access the web service from python. But as I have not done it I don't know about any limitation or if this would actually work for you.
否则,您将需要采用不同的方法并在 Rpi 和 DB 之间使用某种中间件来接收数据并插入它.或者使用其他数据库!
Otherwise, you would need to take a different approach and use some kind of middle ware between the Rpi and the DB to receive the data and insert it. Or use another database!
相关文章