Jump to content

Connect To Database Without Django


yakster8

Recommended Posts

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...