Skip to content

Instantly share code, notes, and snippets.

@maxivak
maxivak / gist:3924899
Created October 20, 2012 21:36
Django: raw SQL - UPDATE
from django.db import connection, transaction
cursor = connection.cursor()
cursor.execute("UPDATE yourtablename SET field1=value1, .. WHERE .. ")
transaction.set_dirty()
transaction.commit()
@maxivak
maxivak / gist:3924877
Created October 20, 2012 21:29
Django: raw SQL - SELECT
# file libs/dbutil.py
from itertools import *
from django.db import connection
def query_to_dicts(query_string, *query_args):
cursor = connection.cursor()
cursor.execute(query_string, query_args)
col_names = [desc[0] for desc in cursor.description]
while True:
@maxivak
maxivak / gist:3924988
Created October 20, 2012 22:02
Dynamic connection to MySQL database in Ruby on Rails (mysql2)
require 'mysql2'
db = Mysql2::Client.new(:host => 'localhost', :username => 'root', :password=> 'password', :database => 'db_name' )
res = db.query("select * from users")
res.each do |row|
col1 = row['id']
col2 = row['email']
..
@maxivak
maxivak / readme.md
Last active October 18, 2015 18:29
Install Centos 7 on Virtual machine using VirtualBox and Vagrant
@maxivak
maxivak / 00_android_myutils
Last active October 29, 2015 08:37
Android. Utils: Get JSON data by URL, download bitmap
package com.maxivak.utils;
import java.io.*;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
@maxivak
maxivak / rails-ids-list
Created January 6, 2013 05:44
Get a list of IDs from database
# 1. connection.select_values or connection.select_all
ids = ActiveRecord::Base.connection.select_values("select id from users WHERE ...")
# array of integers
# ["1", "2", "5", "6", "7", "8", "3", "10", "11", "9"]
# return hashes without model
sql = "sql query"
rows = User.connection.select_all(sql.squish!)
@maxivak
maxivak / rails-db-ids-list
Created January 6, 2013 05:36
Get a list of IDs from database
# 1. connection.select_values or connection.select_all
ids = ActiveRecord::Base.connection.select_values("select id from users WHERE ...")
# array of integers
# ["1", "2", "5", "6", "7", "8", "3", "10", "11", "9"]
# return hashes without model
sql = "sql query"
rows = User.connection.select_all(sql.squish!)
@maxivak
maxivak / 000-readme.md
Last active December 15, 2015 10:49
Rails custom error pages

Rails custom error pages

@maxivak
maxivak / passenger_memory_stats
Last active December 21, 2015 20:58
Munin Passenger plugin
#!/usr/bin/env ruby
# put in /etc/munin/plugins and restart munin-node
# by Dan Manges, http://www.dcmanges.com/blog/rails-application-visualization-with-munin
# NOTE: you might need to add munin to allow passwordless sudo for passenger-memory-stats
def output_config
puts <<-END
graph_category App
graph_title Passenger memory stats
graph_vlabel count
@maxivak
maxivak / gc_disable_request
Last active December 22, 2015 18:49
Rails. Disable GC during request
class ApplicationController < ActionController::Base
around_filter :gc_disable
private
def gc_disable
GC.disable
begin
yield
ensure