Jump to content

Recommended Posts

Posted

I would like to connect to a database using python without Django. My site is at http://a-catering.heliohost.org/cgi-bin/menuDatabaseMakerWeb.py. It takes a text file and creates a table from it. Whenever I run it, I get a 500 Internal Server Error. I believe I've narrowed the problem down to the actual connection (connection = MySQLdb.connect). I created the database and the user/password through cpanel. Please let me know if I missed something.

 

Thank you.

 

The code is below:

 

#!/usr/bin/python

# date: 4/19/12

# description: Database creator for catering website

 

#The following imports the necessary modules

import cgitb; cgitb.enable()

import MySQLdb

 

print "Content-Type: text/html"

print

 

print "Attempting to create DB"

 

menuData = "/home/yakster8/public_html/menus.txt"

menus = open(menuData)

 

try:

connection = MySQLdb.connect(

host = "localhost",

user = yakster8_root,

passwd = password1234,

db = yakster8_menus

)

except:

print "Failed..."

 

print "Connected."

cursor = connection.cursor()

 

try:

cursor.execute("""drop table menu""")

except:

pass

 

sql = """CREATE TABLE menu

(name text,

category text,

imageLocation text,

dietaryConcerns text,

"""

 

# Create table

cursor.execute(sql)

connection.commit()

 

for line in menus:

line = line.strip() #removes leading and trailing white space

lineParts = line.split('\t')

sql = """

INSERT INTO menu

VALUES (%s,%s,%s,%s)

"""

 

parameters = (lineParts[0:4])

 

#The following runs the SQL against the cursor

cursor.execute(sql,parameters)

 

connection.commit()

 

print "Database created OK."

 

Nevermind, I forgot quotes in the connection script. Problem solved.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...