0

can anyone show me the syntax for inserting a python tuple/list into a mysql database? i also need to know if it is possible for user to pass certain rows without inserting anything... for example: a function is returning this tuple:

return job(jcardnum, jreg, jcarddate, jcardtime, jcardserve, jdeliver)

suppose the user didnt enter anything in jreg would python itself enter null in to the related row in the DB or would i run in to trouble?

1
  • 1
    job isn't a tuple, it's a function. This question has too little information to give an answer to. Commented Sep 5, 2009 at 1:02

2 Answers 2

7

As a comment says, the job(...) part is a function (or class) call -- whatever is returned from that call also gets returned from this return statement.

Let's assume it's a tuple. What if "the user didn't enter anything in jreg" -- well then, depending on a lot of code you're not showing us, that could be a runtime error (name jreg being undefined), an empty string or other initial default value never altered, or None; in the latter case that would indeed eventually become a NULL in the DB (if acceptable per the DB schema, of course -- otherwise, the DB would reject the insert attempt).

Once you DO finally have a correct and proper tuple T that you want to insert,

`mycursor.execute('INSERT INTO sometable VALUES(?, ?, ?, ?, ?, ?)', T)

is going to be close to the syntax you want -- if T has six items (and sometable has six columns, of course). Each ? is a placeholder and gets replaced with the corresponding item. mycursor will need to be an instance of Cursor, presumably obtained by some earlier call to myconnection.cursor where myconnection is an instance of Connection, built by the proper call to connect from the DB API module you're using with the right argumentrs.

If you show us about 100 times more code and DB schemas and tell us exactly WHAT you're trying to accomplish, we, collectively speaking, could no doubt be MUCH more useful and specific -- but based on the sub-epsilon amount of info you supply that's about as much as we, collectively speaking, can offer;-).

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for a stunningly complete answer to a partially-asked, and mostly-guessed, question.
0

Maybe, cursor.executemany is what you need?

suppose the user didnt enter anything in jreg would python itself enter null in to the related row in the DB or would i run in to trouble?

I think you should just try, you probably see the warning or error if NULL is not allowed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.