我们如何比较python中的两个对象和SQL的数据内容?

越野

这是python中的套接字编程,其中python是服务器,而android是客户端。在这里,我从Android(客户端)接收QR码数据,该数据存储在df1对象中。另一个对象是MySQL数据库中的df2,其中包含与df1中相同的QR码数据。使用python,如何比较这两个对象的数据是否相同?

server-side.py

    import socket
    import sys
    import pickle

    def server_program():
        HOST = '192.168.0.113' #this is your localhost
        PORT = 8888

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #socket.socket: must use to create a socket.
        #socket.AF_INET: Address Format, Internet = IP Addresses.
        #socket.SOCK_STREAM: two-way, connection-based byte streams.
        print('socket created')

        #Bind socket to Host and Port
        try:
            s.bind((HOST, PORT))
        except socket.error as err:
            print('Bind Failed, Error Code: ' + str(err[0]) + ', Message: ' + err[1])
            sys.exit()

        print('Socket Bind Success!')


        #listen(): This method sets up and start TCP listener.
        s.listen(10)
        print ('Socket is now listening')


        while 1:
            conn, addr = s.accept()
            print ('Connect with ' + addr[0] + ':' + str(addr[1]))
            df1 = conn.recv(1024).decode()
            print(df1)
            print('data recieved from client')
            if not df1:
                break
            print("from connected user: " + str(df1))
            df1 = input(' -> ')
            conn.send(df1.encode())
            break
            conn.close()

        s.close()

    if __name__ == '__main__':
        server_program()
越野

我解决了这个问题,如下所示:

 import mysql.connector as mysql # This module is used to connect python with MySQL database 
    import socket
    import sys 
    import json
    #import time

    mydb1=mysql.connect(
            user = 'rajat',
            passwd = 'rajat',
            host = 'localhost',
            database = 'master_database'
            )

    # This is mysql database which is connected with python.
    #Here we need user,host,password and database name to connect with python
    # Its a mysql query which is selecting all fields from table where id is equals to one.

    def server_program():
        HOST = '192.168.0.116' #this is your localhost
        PORT = 8007

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #socket.socket: must use to create a socket.
        #socket.AF_INET: Address Format, Internet = IP Addresses.
        #socket.SOCK_STREAM: two-way, connection-based byte streams.
        # Binding socket with post and host.

        try:
            s.bind((HOST, PORT))
        except socket.error as err:
            sys.exit()
        #listen(): This method sets up and start TCP listener.
        s.listen(2)
        print ('Socket is now listening')
        while True:
            conn, addr = s.accept()#Accepting the connection and address of the client
            df7 = conn.recv(1024)#Receiving the data in df7
            df8 = json.loads(df7)
            df2 = list(df8.values())#df7 is in dict format, so we convert it into list and take only values of that data.

            mycursor1=mydb1.cursor()
            mycursor1.execute("SELECT * FROM form_simpleform WHERE id=1")#Used for validating QR code data 
            df3=mycursor1.fetchone()
            if df2 == (list(df3)): #If data is valid, it extracts the valid data of that id and send back the data to the client.
                print('Data of QR Code also Exists')
            else:
                print("Data does not exists")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我们如何比较两个对象数组并显示两个数组中的项目

我们如何比较两个整数?

我们如何更快地比较两个不同表之间的数据

我们如何在python的“ openpyxl”包中绘制两个系列的数据(折线图)

我们如何在键和值上比较两个不区分大小写的哈希图

当我们在while循环中给两个比较运算符时,python如何理解?

我们如何不能将两个枚举值与'<'进行比较?

我们如何比较两个特里的相似性?

我们如何在Django中从数据库中随机获取两个以上的用户?

我们如何在R中添加两个具有不同行和列的矩阵?

我们如何在javascript中添加两个事件监听器click和keydown

我如何比较两个对象

PHPMYADMIN | 我们如何在两个不同的MySQL数据库(在同一服务器内)中同步两个表?

我们如何合并两个具有分组条件和两个条件的查询的结果

我们如何产生数字 xy,其中 x 和 y 是两个参数或函数?

我们如何仅使用一个对象同时使用sql和oracle数据库连接

我们如何从 codeigniter 中的模型中获取数组的两个值

我们如何在反应中在输入中设置两个值

如何比较sql中两个表的数据?

我们如何将两个以上的对象数组合并为一个对象数组

我们如何获取MongoTemplate中两个限制之间的列值?

我们如何交换数组中的两个元素?

我们如何在React jsx中添加两个不同的值

我们如何在Java中并行运行两个线程

我们可以在 Java8 中对两个不同的 Streams 执行比较操作吗?

在PostgreSQL中,我们可以直接比较两个带有不同时区的时间戳吗?

我有两个json文档,我需要使用python比较第一个对象的键是否存在于第二个对象中。他们两个都嵌套

在Spring-Boot中,我们如何连接到同一项目中的两个数据库(Mysql数据库和MongoDB)?

比较Java中两个对象的内容