gpt4 book ai didi

python - 可以在 MySQL 表中存储字节(Fernet token )吗? Python

转载 作者:行者123 更新时间:2023-11-29 16:30:59 24 4
gpt4 key购买 nike

我正在尝试将此 Fernet 加密 token 插入到我数据库中的表中。它是有关侦察兵的加密医疗信息

b'gAAAAABcIRmX3txIuOrw6FoSxy7I1vorA8hTTzMcXQGwch_jRBtWTsR9TwVyH125K0R6zG-BTvhv_SpZuW-Hs1WotaabBVj5tQ=='

通过使用此插入语句

    sqlcommand = "INSERT INTO scoutinfo (scoutID, firstname,secondname,age,gender,ethnicity,address,postcode,medicalinfo,parentID,patrolID,userID) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
ScoutInput = (
str(ScoutID), FName.get(), SName.get(), str(Age.get()), str(Gender.get()), str(Ethnicity.get()), Address.get(),
Postcode.get(), EcryptMedInfo, str(ParentID[0]), str(PatrolID), str(UserID))
mycursor.execute(sqlcommand, ScoutInput)

运行时,程序会执行,但是插入不会应用于表,不,我没有忘记mydb.commit()。我相信 bytes 是 MySQL 不支持的数据类型,因此无法存储。在这种情况下,我将如何克服这个问题,以便我能够通过以下方式解密存储的 token :

Ecy.decrypt(EcryptMedInfo)

最佳答案

如果有b""并且你想将其转换为字符串。您应该使用 decode() 而不是 str()。因为如果使用str()很难反转,但是如果使用decode()就很容易了。

a = b"\x00\x00"
print((a,a.decode(),str(a)))
print(a == a.decode().encode())
#(b'\x00\x00', '\x00\x00', "b'\\x00\\x00'")
#True

When i use mysql.connector, i don't have to transform them into string by myself.

import mysql.connector as mysql
from base64 import b64encode, b64decode
conn = mysql.connect(user="kr",passwd="kr",db="kr")
cur = conn.cursor()

def go(stat,param=None):
try:
cur.execute(stat,param)
conn.commit()
except Exception as e:
conn.rollback()
print(e)
go("""
CREATE TABLE test(
col varchar(90) NOT NULL
)""")
something = b64encode(b"\x00\x00")
print(something,type(something))
go(stat="""INSERT INTO test (col) VALUES (%s)""", param=[something])

cur.execute("SELECT col from test")
result = cur.fetchone()[0]
print([b64decode(result)])
#b'AAA=' <class 'bytes'>
#[b'\x00\x00']

I believe that bytes is an unsupported datatype for MySQL and thus cannot be stored.

Mysql可以存储它。 Mysql byte array storage

关于python - 可以在 MySQL 表中存储字节(Fernet token )吗? Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53916397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com