1
我相信它的東西很簡單,但是在Python 3中,其實我不知道爲什麼從這個代碼的結果返回像b'something」sqlalchemy像b'result一樣返回結果可能是unicode問題?
實際代碼:
import sqlalchemy
from sqlalchemy.sql import select
from sqlalchemy import Table, MetaData
def init():
try:
server = 'xx'
db = 'xx'
login = 'xx'
passwd = 'xx'
engine_str = 'mysql+mysqlconnector://{}:{}@{}/{}'.format(login, passwd, server, db)
engine = sqlalchemy.create_engine(engine_str, echo=False, encoding='utf-8')
connection = engine.connect()
metadata = MetaData()
t_servers = Table('servers', metadata, autoload=True, autoload_with=engine)
s = select([t_servers])
result = connection.execute(s)
for row in result:
print(row['address'])
except Exception:
raise
finally:
connection.close()
if __name__ == '__main__':
init()
查詢結果:
b'localhost:7777'
Process finished with exit code 0
在此先感謝。
英語不是我最強的語言......那是什麼意思? –
「b」前綴表示您的查詢結果正在返回實例類型「bytes」,而不是「str」。你可以閱讀更多關於'bytes' [here](http://docs.python.org/3.0/library/functions.html#bytes)。 – Drewness
通過將我的db錶轉換爲utf8_bin來解決 –