我有一张PHP表,我试图将其翻译成Python。其中一个函数是mysql_fetch_array。有没有人知道Python的等价物?如您所见,我的大多数代码都已翻译过。第41行是例外,它仍然是PHP。
#send
import MySQLdb
servername = "localhost"
username = "username"
password = "password"
dbname = "some_database"
import time
today = time.strftime("%d-%m-%Y")
now = time.strftime("%H:%M:%S")
#use Django for this
field = request.GET['field']
value = request.GET['value']
conn = MySQLdb.connect(host="localhost",user="username",passwd="password",db=)
if not conn:
print 'Could not connect: mysql_error'# . mysql_error();
#below may need editing
con_result = conn.select("some_database")
if not con_result:
print 'Could not connect to specific database: '# . mysql_error();
datenow = today + ' ' + now
hvalue = value
sql = "INSERT INTO `DataTable`(`logdata`, `field`, `value`) VALUES (\"datenow\",\"field\",value)"
result = sql.fetchall()
if not result:
print 'Invalid query'
print "THE DATA HAS BEEN SENT!"
conn.close()
#get
fieldToGet = request.GET['field']
sql = "SELECT * FROM `DataTable` WHERE `field` = \"fieldToGet\""
result = sql.fetchall()
if not result:
print 'Invalid query'
print "THE DATA HAS BEEN RECEIVED!!"
while(row == mysql_fetch_array($result, MYSQL_ASSOC))
答案 0 :(得分:2)
php
中的mysql_fetch_array
等效python
为fetchall()
,即:
import MySQLdb as mdb
con = mdb.connect('localhost', 'user', 'pass', 'db');
with con:
cur = con.cursor()
cur.execute("SELECT * FROM `DataTable` WHERE `field` = '"+fieldToGet+"'")
rows = cur.fetchall()
for row in rows:
email = row[0]
password = row[1]