python获取mysql数据库中的数据 您所在的位置:网站首页 python从数据库中提取数据 python获取mysql数据库中的数据

python获取mysql数据库中的数据

#python获取mysql数据库中的数据| 来源: 网络整理| 查看: 265

我是python新手,在mysql查询中传递参数时从mysql数据库获取数据时遇到问题,我认为mysql语法不正确。

下面是这样显示在屏幕上的错误。

内部服务器错误

服务器遇到内部错误或配置错误,无法完成您的请求。

请与webmaster@localhost上的服务器管理员联系,告知他们发生此错误的时间以及在此错误之前执行的操作。

有关此错误的详细信息可以在服务器错误日志中找到。

本地主机端口80上的Apache/2.4.6(Ubuntu)服务器

这是我的Select查询代码,我想从Url的get参数获取数据。#!/usr/bin/python2.7

import cgi

import cgitb

cgitb.enable()

print "Content-type: text/html\n\n"

print "

Hello Python"

#!/usr/bin/python

import MySQLdb

# Create instance of FieldStorage

form = cgi.FieldStorage()

# Get data from fields

first_name = form.getvalue('first_name')

last_name = form.getvalue('last_name')

# Open database connection

db = MySQLdb.connect("localhost","root","123456789","testdrive" )

# prepare a cursor object using cursor() method

cursor = db.cursor()

# Prepare SQL query to INSERT a record into the database

sqlstmt = "SELECT * FROM EMPLOYEE WHERE FIRST_NAME = %(first_name)s AND LAST_NAME = %(last_name)s"

try:

# Execute the SQL command

cursor.execute(sqlstmt, {'first_name': first_name, 'last_name': last_name})

# Fetch all the rows in a list of lists.

results = cursor.fetchall()

for row in results:

fname = row[0]

lname = row[1]

age = row[2]

sex = row[3]

income = row[4]

# Now print fetched result

print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \

(fname, lname, age, sex, income )

except:

print "Error: unable to fecth data"

# disconnect from server

db.close()



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有