Jump to content

Python simple script need help


Recommended Posts

Hello I am a web developer and new php programmer... a girl I know needs help with a project..

 

she need to write a python script that can search a database with

 

a student database...... with subject / class  / section  / students...

 

could someone please point me to some sample code to look at to perform a simalar result?

 

also what type of database is generally used ??

 

thanks paul

Link to comment
Share on other sites

You / she might want to take a look at the MySQLdb module for Python. Its pretty straight forward to use...

 

#!/usr/bin/python

import MySQLdb
db = MySQLdb.connect(host="localhost", user="foo", passwd="",db="foodb")
cursor = db.cursor()
sql = "SELECT subject class section students FROM tbl"
cursor.execute(sql)
rows = int(cursor.rowcount)
if rows > 0:
  for x in range(0,rows):
    row = cursor.fetchone()
    print row[0],row[1],row[2],row[3]

Link to comment
Share on other sites

I have only used mysql through php on my linux server...

 

I have the python and mysql link you gave me running on windows...

 

do you know how to initialize the database

 

and what should the connection read??

 

is it still localhost??? how to I set this database up fro connection...

 

when I do it for my webbased stuff I just use plesk...

 

please help

 

Paul

Link to comment
Share on other sites

I have the python and mysql link you gave me running on windows...

 

have you installed MySQL on this machine?

 

do you know how to initialize the database

 

Type...

 

mysql

 

on the command line or use phpmyadmin if your not up for it.

 

and what should the connection read??

 

is it still localhost???

 

localhost will (should) work if the MySql server is installed and configured on the local machine.

Link to comment
Share on other sites

I installed mysql for python

 

it seemed to install fine...

 

so I think what I need to do is initialize the databse (create)

 

I just have never used python

 

can I create the database withing the code? or do I need to use a phpadmin or comand line to create my database...??

 

thanks for your help

 

I have not spent alot of time but could not find much. I am looking into this after 11 hour workdays at my job... sorry to be such a pain.

 

Paul

Link to comment
Share on other sites

I installed mysql for python

 

That doesn't meen MySQL is installed. Go to the command line. (start->run->cmd) and type...

 

mysql --version

 

what do you get?

 

can I create the database withing the code?

 

Of course you could, just execute the valid query. Its much the same as php.

Link to comment
Share on other sites

ok I had some time to do a little digging

 

she told me she wants the script as simple as possible I found python to have a built in database

 

using the following code

 

import anydbm

db = anydbm.open("database", "c")
db["1"] = "one"
db["2"] = "two"
db["3"] = "three"
db.close()

db = anydbm.open("database", "r")
for key in db.keys():
    print repr(key), repr(db[key])

 

this code produces result:

 

'2' 'two'

'1' 'one'

'3' 'three'

 

now any idea on how I would make a searchable database out of this? with subject .... class... section.... student....??

 

give me just a little direction if you could... I would prefer to use this simple method because they are not really expecting much from this project....

 

thanks

 

Paul

Link to comment
Share on other sites

now any idea on how I would make a searchable database out of this?

 

Ive never used anydbm before but it does'nt look like it supports sql. This could makes things more difficult than need be. Maybe take a look at pySqlite if you dont want to go the route of installing MySQL.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.