help with this simple DB script

J

John Salerno

Ok, before I contact my server host, I figured I should make sure I'm
not just making a Python mistake. I get an Internal Server Error with
this script:

!#/usr/bin/python

import MySQLdb

db = MySQLdb.connect(host='xxx',
user='xxx',
passwd='xxx',
db='xxx') # changed this stuff

cursor = db.cursor()
cursor.execute("CREATE TABLE test (first varchar(10), second number(2))")
cursor.execute("INSERT INTO test (first, second) VALUES ('Hello', 33)")
cursor.execute("SELECT first, second FROM test")
cursor.fetchall()
cursor.close()
db.close()



All I do is run it directly with my URL, not sure if there's more to it.
 
J

John Machin

Please learn to provide needed information when asking questions.
1. What does "run it directly with my URL" mean??
2. Show the traceback that you got.
 
T

trebucket

This is probably causing a problem:
!#/usr/bin/python

It should be "#!", not "!#".

If that doesnt' work, add this line at the top of your script, to check
that the script is begin executed:

print "Content-Type: text/html\n\n"
print "Hello, World!"

If you still get an Internal Server Error put the following before you
import MySQLdb:

import cgitb; cgitb.enable()

Hope this helps,

- Alex Ross
 
J

John Salerno

This is probably causing a problem:
!#/usr/bin/python

It should be "#!", not "!#".

Ugh! So stupid! Thanks for correcting that, but it wasn't the only problem.
If that doesnt' work, add this line at the top of your script, to check
that the script is begin executed:

print "Content-Type: text/html\n\n"
print "Hello, World!"

Yes, this part gets executed.
If you still get an Internal Server Error put the following before you
import MySQLdb:

import cgitb; cgitb.enable()

This produces a page with a ProgrammingError
(http://www.johnjsalerno.com/server_db_test.py), but the main part I
guess is this:

ProgrammingError: (1064, "You have an error in your SQL syntax. Check
the manual that corresponds to your MySQL server version for the right
syntax to use near 'number(2))' at line 1")

I'm not sure what I'm doing wrong with the syntax though. I don't think
I need semicolons (a sample I saw didn't use them).
 
J

John Salerno

BartlebyScrivener said:
Are you able to connect to the DB using MySQL administrator?

Yes, through my hosting control panel. That's how I created the DB, but
I want to try to do everything else with mysqldb.
 
J

John Salerno

John said:
Ok, before I contact my server host, I figured I should make sure I'm
not just making a Python mistake. I get an Internal Server Error with
this script:

Ok, got some help from Dennis Bieber and he solved it. I was using
number(), which is a SQL but not a MySQL command. I needed to use
something else, like int().

Thanks!
 
D

Dennis Lee Bieber

Ok, got some help from Dennis Bieber and he solved it. I was using
number(), which is a SQL but not a MySQL command. I needed to use
something else, like int().
Out of curiosity, which SQL reference listed "number" as a data
type?

I just scanned MySQL, FireFox, PostgreSQL, and MSDE (SQL Server)
texts... They all have variations of "integer", "float", "double",
"decimal" or "numeric", but no "number". MaxDB (SAP-DB) has "fixed",
"float", "integer" -- it will accept "numeric", "double", and "decimal"
but remaps those into "fixed" or "float", as appropriate.

The Access table designer shows "number", but when you look below
you find that "number" merely subsumes a list-box of various "integer",
"float", "decimal" types.

SQLite tends to use the type declaration as an optimization hint,
but takes anything as the data itself. [And it seems I've left my
O'Reilly book in the theater last month]

(Now, to really confuse matters: "second" with () is a function in MySQL
-- and my copy of the MySQL Query Browser is highlighting "first" also,
though I couldn't find it in the function list, might be a v5 entry)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

John Salerno

Dennis said:
Out of curiosity, which SQL reference listed "number" as a data
type?

I was taking the sqlcourse.com tutorial yesterday, and it shows it here:
http://sqlcourse.com/create.html

All the interactive examples in the tutorial seem to work with number().
But after work today I think I will go pick up a copy of the MySQL
Pocket Reference, just to have something to easily consult from now on.
 
D

Dennis Lee Bieber

I was taking the sqlcourse.com tutorial yesterday, and it shows it here:
http://sqlcourse.com/create.html
Forgive me if I'm not too thrilled at the level of detail there...

I suspect that, for safety, they are pre-parsing any submitted
statements before letting them through to the real database engine...
And might have an Oracle back-end which does allow for "number" as a
type.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top