How to Chunk Large Files in Swift
How to Chunk Large Files in Swift (4.1) It takes a bit of work but this is the solution I found that finally worked, thanks to this Stack Overflow thread https://stackoverflow.com/questions/46464177/split-data-in-chunks-and-upload-to-server The example ...
Written by Sean Behan on 09/15/2018
How to Find Postgres Log File and Postgres Data Directory from PSQL
If you want to find the location of your log file in Postgres, you'll need to hop into a Psql session.. psql dbname Then it's as simple as running.. show data_directory ; Which will output the data directory, in my case.. /Users/sea...
Written by Sean Behan on 05/25/2018
How to Just Get SQL Statement Error with SQLAlchemy Python Database Wrapper
If you're working with SQLAlchemy, the best database driver for Python, and want to see only SQL syntax errors, you need to use the StatementError exception class. On it, is an attribute `orig`, that contains just the SQL syntax error... and not any data...
Written by Sean Behan on 05/10/2018
How to Log and Query SQL Queries Hitting Your Database with MySQL
Here is some code just in case you want to look at and query the queries hitting your MySQL database. Enter this from the mysql client console. mysql> SET GLOBAL log_output = 'TABLE' mysql> SET GLOBAL general_log = 'ON'; mysql> select event_ti...
Written by Sean Behan on 03/17/2018
Connect to Postgres on Heroku using DATABASE_URL Config Var with PHP and PDO
Unfortunately PHP's PDO constructor doesn't take a database connection url (in a format that Heroku makes available as a config var) as an argument. It has its own, rather odd syntax. However, it's easy enough to extract url parts with the `parse_url`...
Written by Sean Behan on 11/10/2017
Manual ManyToMany Through with Django's ORM
Here is a code snippet that demonstrates how to set up a __ManyToMany through__ relationship in Django. In Rails, the equivalent would be called a __has_many through__ association. If you set the __through__ argument on the ManyToManyField, Django wil...
Written by Sean Behan on 07/21/2017
Simple SQL for Counting New Signups
Here is a little snippet that will return new signups (or new records) for today select id, email, created_at::date date from signups where email not in (select distinct email from signups where created_at < current_date)
Written by Sean Behan on 07/13/2017
How to Make Cross Database Queries with Postgres and DBLink Extension
Here are a few snippets for cross database queries. It's important to note that you must be explicit in enumerating the columns and types you are querying. Otherwise, things will probably not work as expected. -- enable extension create extension db...
Written by Sean Behan on 03/18/2017
How to Import/Export a Database from One Heroku App to Another Heroku App
Heroku is awesome! Let's say we want to copy a production database to a staging database. We can use the `pg:backups:restore` command to accomplish this. Here is an example. For the source database we are using the `production-app-name` and for stagi...
Written by Sean Behan on 03/07/2017
How To Create a Dump File in Postgres Compatible with Heroku
When Heroku creates a dump file of your Postgres database it uses the `-Fc` option It is equivalent to running pg_dump -Fc -d name_of_db > name_of_db.dump This command will let you import your database with the `pg_restore` command pg_rest...
Written by Sean Behan on 03/06/2017
Destructuring Dictionaries in Python
Here is a quick and dirty way to destructure dictionaries in [Python] d = {'a':'Apple', 'b':'Banana','c':'Carrot'} a,b,c = [d[k] for k in ('a', 'b','c')] a == 'Apple' b == 'Banana' c == 'Carrot'
Written by Sean Behan on 03/04/2017
How to Resolve ERROR 1396 (HY000): Operation CREATE USER failed for Error in MySQL
If you run into this error when trying to create a user in mysql, chances are you already have this user account created. create user 'someuser'@'localhost' identified by 'somepassword'; ERROR 1396 (HY000): Operation CREATE USER failed for 'someuser...
Written by Sean Behan on 03/02/2017
Extract Domain Names From Links in Text with Postgres and a Single SQL Query
This query and pattern will return urls in text all within a single SQL query. select substring(column_name from '.*://([^/]*)') as domain_name from table_name; And here it is in a larger query, say for retrieving page view counts for referrers. ...
Written by Sean Behan on 11/23/2013
How To Fix ActiveRecord::ConnectionTimeoutError with Sinatra
If you get this error message ActiveRecord::ConnectionTimeoutError could not obtain a database connection within 5.000 seconds (waited 5.001 seconds) Try closing the connection after each request using "after" in Sintara. # app.rb after { A...
Written by Sean Behan on 11/09/2013
How To Remove Duplicates From a Table with Postgres
Let's create a table that will hold some dummy data. > create table fruits (id serial primary key, name varchar, color varchar); > insert into fruits (name, color) values ('apple', 'red'); > insert into fruits (name, color) values ('ap...
Written by Sean Behan on 10/08/2013
How to cast a string of comma separated numbers into an array of integers for Postgres
If you have an string of numbers like "1,2,3" and you want to turn it into an array of integers you need to cast it into an integer array type. "{1,2,3}"::int[] This is commonly used together when grabbing a set using the ANY clause. sele...
Written by Sean Behan on 09/26/2013
How To Export A MySQL Database to JSON, CSV and XML with Ruby and the ActiveRecord Gem
It's trivial to export data from a mysql database with Ruby and ActiveRecord. All you have to do is establish a connection and define a class that represents the table. require 'rubygems' require 'active_record' ActiveRecord::Base.establish_con...
Written by Sean Behan on 06/23/2012
How to Merge a YAML File Into a Single Hash in Ruby
require 'yaml' # Nested YAML structure from something-nested.yml # # nested: # key: value # key_two: value_two # Procedural Approach to building a single Hash from nested YAML data structure. @yaml = YAML.load_file("something-nested...
Written by Sean Behan on 06/17/2012
Backup and Rotate MySQL Databases Simple Bash Script
Make a directory ( it can anywhere ) called baks/mysql mkdir -p /baks/mysql Create a file (it can be anywhere) called /root/mysql_backups.sh and put this script in it #!/bin/bash # modify the following to suit your environment export DB...
Written by Sean Behan on 05/16/2017
Repost
This post is inspired by http://pupeno.com/blog/really-resetting-the-database/#comment-1179. But as my blog mostly serves as a reference for my future self, I'd like to reprint this little snippet of code here as well. namespace :db do desc "Crush a...
Written by Sean Behan on 06/17/2012
A List of Every High School in the United States in Pipe Delimitted Form (2005)
Here is a text file containing every high school in the United States(2005), including city, state, zip code and high school name. us_high_schools_pd
Written by Sean Behan on 06/17/2012
How to Import/Export Your Wordpress Blogroll... er, Your Links
It's not immediately apparent how to import/export the links in your Wordpress blogroll. One would expect that the import/export tool, used to backup/restore Wordpress posts and pages would handle this functionality as well. But the import/export tool has...
Written by Sean Behan on 06/17/2012
Dump MySQL Database without Drop Table Syntax
Output .sql file for MySQL but without the drop table syntax before table name use the --skip-add-drop-table flag mysqldump -u root -p database_name --skip-add-drop-table --skip-lock-tables > database_name.sql
Written by Sean Behan on 06/17/2012
The survey for people who make websites
I took the survey! Click here to take it →
Written by Sean Behan on 06/17/2012
Managing Timestamps in MySQL with a Trigger
MySQL doesn't support having two columns with time stamping on both initialization and/or on updating at the same time. It would be nice to be able to do *this* where the created_at column gets the current_timestamp on initialization and the updated_at ge...
Written by Sean Behan on 06/17/2012
Me in 3D
My friend Andy made this 3D rendering of me using LIDAR (Laser Detection and Ranging). In the first image you can see me holding up my hand along with a number of file cabinets. The cube in the center is a table used to calibrate the device. In this sh...
Written by Sean Behan on 06/17/2012
My First Data Visualization McCain V. Obama Election Results
Here is my first data visualization. I used the Processing programming language and an SVG map of the United States. ...
Written by Sean Behan on 06/17/2012
Manage Fixtures with Yaml DB Plugin for Rails
Get the plugin like so... script/plugin install git://github.com/adamwiggins/yaml_db.git This command will dump your data rake db:data:dump And load it back rake db:data:load Beautiful :) More info here http://blog.heroku.com/archives/2007/11/23/yam...
Written by Sean Behan on 06/17/2012
Install do_mysql Ruby Gem on Mac OS X
I ran into the same problem when installing mysql gem for Rails development. This fix worked for me http://seanbehan.com/programming/fixing-mysql-for-rails-2-2-development-on-mac-os-x/ The same thing works with the data objects gem. Just specify the path...
Written by Sean Behan on 06/17/2012
Installing Redis Server and Client on Mac OS X and Ubuntu
wget http://redis.googlecode.com/files/redis-0.900_2.tar.gz tar xzvf redis-0.900_2.tar.gz cd redis-0.900 make mv redis-server /usr/bin/ mv redis-cli /usr/bin/
Written by Sean Behan on 06/17/2012
Problem slash Bug in Rails with attr_accessor and Datetime Select Fields
Looks like there is a problem with using the attr_accessor method with datetime form fields http://dev.rubyonrails.org/ticket/8983 In Rails 2.3.2, for me at least, the problem seems to be popping up again. While trying to process a date for credit card v...
Written by Sean Behan on 06/17/2012