springcj1
springcj1

Reputation: 137

pymysql how to pass parameters for a stored procedure

I am trying to use a csv as a data source and call a stored procedure from mysql database to populate the DB. I am currently getting the exception: exception occured: (1318, 'Incorrect number of arguments for PROCEDURE mydb.update_servers; expected 3, got 0')

import pymysql
#import myconnutils
import datetime
import xlrd
import re
import os
import csv



#open csv file
with open('DBstatus.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row['Host Name'], row["OS Type"], row['Host Description'])





#create database connection

databaseConnection = pymysql.connect(host='localhost', user='xxx', password='xxx', db='mydb', cursorclass= pymysql.cursors.DictCursor)

try:
    cursorObject = databaseConnection.cursor()
    # parms = (row['Host Name'], row["OS Type"], row['Host Description'])

    # resultArgs = cursor.callproc('update_servers', inOutParams)
    cursorObject.execute("call update_servers")

    for result in cursorObject.fetchall():
        print(result)
except Exception as e:
    print("exception occured: {}".format (e))

finally:
    databaseConnection.close()

Stored Procedure:

CREATE DEFINER=`root`@`%` PROCEDURE `update_servers`(
    IN p_name varchar(100),
    IN p_software_name varchar(100),
    IN p_description varchar(250)
    )
BEGIN   


        INSERT INTO servers (name, software_name, description) 
        VALUES (p_name, p_software_name, 
        p_description);
        SELECT LAST_INSERT_ID() into @recordId;




    SET @retVal = CAST(@recordId AS UNSIGNED);
    SELECT @retVal;  
END

Upvotes: 2

Views: 6738

Answers (1)

zwer
zwer

Reputation: 25779

You're not passing any parameters to your procedure even tho it expects three parameters (p_name, p_software_name and p_description), hence the error. try:

params = row['Host Name'], row["OS Type"], row['Host Description']
cursorObject.execute("call update_servers(?, ?, ?)", params)

Although I'd recommend using the official MySQL connector which has a special cursor case for stored procedures as you already have it commented out - MySQLCursor.callproc()

Upvotes: 2

Related Questions