'Sqlalchemy insert into mysql db gives error when it encounters whitespace in value

I'm writing a small program to insert 1000 values into a db.

Some of my values are names which contain whitespace. As soon as the insert statement hits a whitespace, I get an error.

Why?

eg: sqlalchemy.exc.ProgrammingError: (mysql.connector.errors.ProgrammingError) 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Oyster,Pleurotus,null,WhiOy - 0,null,0,2,2021-01-02,veg1,null,null,null,null,...' at line 1 [SQL: INSERT INTO cultures VALUES (0,White Oyster,Pleurotus,null,WhiOy - 0,null,0,2,2021-01-02,veg1,null,null,null,null,2021-06-12,null,null,null,null,null,null,null,null);]

problem code:

sql = ('INSERT INTO cultures VALUES ('+str(id)+nameSql[randomName]+sciSql[randomName]+"null"+nickSql+"null,0,"+str(stage)+','+str(startDate)+values+');')

full code:

import datetime
import random
import sqlalchemy
from sqlalchemy import create_engine

engine = create_engine('mysql+mysqlconnector://removedforstackoverflow', echo=True)

id = 0
nameSql = {
    0: ',Button,',
    1: ',Brown Oyster,',
    2: ',Black Oyster,',
    3: ',Blue Oyster,',
    4: ',Elm Oyster,',
    5: ',King Oyster,',
    6: ',Pink Oyster,',
    7: ',Yellow Oyster,',
    8: ',White Oyster,',
    9: ',Chestnut,',
    10: ',Enoki,',     
    11: ",Lion's Mane,",
    12: ',Maitake,',
    13: ',Nameko,',
    14: ',Pioppino,',
    15: ',Reishi,',
    16: ',Shiitake,',
    17: ',Albino Riptide,',
    18: ',Ape 338,',
    19: ',Big Brazil,',
    20: ',DC Mak Longboi,',
    21: ',Fat Ghost,',
    22: ',Omni Mutant,',
    23: ',Shatiki,',
    24: ',Yeti 125,',
}
sciSql = {
    0: 'Agaricus bisporus,',
    1: 'Pleurotus australis,',
    2: 'Pleurotus ostreatus,',
    3: 'Pleurotus columbinus,',
    4: 'Hypsizygus ulmarius,',
    5: 'Pleurotus eryngii,',
    6: 'Pleurotus djamor,',
    7: 'Pleurotus citrinopileatus,',
    8: 'Pleurotus,',
    9: 'Pholiota adiposa,',
    10: 'Flammulina velutipes,',     
    11: "Hericium erinaceus,",
    12: 'Grifola frondosa,',
    13: 'Pholiota nameko,',
    14: 'Cyclocybe aegerita,',
    15: 'Ganoderma lucidum,',
    16: 'Lentinula edodes,',
    17: 'null,',
    18: 'null,',
    19: 'null,',
    20: 'null,',
    21: 'null,',
    22: 'null,',
    23: 'null,',
    24: 'null,',
}

nickBase = {
    0: 'Agari',
    1: 'BroOy',
    2: 'BlaOy',
    3: 'BluOy',
    4: 'ElmOy',
    5: 'KinOy',
    6: 'PinOy',
    7: 'YelOy',
    8: 'WhiOy',
    9: 'Chest',
    10: 'Enoki',     
    11: "LioMa",
    12: 'Maita',
    13: 'Namek',
    14: 'Pioppi',
    15: 'Reis',
    16: 'Shiit',
    17: 'AlbRi',
    18: 'Ape3',
    19: 'BigBr',
    20: 'DCMLo',
    21: 'FatGh',
    22: 'OmnMu',
    23: 'Shati',
    24: 'Yeti1',
}


stageOptions = [0,1,2,3,4,5,6]
stages = random.choices(stageOptions, weights=(5,10,30,15,15,13,12), k=1000)

for i in range(1000):

    randomName = random.randrange(24)
    nickSql = ','+nickBase[randomName] + ' - ' + str(id)+','
    

    timeframeStart = datetime.date(2021, 1, 1)
    timeframEnd = datetime.date(2022, 3, 16)
    startDate = timeframeStart + datetime.timedelta(
        days=random.randrange((timeframEnd - timeframeStart).days)
    )
    endDate = startDate + datetime.timedelta(
        days=random.randrange((timeframEnd - startDate).days)
    )
    pinDate = endDate - datetime.timedelta(
        days=random.randrange(6, 20)
    )
    fininishedSql = ',null,null,null,null,'+str(endDate)+',null,null,null,null,null,null,null,null'
    aliveSql = ',null,null,null,null,null,null,null,null,null,null,null,null,null'


    values = ''
    stage = stages[id]
    if stage == 0:
        if random.randrange(3) < 3:
            values = ',fridge'+fininishedSql
        else:
            values = ',fridge'+aliveSql
    elif stage == 1:
        if random.randrange(3) < 2:
            values = ',fridge'+fininishedSql
        else:
            values = ',fridge'+aliveSql
    elif stage >= 2 and stage <= 4:
        if random.randrange(9) < 9:
            values = ',veg1'+fininishedSql
        else:
            values = ',veg1'+aliveSql
    elif stage == 5:
        if random.randrange(9) < 9:
            values = ',veg2'+fininishedSql
        else:
            values = ',veg2'+aliveSql
    else:
        if random.randrange(9) < 9:

            values = ',bud,null,null,null,null,'+str(endDate)+',null,null,null,null,null,null,'+str(pinDate)+',null'
        else:
            values = ',bud'+aliveSql


    sql = ('INSERT INTO cultures VALUES ('+str(id)+nameSql[randomName]+sciSql[randomName]+"null"+nickSql+"null,0,"+str(stage)+','+str(startDate)+values+');')
    result = engine.execute(sql)
    print('ok '+ id)
    id = id + 1


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source