1

I'm very newbie to python and how its mysql connector works in any web application. I want to try something easy actually but got no idea how to work it out. I have this simple python script to make random numbers and allocate it in a variable. (numbers.py)

import random
number = random.radint(1,1000)
print number

and this very simple python script to make a table in a mysql database (try.py)

import MySQLdb as mdb

con = mdb.connect('localhost', 'testuser', 'test623', 'testdb');

with con:
         cur = con.cursor()
         cur.execute("DROP TABLE IF EXISTS Table_Try")
         cur.execute("CREATE TABLE Table_Try(Value INT)")

My question is how do I insert the field "Value" with the value from variable "number" in number.py, since they are different scripts. When the value is inserted, how do I displayed in a php page so it can show random values repeatedly in a endless loop when opened? I have searched tutorial links about this in google but none could answer my problem here.

3

1 Answer 1

0

you can insert values into the database as follows:

c.execute("INSERT INTO db_name.Table(value) VALUES(%s)",[5])

In the above code c is my cursor and Table is my table in that database. If you want to insert many values you can use executemany instead of execue

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.