如何使用带有python和mysql库的列名检索数据

时间:2019-04-06 03:24:33

标签: mysql python-3.7

我知道我们可以按索引从 python mysql库中检索数据,但是我需要使用列名来访问它们,有什么方法可以使用mysql访问它们,因为我看到有办法 MYSQLDB ,但此刻我必须在100个文件中更改整个代码才能使用该方式。我只需要使用mysql的列名来访问它们

类文件//

    func fillPixelBufferFromImage(_ image: UIImage, pixelBuffer: CVPixelBuffer) {
        CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))

        let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer)
        let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
        let context = CGContext(
            data: pixelData,
            width: Int(image.size.width),
            height: Int(image.size.height),
            bitsPerComponent: 8,
            bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer),
            space: rgbColorSpace,
            bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue
        )

        context!.clear(CGRect(x: 0, y: 0, width: CGFloat(self.outputSize.width), height: CGFloat(self.outputSize.height)))
        context?.draw(image.cgImage!, in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))

        CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))
    }

对象调用

import mysql.connector
from mysql.connector import Error

class Database:
    def __init__(self):
        try:
            self.connetion = mysql.connector.connect(
                host="localhost",
                user="root",
                passwd="",
                database="leame",
                use_pure=True
            )

        except Error as e:
            print(e)

    def query(self, query, param="NULL"):
        mycursor = self.connetion.cursor(prepared=True)

        if param != "NULL":
            mycursor.execute(query, params=param)
        else:
            mycursor.execute(query)

        myresult = mycursor.fetchall()
        return myresult

通过这种方法可以正常工作,但是我需要其他方法来避免索引
通过这种方式,我可以访问字段,但是我需要使用x ['column_name']这样的字段来访问它们,有没有办法使用MYSQL库

0 个答案:

没有答案