Letractively / karrigell

Automatically exported from code.google.com/p/karrigell
0 stars 0 forks source link

How to connect to postgresql database? #1

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
If I want to py-postgresql,
how to connect to postgresql database

http://python.projects.postgresql.org/

Original issue reported on code.google.com by zj8...@gmail.com on 28 Jun 2011 at 3:40

GoogleCodeExporter commented 8 years ago
Hi,

Sorry for the late answer, I haven't checked this page for some time

I don't have postgresql but since there seems to be a Python 3 version of the 
adapter, it should work inside a Karrigell script like in any Python script

Untested example copied from the "code sample" page on the site you mention :

def index():
    import postgresql
    db = postgresql.open("pq://user:password@host/name_of_database")
    db.execute("CREATE TABLE emp (emp_name text PRIMARY KEY, emp_salary numeric)")

    # Create the statements.
    make_emp = db.prepare("INSERT INTO emp VALUES ($1, $2)")
    raise_emp = db.prepare("UPDATE emp SET emp_salary = emp_salary + $2 WHERE emp_name = $1")
    get_emp_with_salary_lt = db.prepare("SELECT emp_name FROM emp WHERE emp_salay < $1")
    # Create some employees, but do it in a transaction--all or nothing.
    with db.xact():
        make_emp("John Doe", "150,000")
        make_emp("Jane Doe", "150,000")
        make_emp("Andrew Doe", "55,000")
        make_emp("Susan Doe", "60,000")
    >>>
    # results
    result = ''
    with db.xact():
        for row in get_emp_with_salary_lt("125,000"):
            result += str(row["emp_name"])+'<br>'

    return result

Original comment by pierre.q...@gmail.com on 18 Jul 2011 at 1:34