Re: Using Python to Populate sqlite3 database with mtgsdk

At Ethin, thank you for the reply, I have tried setting all of my datatypes to text already, and I was getting an error, once I changed the datatypes to match the python datatypes, it seemed to function better. I am not sure why. I tried changing everything to blob, but I got some other error, so I changed it back. I figured out the error I was having though.

I was using filter() to filter out all of the None values from my tuple, but as a result it was returning an iterator not a tuple, and the execute() function only takes tuples as an argument.
So, I casted the iterator to a tuple and I am now getting a new error.


Traceback (most recent call last):
  File "mtgsdkTester.py", line 90, in <module>
    main()
  File "mtgsdkTester.py", line 16, in main
    insertCard(connection, cardProperties)
  File "mtgsdkTester.py", line 86, in insertCard
    cur .execute(sql, temp)
sqlite3.OperationalError: table Cards has 19 columns but 20 values were supplied

I have counted the number of elements in the insert statement, number of items in the tuple (temp), and the number of columns in my sql, but I keep getting 20 for everything. Does anyone see the problem?

Here is my updated code for my sql and python:
note: I change the loyalty from text to int, didn't seem to make a difference since I am testing a non planeswalker, but I tested the value of the variable with a planeswalker, and it is an int.

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" INT,
  "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)
    temp = tuple(temp)
    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