Using Python to Populate sqlite3 database with mtgsdk

Hi, I am working on trying to pull in data from the magic the gathering database into my local sqlite3 database, and I am using python 3.4 with mtgsdk. you can get mtgsdk from pip install mtgsdk.
I believe I have mostly gotten the program set up, but I am getting an error that I can't figure out.
I am just testing with 1 card right now to make sure it is working.

Part of my problem is that I am not certain what python data types I can use to pass into the insert into statement. I am under the assumption that only ints, floats ,and strings work ,but I don't know, I can't find anything on this to confirm this. Also, I am assuming that None does not work as sql NULL.So, a lot of my code is joining lists and dictionaries, and accounting for None's.

The error I am getting is:

Traceback (most recent call last):
  File "mtgsdkTester.py", line 89, in <module>
    main()
  File "mtgsdkT ester.py", line 15, in main
    insertCard(connection, cardProperties)
  File "mtgsdkTester.py", line 86, in insertCard
    cur.execute(sql, temp)
ValueError: parameters are of unsupported type

here is my sql and python.

SQL:

CREATE TABLE Cards (
  "id" INT,
  "name" TEXT,
  "multiverse_id" INT,
  "names" TEXT,
  "mana_cost" TEXT,
  "cmc" INT,
  "colors" TEXT,
  "color_identity" TEXT,
  "type" TEXT,
  "supertypes" TEXT,
  "subtypes" TEXT,
  "rarity" TEXT,
  "text" TEXT,
  "flavor" TEXT,
  "power" TEXT,
  "toughness" TEXT,
  "loyalty" TEXT,
  "rulings" TEXT,
  "legalities" TEXT,
  "set_name" TEXT
);

python:

import sqlite3

from mtgsdk import Card

def main():
    dbFile = "cards.db"
    connection = createConnection(dbFile)
    cards = Card.where(name="The Great Aurora").all()
    cardProperties = getCardProperties(cards[0])
    insertCard(connection, cardProperties)


def getCardProperties(card):
    names = joinList(card.names)
    colors = joinList(card.colors)
    color_identity = joinList(card.color_identity)
    supertypes = joinList(card.supertypes)
    subtypes = joinList(card.subtypes)
    legalities = formatLegalities(card.legalities)
    
    return (1, card.name, card.multiverse_id, names, card.mana_cost, card.cmc, colors, color_identity, card.type, supertypes, subtypes, card.rarity, card.text, card.flavor, card.power,
        card.toughness,card.loyalty, card.rulings, legalities,card.set_name)


def joinList(cardProperty):
    if cardProperty != None:
        return "|".join(cardProperty)
    
    return cardProperty


def formatLegalities(legalities):
    if legalities == None:
        return legalities
    
    legalitiesStr = ""
    legalitiesLength = len(legalities)
    
    for index in range(0, legalitiesLength):
        legalitiesStr += legalities[index]["format"] + ": "
        legalitiesStr += legalities[index]["legality"]
        
        if (index + 1) != legalitiesLength:
            legalitiesStr += "|"
    
    return legalitiesStr


def createConnection(dbFile):
    try:
        connection = sqlite3.connect(dbFile)
        return connection
    except Error as e:
        print(e)
    
    return None


def insertCard(connection, cardProperties):
    sql = "INSERT INTO Cards VALUES("
    cur = connection.cursor()
    cardPropertiesLength = len(cardProperties)
    
    for index in range(cardPropertiesLength):
        if cardProperties[index] == None:
            sql += "NULL"
        else:
            sql += "?"
        
        if (index + 1) != cardPropertiesLength:
            sql += ","
    
    sql += ");"
    temp = filter(None, cardProperties)    
    cur.execute(sql, temp)


main()

Thanks,

TJ Breitenfeldt

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : TJ . Breitenfeldt via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Zersiax via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : TJ . Breitenfeldt via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : TJ . Breitenfeldt via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : TJ . Breitenfeldt via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector

Reply via email to