amit
amit

Reputation: 10261

inserting a tuple into a mysql db

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?

Upvotes: 0

Views: 5971

Answers (2)

L1ker
L1ker

Reputation: 990

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.

Upvotes: 0

Alex Martelli
Alex Martelli

Reputation: 881765

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;-).

Upvotes: 7

Related Questions