Skip to content

Instantly share code, notes, and snippets.

@cloverstd
Forked from qpleple/torndb-examples.py
Created October 19, 2013 12:05

Revisions

  1. @qpleple qpleple revised this gist Jun 17, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions torndb-examples.py
    Original file line number Diff line number Diff line change
    @@ -9,11 +9,11 @@
    from torndb import Connection

    # Connect by IP address
    db = Connection('127.0.0.1', 'my_table', user='root', password='root')
    db = Connection('127.0.0.1', 'database_name', user='root', password='root')
    # Connect by IP address with port
    db = Connection('127.0.0.1:1234', 'my_table', user='root', password='root')
    db = Connection('127.0.0.1:1234', 'database_name', user='root', password='root')
    # Connect by socket
    db = Connection('/tmp/mysql.sock', 'my_table', user='root', password='root')
    db = Connection('/tmp/mysql.sock', 'database_name', user='root', password='root')
    # Connection over SSH, open a SSH tunnel with
    # ssh -L 1234:localhost:3306 [email protected]
    # then connect to 127.0.0.1:1234
  2. @qpleple qpleple revised this gist Apr 1, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions torndb-examples.py
    Original file line number Diff line number Diff line change
    @@ -9,11 +9,11 @@
    from torndb import Connection

    # Connect by IP address
    db = Connection('127.0.0.1', 'research', user='root', password='root')
    db = Connection('127.0.0.1', 'my_table', user='root', password='root')
    # Connect by IP address with port
    db = Connection('127.0.0.1:1234', 'research', user='root', password='root')
    db = Connection('127.0.0.1:1234', 'my_table', user='root', password='root')
    # Connect by socket
    db = Connection('/tmp/mysql.sock', 'research', user='root', password='root')
    db = Connection('/tmp/mysql.sock', 'my_table', user='root', password='root')
    # Connection over SSH, open a SSH tunnel with
    # ssh -L 1234:localhost:3306 [email protected]
    # then connect to 127.0.0.1:1234
  3. @qpleple qpleple revised this gist Apr 1, 2013. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions torndb-examples.py
    Original file line number Diff line number Diff line change
    @@ -10,8 +10,13 @@

    # Connect by IP address
    db = Connection('127.0.0.1', 'research', user='root', password='root')
    # Connect by IP address with port
    db = Connection('127.0.0.1:1234', 'research', user='root', password='root')
    # Connect by socket
    db = Connection('/tmp/mysql.sock', 'research', user='root', password='root')
    # Connection over SSH, open a SSH tunnel with
    # ssh -L 1234:localhost:3306 [email protected]
    # then connect to 127.0.0.1:1234

    # Retreive one object
    post = db.get("SELECT * FROM posts LIMIT 1")
  4. @qpleple qpleple revised this gist Mar 31, 2013. 1 changed file with 16 additions and 4 deletions.
    20 changes: 16 additions & 4 deletions torndb-examples.py
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,28 @@
    # Torndb is a very thin wrapper around MySQLdb that makes it even easier to use MySQL.
    # Because it is very light, one can just go through the one-file python source
    # to learn how to use it.

    # Installation: pip install torndb
    # Official doc: http://torndb.readthedocs.org/en/latest/
    # Source: https://github.com/bdarnell/torndb/blob/master/torndb.py

    from torndb import Connection

    # Connect by IP address
    db = Connection('127.0.0.1', 'research', user='root', password='root')
    # Connect by socket
    db = Connection('/tmp/mysql.sock', 'research', user='root', password='root')

    # Official doc: http://torndb.readthedocs.org/en/latest/

    # Retreive one object
    post = db.get("SELECT * FROM posts LIMIT 1")

    # Retreive several objects
    for post in db.query("SELECT * FROM posts"):
    print post.title

    # Insert one entry
    db.execute("INSERT INTO posts (user_id, content) VALUES (%s, %s)", 12, "hello world !")

    # INSERT
    db.execute("INSERT INTO posts (user_id, content) VALUES (%s, %s)", 12, "hello world !")
    # Insert multiple entries
    values = [(12, "hello"), (17, "world"), (22, "blah")]
    db.executemany("INSERT INTO posts (user_id, content) VALUES (%s, %s)", values)
  5. @qpleple qpleple revised this gist Feb 26, 2013. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions torndb-examples.py
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,13 @@

    # Official doc: http://torndb.readthedocs.org/en/latest/

    # SELECT
    for article in db.query("SELECT * FROM articles"):
    print article.title
    # Retreive one object
    post = db.get("SELECT * FROM posts LIMIT 1")

    # Retreive several objects
    for post in db.query("SELECT * FROM posts"):
    print post.title


    # INSERT
    db.execute("INSERT INTO posts (user_id, content) VALUES (%s, %s)", 12, "hello world !")
  6. @qpleple qpleple created this gist Feb 26, 2013.
    12 changes: 12 additions & 0 deletions torndb-examples.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    from torndb import Connection

    db = Connection('/tmp/mysql.sock', 'research', user='root', password='root')

    # Official doc: http://torndb.readthedocs.org/en/latest/

    # SELECT
    for article in db.query("SELECT * FROM articles"):
    print article.title

    # INSERT
    db.execute("INSERT INTO posts (user_id, content) VALUES (%s, %s)", 12, "hello world !")