0

I have a php form which saves these values to the database:

  • id
  • rand (a randomly generated string)
  • x_val
  • y_val

I am calling these values back from database in a python program using mysqldb:

import MySQLdb
db = MySQLdb.connect("localhost","root","","test" )
cursor = db.cursor()
sql = "SELECT id,rand,x_val,y_val FROM table"
 try:
cursor.execute(sql)
results = cursor.fetchall()
results = {}
for row in results:
 results[row[1]] = [row[0], row[2], row[3]]
 rand = row[1]
 x_val = row[2]
 y_val = row[3]
except:
 print "Error: unable to fecth data"
db.close()

UPDATE: It gives the Error -> Error: unable to fecth data

In this piece (as you can see) i want rand (i.e. row[1]) to serve as the identifier for the row. However, I am not able to find a way about how to use this as an identifier since rand = row[1] is called after the sql variable. In this example, I have used a static '63kfjf' rand value, which is just to show you the working. Is there a way?

asked Jun 13, 2012 at 7:56
7
  • Could you elaborate? Why do you need it to be a random string? Why not make rand an "integer auto increment"? Commented Jun 13, 2012 at 7:59
  • I need it for processing as a unique identifier for the row in the python program...don't need it to be the id. Commented Jun 13, 2012 at 8:01
  • Set it to "integer auto increment" you'll get a string of numbers that do not have any matches. You don't have to use it as a key. Commented Jun 13, 2012 at 8:04
  • SELECT * is a bad idea. If the order of the columns in your database ever changes your code might break. Better to do SELECT id, rand, x_val, y_val .... Commented Jun 13, 2012 at 8:06
  • well..even if i set it as an auto incrementing entity in the db, how should i call it in the sql = "SELECT * FROM bar_graph WHERE rand='rand'" line? It is being called after the table is selected. I need it like sql = "SELECT * FROM table WHERE rand=$this" type of thing.. Commented Jun 13, 2012 at 8:09

2 Answers 2

1

After selecting your datas from DB, you can create a dictionnary with "rand" value as key:

Something like this:

dicResults = {}
for row in results:
 dicResults[row[1]] = [row[0], row[2], row[3]]
answered Jun 13, 2012 at 8:12
Sign up to request clarification or add additional context in comments.

Comments

0

I think you're trying to randomly select a row? If that is correct, this will work.

  1. Change rand to an auto incrementing integer.

  2. SELECT MAX(rand) FROM table; to determine the highest 'random' value.

  3. Generate a random value between 0 and MAX(rand)

  4. SELECT * FROM table WHERE rand = $randomGeneratedValue;

answered Jun 13, 2012 at 8:47

1 Comment

rand is generated from a php script and it is just a unique random code that comes from the input fields of the form. So, it is always unique, which is the reason why I want this to be served as the identifier which can be further processed in the python script. Plus, putting rand = $randomGeneratedValue may not do anything because this python script has no connection with the php variable. Two different scripts at work here. One posts the data and the other one retrieves it. :/

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.