<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: database code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 00:44:22 GMT</pubDate>
    <description>DZone Snippets: database code</description>
    <item>
      <title>PHP Dynamic Checkbox Table Creator (data retrieved from MySQL DB)</title>
      <link>http://snippets.dzone.com/posts/show/5450</link>
      <description>Hi All.&lt;br /&gt;This is a function for Dynamically Create a Checbox Table retrieving information for from a MySQL Database.&lt;br /&gt;As it is quite commented, it's also good for learning how this things work :)&lt;br /&gt;Hope you find it useful.&lt;br /&gt;Feedback is welcome.&lt;br /&gt;Cheers&lt;br /&gt;Dan&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function dynamic_checkbox_table ($sql_str, $col_label, $col_name, $val_checked="S", $cant_cols_tbl=3){&lt;br /&gt;/*&lt;br /&gt;	by Daniel Neumann&lt;br /&gt;	this script creates dynamically permite a table containing checkboxes &lt;br /&gt;	getting the data for the checkboxes from a MySQL DB&lt;br /&gt;	$sql_str, SQL select string to retrieve data from DB (see example in last comment line)&lt;br /&gt;	$col_label, DB column that has values for the checkbox label &lt;br /&gt;	$col_name, DB column that has values for the checkbox name&lt;br /&gt;	$val_checked="S", value when checked (value="" attribute) it uses the same value for all of them. If you whish to use a dynamic value from a DB, you should comment the line (it&#180;s explained next to the code in the middle of the function) and de-comment the other line (check the code,. you'll understand what I mean). Also, you should use this parameter to specify the column name for the values&lt;br /&gt;	$cant_cols_tbl=3, quantity of columns for the table, it defaults to 3&lt;br /&gt;	usage example: dynamic_checkbox_table("SELECT * FROM keywords", "Keyword", "ID_Keywrd");&lt;br /&gt;*/&lt;br /&gt;	&lt;br /&gt;	//connect DB and run query&lt;br /&gt;	$db="MyDB";&lt;br /&gt;	$db_user="MyUser";&lt;br /&gt;	$pass="MyPass";&lt;br /&gt;	$host="localhost";&lt;br /&gt;	@mysql_connect($host,$db_user,$pass);&lt;br /&gt;	@mysql_select_db($db) or die ("cannot connect to DB");&lt;br /&gt;	$q_resultado = mysql_query($sql_str);&lt;br /&gt;	mysql_close();&lt;br /&gt;	if (mysql_num_rows($q_resultado)==0) exit("no rows returned");&lt;br /&gt;	&lt;br /&gt;	$next_row = mysql_fetch_array($q_resultado); //fetch first row&lt;br /&gt;	&lt;br /&gt;	$output = "&lt;table  border=\"1\"&gt;\n"; //open table tag&lt;br /&gt;	do {&lt;br /&gt;		$output .= "&lt;tr&gt;\n"; //open row tag&lt;br /&gt;		for ($i=1 ; $i &lt;= $cant_cols_tbl ; $i++ ){ //loops as many times as $cant_cols_tbl&lt;br /&gt;			$row=$next_row; //assign $row, next row will be checking next one, that avoids starting a new row when it's gonna be empty&lt;br /&gt;			$output .= "&lt;td&gt;"; //open TD tag&lt;br /&gt;			$output .= (!$row) ? "" : '&lt;input type="checkbox" name="'.$row[$col_name].'" value="'.$val_checked.'" /&gt;'.$row[$col_label]; //create checkbox and data from $row (**** you should comment this line if you whish to use dynamic $val_checked****)&lt;br /&gt;//			echo (!$row) ? "" : '&lt;input type="checkbox" name="'.$row[$col_name].'" value="'.$row[$val_checked].'" /&gt;'.$row[$col_label]; //create checkbox and data from $row (**** you should de-comment this line if you whish to use dynamic $val_checked****)&lt;br /&gt;			$next_row = mysql_fetch_array($q_resultado); //retrieve next row&lt;br /&gt;			$output .= "&lt;/td&gt;\n"; //close TD&lt;br /&gt;		} //close for loop&lt;br /&gt;		$output .= "&lt;/tr&gt;\n"; //close row&lt;br /&gt;	} while ($next_row); //close do-while (and checks if there's another row)&lt;br /&gt;	$output .= "&lt;/table&gt;\n"; //close table&lt;br /&gt;	return $output; &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 May 2008 12:09:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5450</guid>
      <author>dneuma (Daniel Neumann)</author>
    </item>
    <item>
      <title>Rails Array#add_condition Method</title>
      <link>http://snippets.dzone.com/posts/show/5147</link>
      <description>Lets you add a condition to a set of ActiveRecord conditions easily like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  conditions = ['active = ? and type = ?', true, 2]&lt;br /&gt;  conditions.add_condition ['person_id = ?', 345]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;  def add_condition(condition, conjunction='and')&lt;br /&gt;    if condition.is_a? Array&lt;br /&gt;      if self.empty?&lt;br /&gt;        (self &lt;&lt; condition).flatten!&lt;br /&gt;      else&lt;br /&gt;        self[0] += " #{conjunction} " + condition.shift&lt;br /&gt;        (self &lt;&lt; condition).flatten!&lt;br /&gt;      end&lt;br /&gt;    elsif condition.is_a? String&lt;br /&gt;      self[0] += " #{conjunction} " + condition&lt;br /&gt;    else&lt;br /&gt;      raise "don't know how to handle this condition type"&lt;br /&gt;    end&lt;br /&gt;    self&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 18 Feb 2008 02:54:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5147</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
    <item>
      <title>How to produce daily database table dumps in CSV format</title>
      <link>http://snippets.dzone.com/posts/show/4790</link>
      <description>The following code demonstrates how to produce CSV files with dynamic&lt;br /&gt;file name pattern based on a current day. The produced files have the following naming format:&lt;br /&gt;TABLE_NAME_MM_DD_YYYY.csv&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!DOCTYPE etl SYSTEM "http://scriptella.javaforge.com/dtd/etl.dtd"&gt;&lt;br /&gt;&lt;etl&gt;&lt;br /&gt;    &lt;properties&gt; &lt;!-- Configure table name --&gt;&lt;br /&gt;        table_name=test&lt;br /&gt;    &lt;/properties&gt;&lt;br /&gt;    &lt;connection id="in" driver="auto" url="jdbc:oracle:thin:@localhost:1521:ORCL" &lt;br /&gt;      classpath="ojdbc14.jar" user="scott" password="tiger"/&gt;&lt;br /&gt;    &lt;connection id="out" driver="csv" url="${table_name}_${etl.date.now('MM_dd_yyyy')}.csv" /&gt;&lt;br /&gt;    &lt;query connection-id="in"&gt; &lt;!-- Query table rows --&gt;&lt;br /&gt;        SELECT * FROM ${table_name}&lt;br /&gt;        &lt;script connection-id="out"&gt; &lt;!-- Export each row into a CSV --&gt;&lt;br /&gt;            $ID, $Name, $Surname &lt;!-- Use column names from selected table --&gt;&lt;br /&gt;        &lt;/script&gt;&lt;br /&gt;    &lt;/query&gt;&lt;br /&gt;&lt;/etl&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Use &lt;a href="http://scriptella.javaforge.com"&gt;Scriptella ETL&lt;/a&gt; to run the example. &lt;br /&gt;&lt;br /&gt;See &lt;a href="http://snippets.dzone.com/posts/show/4862"&gt;How to execute an ETL file&lt;/a&gt; from command line, Ant or directly from Java .</description>
      <pubDate>Fri, 16 Nov 2007 21:08:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4790</guid>
      <author>ejboy (Fyodor Kupolov)</author>
    </item>
    <item>
      <title>Project Schema</title>
      <link>http://snippets.dzone.com/posts/show/4751</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;CREATE TABLE passenger (id serial NOT NULL, nickname character varying(255) NOT NULL, description text);&lt;br /&gt;&lt;br /&gt;CREATE TABLE location (id serial NOT NULL, name character varying(255), latitude double precision, longitude double precision);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 13 Nov 2007 21:41:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4751</guid>
      <author>tmcw ()</author>
    </item>
    <item>
      <title>Project Schema</title>
      <link>http://snippets.dzone.com/posts/show/4750</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;CREATE TABLE passenger (id serial NOT NULL, nickname character varying(255) NOT NULL, description text);&lt;br /&gt;&lt;br /&gt;CREATE TABLE location (id serial NOT NULL, name character varying(255), latitude double precision, longitude double precision);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 13 Nov 2007 21:41:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4750</guid>
      <author>tmcw ()</author>
    </item>
    <item>
      <title>Project Schema</title>
      <link>http://snippets.dzone.com/posts/show/4749</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;CREATE TABLE passenger (id serial NOT NULL, nickname character varying(255) NOT NULL, description text);&lt;br /&gt;&lt;br /&gt;CREATE TABLE location (id serial NOT NULL, name character varying(255), latitude double precision, longitude double precision);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 13 Nov 2007 21:41:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4749</guid>
      <author>tmcw ()</author>
    </item>
    <item>
      <title>disable SQL / MySQL in rails logging in development mode</title>
      <link>http://snippets.dzone.com/posts/show/4727</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ActiveRecord::Base.logger = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}_database.log")&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;goes at end of config/env</description>
      <pubDate>Fri, 02 Nov 2007 00:09:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4727</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>C#: Execute A Query &amp; Return A Reader</title>
      <link>http://snippets.dzone.com/posts/show/4332</link>
      <description>&lt;code&gt;&lt;br /&gt;public static SqlDataReader GetReader(string Query)&lt;br /&gt;{&lt;br /&gt;	string ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CONNECTION_STRING_NAME"].ConnectionString;&lt;br /&gt;	SqlConnection con = new SqlConnection(ConnectionString);&lt;br /&gt;	SqlCommand command = new SqlCommand();&lt;br /&gt;&lt;br /&gt;	command.Connection = con;&lt;br /&gt;	command.Connection.Open();&lt;br /&gt;	command.CommandText = Query;&lt;br /&gt;	return command.ExecuteReader();&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jul 2007 22:02:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4332</guid>
      <author>cornerblue (CornerBLUE, Inc.)</author>
    </item>
    <item>
      <title>dbms database class</title>
      <link>http://snippets.dzone.com/posts/show/4242</link>
      <description>I'm at my first attempt at creating something for the PyS60 and I needed a simple sql wrapper class...&lt;br /&gt;I'm an experienced programmer, but has never programmed in python for s60 and it's been a while since I've used Python... So I hope this will help somebody, not annoy ;-)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Filename: db.py save in your Python dir&lt;br /&gt;import e32db&lt;br /&gt;&lt;br /&gt;class db:&lt;br /&gt;    def __init__(self, dbpath):&lt;br /&gt;	self.db = e32db.Dbms()&lt;br /&gt;	self.dbv = e32db.Db_view()&lt;br /&gt;	self.reset_counters()&lt;br /&gt;	try:&lt;br /&gt;	    self.db.open(unicode(dbpath))&lt;br /&gt;	except:&lt;br /&gt;	    self.db.create(unicode(dbpath))&lt;br /&gt;	    self.db.open(unicode(dbpath))&lt;br /&gt;&lt;br /&gt;    def reset_counters(self):&lt;br /&gt;	self.affected_rows = 0&lt;br /&gt;	self.num_rows = 0&lt;br /&gt;	self.__internal_counter = 0&lt;br /&gt;&lt;br /&gt;    def query(self, sql):&lt;br /&gt;	self.reset_counters()&lt;br /&gt;	if sql.lower().startswith('select'):&lt;br /&gt;	    self.dbv.prepare(self.db, unicode(sql))&lt;br /&gt;	    self.dbv.first_line()&lt;br /&gt;	    self.num_rows = self.dbv.count_line()&lt;br /&gt;	else:&lt;br /&gt;	    self.affected_rows = self.db.execute(unicode(sql))&lt;br /&gt;&lt;br /&gt;    def next(self):&lt;br /&gt;	row = {'id': 0}&lt;br /&gt;	if self.num_rows &lt; 1:&lt;br /&gt;	    self.reset_counters()&lt;br /&gt;	    raise StopIteration&lt;br /&gt;	elif self.__internal_counter &lt; self.num_rows:&lt;br /&gt;	    self.dbv.get_line()&lt;br /&gt;	    for i in range(self.dbv.col_count()):&lt;br /&gt;		row[i] = self.dbv.col(i+1)&lt;br /&gt;	    self.dbv.next_line()&lt;br /&gt;	    self.__internal_counter += 1&lt;br /&gt;	    return row&lt;br /&gt;	else:&lt;br /&gt;	    self.reset_counters()&lt;br /&gt;	    raise StopIteration&lt;br /&gt;&lt;br /&gt;    def __iter__(self):&lt;br /&gt;	return self&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now to create and fill db, create a file in the same dir as the db.py with this content:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Change __exec_path to the path of your python script dir&lt;br /&gt;__exec_path = "E:\\Python\\"&lt;br /&gt;dbname = "test"&lt;br /&gt;&lt;br /&gt;import sys&lt;br /&gt;sys.path.append(__exec_path)&lt;br /&gt;from db import db&lt;br /&gt;&lt;br /&gt;# This will open E:\\Python\test.db - it will be created first, if not existing&lt;br /&gt;mydb = db(__exec_path+dbname+'.db')&lt;br /&gt;mydb.query("create table testing (id counter, name varchar)")&lt;br /&gt;mydb.query("insert into testing (name) values ('test 1')")&lt;br /&gt;mydb.query("insert into testing (name) values ('test 2')")&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now go ahead and have fun:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Again change __exec_path to the path of your python script dir&lt;br /&gt;__exec_path = "E:\\Python\\"&lt;br /&gt;dbname = "test"&lt;br /&gt;&lt;br /&gt;import sys&lt;br /&gt;sys.path.append(__exec_path)&lt;br /&gt;from db import db&lt;br /&gt;&lt;br /&gt;# Opens E:\\Python\test.db&lt;br /&gt;mydb = db(__exec_path+dbname+'.db')&lt;br /&gt;mydb.query("select * from testing")&lt;br /&gt;for row in mydb:&lt;br /&gt;    print "-&gt; ",row[0]," ",row[1]," &lt;-"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Have fun!&lt;br /&gt;Dan</description>
      <pubDate>Tue, 03 Jul 2007 21:46:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4242</guid>
      <author>nerdcoder (Dan Larsen)</author>
    </item>
  </channel>
</rss>
