简体   繁体   English

如何使用python将树莓派连接到另一台PC的数据库

[英]How to connect raspberry pi to another PC's database using python

I have a database in my current RPI.我当前的 RPI 中有一个数据库。 Since I'm only using 64GB SD card, I want to remove the database in my rpi and connect it to database of my laptop.由于我只使用 64GB SD 卡,我想删除 rpi 中的数据库并将其连接到我的笔记本电脑的数据库。

I've setup my LAN, I can now access the database through browser using my IP (192.168.1.14) but my code is not working.我已经设置了我的 LAN,我现在可以使用我的 IP (192.168.1.14) 通过浏览器访问数据库,但我的代码不起作用。

Can you guys help me on how to connect my python code to another database.你们能帮助我如何将我的 python 代码连接到另一个数据库。

here's my code to connect to database这是我连接到数据库的代码

import tkinter as tk
import pymysql
from tkinter import *
from tkinter import messagebox
from mfrc522 import SimpleMFRC522
import RPi.GPIO as GPIO

class Kiosk(tk.Frame):
   def __init__(self, master, *args, **kwargs):
       tk.Frame.__init__(self, master, *args, **kwargs)
       self.master = master
       master.title("Thesis")

       self.login_button = Button(master, text = "Login", bg= "white")
       self.login_button.pack()
       self.register_button = Button(master, text="Register", command=self.register)
       self.register_button.pack()

       self.close_button = Button(master, text="Close", command=master.destroy)
       self.close_button.pack()
       self.reader = SimpleMFRC522()

       self.master.configure(bg="white")    
       self.ws = self.master.winfo_screenwidth()
       self.hs = self.master.winfo_screenheight()

       self.master.geometry("{}x{}".format(self.ws, self.hs))

       self.db = pymysql.connect(host = "192.168.1.14", port = 3306, user = "root",passwd = "",db= "Thesis")
       self.cursor = self.db.cursor()
       self.QueryResident = "CREATE TABLE IF NOT EXISTS residence (FIRST_NAME varchar(255) not null, MIDDLE_NAME varchar(255) not null, LAST_NAME varchar(255) not null,SEX varchar(255) not null, BIRTH_DATE date, CIVIL_STATUS varchar(255) not null, PLACE_OF_BIRTH varchar(255) not null, RFID varchar(255))"
       self.cursor.execute(self.QueryResident)
       GPIO.cleanup()

   def registered(self, first_name, middle_name, last_name, sex, birth_day, civil_status, place_of_birth):
       self.get_first_name = self.first_name.get()
       self.get_middle_name = self.middle_name.get()
       self.get_last_name = self.last_name.get()
       self.get_sex = self.sex.get()
       self.get_birth_day = self.birth_day.get()
       self.get_civil_status = self.civil_status.get()
       self.get_place_of_birth = self.place_of_birth.get()


       if (self.get_first_name == "" or self.get_middle_name == "" or
           self.get_last_name == "" or self.get_sex == "" or self.get_birth_day == "" or
           self.get_civil_status == "" or self.get_place_of_birth == ""):
               messagebox.showerror("Error!","Please complete the required field", parent = self.master_register)
       else:
           self.master_register.destroy()
           self.RFID_registered()

   def RFID_registered(self):
       if (self.cursor.fetchone() is not None):
           messagebox.showerror("Notice!", "RFID card is already registered", parent = self.master_register)
       else:
           messagebox.showinfo("Success!","Your Registration is Successful", parent = self.master_register)
           self.cursor.execute ("INSERT INTO residence (FIRST_NAME, MIDDLE_NAME, LAST_NAME, SEX, BIRTH_DATE, CIVIL_STATUS, PLACE_OF_BIRTH) VALUES(%s, %s, %s, %s, %s, %s, %s)",(self.get_first_name,
                                                                                                                                                                                          self.get_middle_name,
                                                                                                                                                                                          self.get_last_name,
                                                                                                                                                                                          self.get_sex,
                                                                                                                                                                                          self.get_birth_day,
                                                                                                                                                                                          self.get_civil_status,
                                                                                                                                                                                          self.get_place_of_birth,
                                                                                                                                                                                          ))
           self.db.commit()
           self.master_register.destroy()
           GPIO.cleanup()

       GPIO.cleanup()


   def only_numeric_input(self, P):
       if P.isdigit() or P == "":
           return True
       return False

   def register(self):
       self.master_register = Toplevel()


       self.first_name = StringVar()
       self.middle_name = StringVar()
       self.last_name = StringVar()
       self.sex = StringVar()
       self.birth_day = StringVar()
       self.civil_status = StringVar()
       self.place_of_birth = StringVar()

       self.label_head = Label(self.master_register, text = "Please fill up all informations below",bg = "white")
       self.label_head.pack()

       self.label_first_name = Label(self.master_register, text = "First Name",bg = "white")
       self.label_first_name.pack()
       self.entry_first_name = Entry(self.master_register, textvariable = self.first_name,bg = "white")
       self.entry_first_name.pack()

       self.label_middle_name = Label(self.master_register, text = "Middle Name",bg = "white")
       self.label_middle_name.pack()
       self.entry_middle_name = Entry(self.master_register, textvariable = self.middle_name,bg = "white")
       self.entry_middle_name.pack()

       self.label_last_name = Label(self.master_register, text = "Last Name",bg = "white")
       self.label_last_name.pack()
       self.entry_last_name = Entry(self.master_register, textvariable = self.last_name,bg = "white")
       self.entry_last_name.pack()

       self.label_sex = Label(self.master_register, text = "Sex",bg = "white")
       self.label_sex.pack()
       self.radio_button_sex = Radiobutton(self.master_register, text = "Male", variable = self.sex, value = "Male",bg = "white")
       self.radio_button_sex.pack()
       self.radio_button_sex = Radiobutton(self.master_register, text = "Female", variable = self.sex, value = "Female",bg = "white")
       self.radio_button_sex.pack()

       self.label_birth_day = Label(self.master_register, text = "Birth Day",bg = "white")
       self.label_birth_day.pack()

       self.entry_birth_day = Entry(self.master_register, textvariable = self.birth_day,bg = "white")
       self.entry_birth_day.pack()
       self.callback = self.master_register.register(self.only_numeric_input)
       self.entry_birth_day.configure(validate = "key", validatecommand = (self.callback, "%P"))

       self.label_civil_status = Label(self.master_register, text = "Civil Status",bg = "white")
       self.label_civil_status.pack()
       self.entry_civil_status = Entry(self.master_register, textvariable = self.civil_status,bg = "white")
       self.entry_civil_status.pack()

       self.label_place_of_birth = Label(self.master_register, text = "Place of Birth",bg = "white")
       self.label_place_of_birth.pack()
       self.entry_place_of_birth = Entry(self.master_register, textvariable = self.place_of_birth,bg = "white")
       self.entry_place_of_birth.pack()



       self.button_submit = Button(self.master_register, text = "Submit",bg = "white", command = lambda: self.registered(self.first_name, self.middle_name,
                                                                                           self.last_name, self.sex, self.birth_day,
                                                                                           self.civil_status, self.place_of_birth))
       self.button_submit.pack()


if __name__== "__main__":
   root = Tk()
   main = Kiosk(root)
   root.mainloop()


Here's the error I've encountered.
`/usr/local/lib/python3.7/dist-packages/mfrc522/MFRC522.py:151: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(pin_rst, GPIO.OUT)
Traceback (most recent call last):
  File "/home/pi/stack.py", line 145, in <module>
    main = Kiosk(root)
  File "/home/pi/stack.py", line 29, in __init__
    self.db = pymysql.connect(host = "192.168.1.14", port = 3306, user = "root",passwd = "",db= "Thesis")
  File "/usr/local/lib/python3.7/dist-packages/pymysql/__init__.py", line 94, in Connect
    return Connection(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/pymysql/connections.py", line 325, in __init__
    self.connect()
  File "/usr/local/lib/python3.7/dist-packages/pymysql/connections.py", line 598, in connect
    self._get_server_information()
  File "/usr/local/lib/python3.7/dist-packages/pymysql/connections.py", line 975, in _get_server_information
    packet = self._read_packet()
  File "/usr/local/lib/python3.7/dist-packages/pymysql/connections.py", line 671, in _read_packet
    % (packet_number, self._next_seq_id))
pymysql.err.InternalError: Packet sequence number wrong - got 1 expected 0

Hope you guys can help me, Thank you so much!希望大家帮帮我,万分感谢!

I am not exactly sure what's wrong with your program.我不确定你的程序有什么问题。 However, you can give the below program a shot!但是,您可以试一试下面的程序!

import pymysql as db
try:
    con=db.connect("Enter the host here ","user_name","pass_word","db_name")
except Exception as e:
    print(e)
con.autocommit(True)
with con:
        cur=con.cursor()
        cur.execute("DROP table if exists Test_Table")
        cur.execute("CREATE TABLE Test_Table(id INT PRIMARY KEY AUTO_INCREMENT, name varchar(20),Time timestamp,age float(20)")

Change the column names accordingly for your requirement!根据您的要求相应地更改列名称!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用python连接树莓派和Windows PC的套接字编程 - socket programming using python to connect raspberry pi and windows PC 使用 Python 将数据从 PC 发送到 Raspberry Pi - Sending data from PC to Raspberry Pi using Python 如何从单独的PC控制Raspberry Pi的GPIO引脚 - How to control Raspberry Pi's GPIO pins from separate PC 如何使用 python 将实时数据从计算机(PC)发送到 Raspberry pi 4? - How to send Live Data from Computer(PC) to Raspberry pi 4 using python? 如何使用 OPCUA 连接两个树莓派? - how to connect two raspberry pi using OPCUA? Raspberry pi 3b+ 上的 IndexError 但在 pc Python 中没有 - IndexError on Raspberry pi 3b+ but not in pc Python 如何将Windows用作树莓派,以及如何将Windows与其他树莓派连接[保持] - How to use windows as raspberry pi and connect the windows with another raspberry pi [on hold] 如何连接到托管在私有树莓派服务器上的远程 MySQL 数据库? - How to connect to a remote MySQL database that is hosted on a private raspberry pi server? 如何从 Raspberry Pi 向 PC 发送读数? - How to send readings to PC from Raspberry Pi? 使用python将树莓派传感器输入到sqlite3数据库 - Raspberry pi sensor input to sqlite3 database using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM