<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: db code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 16:58:40 GMT</pubDate>
    <description>DZone Snippets: db code</description>
    <item>
      <title>how to create a new user in MySQL</title>
      <link>http://snippets.dzone.com/posts/show/5277</link>
      <description>// first, mysql -uroot mysql&lt;br /&gt;// then:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;  mysql% GRANT ALL PRIVILEGES ON *.* TO 'jm3_spoon'@'localhost' IDENTIFIED BY 'stirthatshit' WITH GRANT OPTION;&lt;br /&gt;  mysql% create database jm3_agitator;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 24 Mar 2008 04:06:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5277</guid>
      <author>jm3 (john manoogian III)</author>
    </item>
    <item>
      <title>Select DataBase Schema</title>
      <link>http://snippets.dzone.com/posts/show/4664</link>
      <description>// Select database schema.&lt;br /&gt;//This could be used to recreate or test for existence of columns/tables&lt;br /&gt;//or could also be used to create a database template system to enable the writing //of database schema into txt template file to be recreated again by reading the //txt file via an application&lt;br /&gt;//&lt;br /&gt;//You can also use SELECT * instead of defining each schema property (column)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;SELECT TABLE_CATALOG&lt;br /&gt;, TABLE_SCHEMA&lt;br /&gt;, TABLE_NAME&lt;br /&gt;, ORDINAL_POSITION&lt;br /&gt;, COLUMN_DEFAULT&lt;br /&gt;, IS_NULLABLE&lt;br /&gt;, DATA_TYPE&lt;br /&gt;, CHARACTER_MAXIMUM_LENGTH&lt;br /&gt;, COLLATION_NAME &lt;br /&gt;FROM &lt;br /&gt;INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = (N'Persons')&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 17 Oct 2007 11:30:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4664</guid>
      <author>dubby (Dave)</author>
    </item>
    <item>
      <title>Storm ORM by Canonical in WSGI enabled applications</title>
      <link>http://snippets.dzone.com/posts/show/4408</link>
      <description>&lt;code&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;# vim:ts=4:sw=4:et&lt;br /&gt;# (c) 2007 Vsevolod Balashov &lt;vsevolod@balashov.name&gt; under terms of LGPL 2.1&lt;br /&gt;# use Storm ORM &lt;https://storm.canonical.com&gt; in WSGI enabled applications&lt;br /&gt;&lt;br /&gt;"""WSGI middleware for Storm.&lt;br /&gt;&lt;br /&gt;This is the database access inteface for WSGI enabled (PEP 333) web applications.&lt;br /&gt;&lt;br /&gt;Pylons framework example:&lt;br /&gt;&lt;br /&gt;in wsgiapp.py&lt;br /&gt;&lt;br /&gt;from storm.database import create_database&lt;br /&gt;from middlestorm import MiddleStorm&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;# CUSTOM MIDDLEWARE HERE&lt;br /&gt;app = MiddleStorm(app, create_database(config['app_conf']['sqlalchemy.default.uri']))&lt;br /&gt;&lt;br /&gt;in controller:&lt;br /&gt;&lt;br /&gt;class DemoController(BaseController):&lt;br /&gt;    def index(self):&lt;br /&gt;        store = request.environ['storm.store']&lt;br /&gt;        ...&lt;br /&gt;"""&lt;br /&gt;&lt;br /&gt;from storm.database import Database&lt;br /&gt;from storm.store import Store&lt;br /&gt;from threading import local&lt;br /&gt;&lt;br /&gt;__all__ = ["MiddleStorm"]&lt;br /&gt;__author__ = "Vsevolod Balashov &lt;http://vsevolod.balashov.name&gt;"&lt;br /&gt;__version__ = "0.1"&lt;br /&gt;&lt;br /&gt;class SingleConn(Database):&lt;br /&gt;    """Database proxy class.&lt;br /&gt;    Share single database connection between all Store object instances in threads.&lt;br /&gt;    If you have readonly database or not use transactions why not?&lt;br /&gt;    """&lt;br /&gt;    def __init__(self, database):&lt;br /&gt;        self._database = database&lt;br /&gt;        self._connection = database.connect()&lt;br /&gt;    def connect(self):&lt;br /&gt;        return self._connection&lt;br /&gt;&lt;br /&gt;class MiddleStorm(object):&lt;br /&gt;    """WSGI middleware.&lt;br /&gt;    Add Store object instance in environ['storm.store']. Each thread contains own instance.&lt;br /&gt;    """&lt;br /&gt;&lt;br /&gt;    def __init__(self, app, database, single = False):&lt;br /&gt;        """Create WSGI middleware.&lt;br /&gt;        @param app: up level application or middleware.&lt;br /&gt;        @param database: instance of Database returned create_database.&lt;br /&gt;        @param: single: use single database connection in all threads. &lt;br /&gt;        """&lt;br /&gt;        assert isinstance(database, Database), \&lt;br /&gt;            'database must be subclass of storm.database.Database'&lt;br /&gt;        if single:&lt;br /&gt;            self._database = SingleConn(database)&lt;br /&gt;        else:&lt;br /&gt;            self._database = database&lt;br /&gt;        self._app = app&lt;br /&gt;        self._local = local()&lt;br /&gt;&lt;br /&gt;    def __call__(self, environ, start_response):&lt;br /&gt;        try:&lt;br /&gt;            environ['storm.store']  = self._local.store&lt;br /&gt;        except AttributeError:&lt;br /&gt;            environ['storm.store']  = \&lt;br /&gt;                self._local.__dict__.setdefault('store', Store(self._database))&lt;br /&gt;        return self._app(environ, start_response)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 10 Aug 2007 11:39:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4408</guid>
      <author>sevkin (Vsevolod Balashov)</author>
    </item>
    <item>
      <title>Simple mySQL backup script for cron</title>
      <link>http://snippets.dzone.com/posts/show/4172</link>
      <description>Simple mySQL backup script for cron - backs up all databases, saves the last 4 copies.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;&lt;br /&gt;# modify the following to suit your environment&lt;br /&gt;export DB_BACKUP="/backup/mysql_backup"&lt;br /&gt;export DB_USER="root"&lt;br /&gt;export DB_PASSWD="********"&lt;br /&gt;&lt;br /&gt;# title and version&lt;br /&gt;echo ""&lt;br /&gt;echo "mySQL_backup"&lt;br /&gt;echo "----------------------"&lt;br /&gt;echo "* Rotating backups..."&lt;br /&gt;rm -rf $DB_BACKUP/04&lt;br /&gt;mv $DB_BACKUP/03 $DB_BACKUP/04&lt;br /&gt;mv $DB_BACKUP/02 $DB_BACKUP/03&lt;br /&gt;mv $DB_BACKUP/01 $DB_BACKUP/02&lt;br /&gt;mkdir $DB_BACKUP/01 &lt;br /&gt;&lt;br /&gt;echo "* Creating new backup..."&lt;br /&gt;mysqldump --user=$DB_USER --password=$DB_PASSWD --all-databases | bzip2 &gt; $DB_BACKUP/01/mysql-`date +%Y-%m-%d`.bz2&lt;br /&gt;echo "----------------------"&lt;br /&gt;echo "Done"&lt;br /&gt;exit 0&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 20 Jun 2007 15:01:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4172</guid>
      <author>fak3r (fak3r)</author>
    </item>
    <item>
      <title>php image from database using PEAR::DB</title>
      <link>http://snippets.dzone.com/posts/show/3730</link>
      <description>This fetches an image from a MySQL database (I know, a database may not be the best place to keep images). The image is sent to the web client as an image/jpeg.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;require_once 'DB.php';&lt;br /&gt;&lt;br /&gt;define('DB_SERVER', 'localhost');&lt;br /&gt;define('DB_USER', 'php');&lt;br /&gt;define('DB_PASS', '');&lt;br /&gt;define('DB_DATABASE', 'images');&lt;br /&gt;define('DSN', 'mysql://'.DB_USER.':'.DB_PASS.'@'.DB_SERVER.'/'.DB_DATABASE);&lt;br /&gt;&lt;br /&gt;global $db;&lt;br /&gt;$db = DB::connect(DSN);&lt;br /&gt;&lt;br /&gt;if (PEAR::isError($db)) {&lt;br /&gt;    die($db-&gt;getMessage());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;if (isset($_GET['id'])) {&lt;br /&gt;  header('Content-Type: image/jpeg');&lt;br /&gt;  $id = $_GET['id'];&lt;br /&gt;  $sql = "select image_data from images where id = ?";&lt;br /&gt;  $result =&amp; $db-&gt;query($sql, $id);&lt;br /&gt;&lt;br /&gt;  if (PEAR::isError($result)) {&lt;br /&gt;    die($result-&gt;getMessage());&lt;br /&gt;  } else {&lt;br /&gt;    if ($result-&gt;fetchInto($row)) {&lt;br /&gt;      echo $row[0];&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;} else {&lt;br /&gt;  echo file_get_contents('broken.png');&lt;br /&gt;}&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 26 Mar 2007 08:16:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3730</guid>
      <author>mikewilsonuk (Mike Wilson)</author>
    </item>
    <item>
      <title>Result Object Methods</title>
      <link>http://snippets.dzone.com/posts/show/3011</link>
      <description>// Assuming that we have set result to being a result object we can use the following methods:&lt;br /&gt;&lt;br /&gt;len(result)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;     this will show the number rows returned (which would be 3 in the example above).&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;result.names()&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    a list of all the column headings, returning a list containing emp_id, first, last and salary&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;result.tuples()&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    returns a list of tuples in our example:&lt;br /&gt;&lt;br /&gt;    [(43, 'Bob', 'Roberts', 50000),&lt;br /&gt;     (101, 'Cheeta', 'leCat', 100000),&lt;br /&gt;     (99, 'Jane', 'Junglewoman', 100001)]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;result.dictionaries()&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    will return a list of dictionaries, with one dictionary for each row:&lt;br /&gt;&lt;br /&gt;    [{'emp_id': 42, 'first': 'Bob','last': 'Roberts', 'salary': 50000},&lt;br /&gt;     {'emp_id': 101, 'first: 'Cheeta', 'last': 'leCat', 'salary': 100000},&lt;br /&gt;     {'emp_id': 99, 'first': 'Jane', 'last': 'Junglewoman', 'salary': 100001}]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;result.data_dictionary()&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    returns a dictionary describing the structure of the results table. &lt;br /&gt;&lt;br /&gt;The dictionary has the key name, type, null and width. &lt;br /&gt;Name and type are self explanatory, null is true if that field may contain a null value and width is the width in characters of the field. &lt;br /&gt;Note that null and width may not be set by some Database Adapters.&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;result.asRDB()&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    displays the result in a similar way to a relational database. &lt;br /&gt;&lt;br /&gt;The DTML below displays the result below:&lt;br /&gt;    &lt;pre&gt;&lt;br /&gt;      &lt;dtml-var "list_all_employees().asRDB()"&gt;&lt;br /&gt;    &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;    ... displays ...&lt;br /&gt;&lt;br /&gt;    emp_id first last salary&lt;br /&gt;    42 Bob Roberts 50000&lt;br /&gt;    101 Cheeta leCat 100000&lt;br /&gt;    99 Jane Junglewoman 100001&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;result[0][1]&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    return row 0, column 1 of the result, bob in this example. &lt;br /&gt;&lt;br /&gt;Be careful using this method as changes in the schema will cause unexpected results. &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 17 Nov 2006 17:11:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3011</guid>
      <author>morlandi (Mario Orlandi)</author>
    </item>
    <item>
      <title>Db Connection</title>
      <link>http://snippets.dzone.com/posts/show/2840</link>
      <description>//  eGenix mxODBC Database Connection: connection string sample&lt;br /&gt;&lt;code&gt;&lt;br /&gt;DSN=LocalServer;&lt;br /&gt;DATABASE=GammaMedidata;&lt;br /&gt;UID=sa;&lt;br /&gt;PWD=******;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;//  eGenix mxODBC Database Connection: connection string sample&lt;br /&gt;&lt;code&gt;&lt;br /&gt;DRIVER=SQL Server;&lt;br /&gt;SERVER=(local);&lt;br /&gt;UID=sa;&lt;br /&gt;PWD=****;&lt;br /&gt;DATABASE=wam&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;//  MySQL connection string sample&lt;br /&gt;&lt;code&gt;&lt;br /&gt;dbcobraf@www2.cobraf.com cobraf 'password'&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 16 Oct 2006 18:23:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2840</guid>
      <author>morlandi (Mario Orlandi)</author>
    </item>
    <item>
      <title>python and sqllite : simplest example</title>
      <link>http://snippets.dzone.com/posts/show/653</link>
      <description>&lt;code&gt;&lt;br /&gt;import sqlite&lt;br /&gt;&lt;br /&gt;con = sqlite.connect('mydatabase.db')&lt;br /&gt;cur = con.cursor()&lt;br /&gt;#~ cur.execute('CREATE TABLE foo (o_id INTEGER PRIMARY KEY, fruit VARCHAR(20), veges VARCHAR(30))')&lt;br /&gt;#~ con.commit()&lt;br /&gt;cur.execute('INSERT INTO foo (o_id, fruit, veges) VALUES(NULL, "apple", "broccoli")')&lt;br /&gt;con.commit()&lt;br /&gt;print cur.lastrowid&lt;br /&gt;&lt;br /&gt;cur.execute('SELECT * FROM foo')&lt;br /&gt;print cur.fetchall()&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 08 Sep 2005 01:41:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/653</guid>
      <author>manatlan (manatlan)</author>
    </item>
  </channel>
</rss>
