Jump to content
teckV

SQL Introduction for Python Programmers

Recommended Posts

teckV

Um documentos muito bom explicando as questões de programação de bases de dados SQL com Python.

No caso usa-se PostgreSQL e Python...

artigo completo

exemplos para criar água na boca....

Ligação à base de dados e uma pequena query com o resultado:

#!/usr/bin/env python
#
import pg
conn = pg.connect(dbname="school"host="localhost"user="postgres")
result = conn.query("select * from course")
print result

>> resultado <<
number|name                |credits|teacherid
------+--------------------+-------+---------
   101|Algebra I           |      5|     1003
   201|Algebra II          |      5|     1002
   150|World History       |      2|     1001
   301|Calculus            |      5|     1002
   314|Computer Programming|      4|     1001
   204|Spanish II          |      3|     1003
(6 rows)

Um pequeno programa para report:

#!/usr/bin/env python
#
#       Simple report of classes taught by each teacher
#
import pg

db = pg.connect(dbname="school"host="localhost"user="postgres")

def main () :
    result = db.query("select * from teacher order by name")
    trows = result.dictresult()
    for trow in trows :
        tId = trow['id']
        print "%s" % trow['name']
        query ="select * from course where teacherId=%d order by name"
        result = db.query(query % tId)
        crows = result.dictresult()
        for crow in crows :
            print "  %s" % crow['name']  # Name of course

    if __name__ == "__main__" : main()

teckV

Share this post


Link to post
Share on other sites

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

Important Information

By using this site you accept our Terms of Use and Privacy Policy. 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.