Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
8 total  XML / RSS feed 

Convert MySQL tables to InnoDB

// Slower than MyISAM, but row locking is clutch if you're doing a lot of writes amirite?

for t in $(mysql --batch --column-names=false -e "show tables" mydbname |grep -v "exclude_this");
do
mysql -e "alter table $t type=InnoDB" mydbname;
done


From:
http://ludo.qix.it/archive/2005/04/convert-a-bunch-of-tables-to-innodb.html

Delete duplicate rows from a table

// Whoops, didja forgot to add a unique index to that column? No problem

CREATE TABLE new_table AS
SELECT * FROM old_table WHERE 1 GROUP BY [COLUMN TO remove duplicates BY];
DROP TABLE old_table;
RENAME TABLE new_table TO old_table;

VB.net Modified Preorder Tree Traversal

In order to utilize this, I would recommend reading the article located at sitepoint.com (Search google for modified preorder tree traversal). It took me quite some time and some help from a coworker to port this from PHP to VB.net. :D

Replace the application("sql").query("*") with your SQL code, I was inclined to write my own SQL class to make querying DB's easier, but you can do whatever you want.
record = application("sql").query("select * from categories order by lft asc")
dim output as new arraylist()
dim right as new arraylist()
do while not record.eof
  if right.count > 0
    do while right(right.count - 1) < record("rgt").value
      right.removeAt(right.count - 1)
      if right.count = 0 then exit do
    loop
  end if
  response.write(record("name").value & " is " & right.count & " levels deep in the tree..." & controlchars.cr)
  right.add(record("rgt").value)
  record.movenext
loop

Scripting schema updates in MSSQL #1

This SQL statement will alter a table only if a column in the table to be altered does NOT contain a column named 'FavoriteColorId'.

From: http://haacked.com/

IF NOT EXISTS
(
    SELECT * FROM [information_schema].[columns]
    WHERE    table_name   = 'Customer'
    AND      table_schema = 'dbo'
    AND      column_name  = 'FavoriteColorId'
)
BEGIN
    ALTER TABLE [dbo].[Customer]
    ADD FavoriteColorId int

Web and database backup


mysqldump -u root -p —all-databases > /path/to/backups/YYYY-MM-DD-alldbs.sql
scp /path/to/backups/YYYY-MM-DD-alldbs.sql.gz [email protected]:/path/to/backups
tar czvf /path/to/backups/YYYY-MM-DD-production.tgz /path/to/production
scp /path/to/backups/YYYY-MM-DD-production.tgz [email protected]:/path/to/backups

              

            

Upload Image To Database

// Code To Upload an image to a database and then update its contents.

# Create an ew classified and attach an image with it.
def create
  image_types = ["image/jpeg", "image/pjpeg", "image/gif","image/png", "image/x-png"]
  @categories = Category.find(:all)
  @classified = Classified.new(params[:classified])
  @classified.user = session[:user]
  unless params[:classified][:picture].blank?
    if (image_types.include?params[:classified][:picture].content_type.chomp)
      @classified.picture = params[:classified][:picture].read
    else
      @classified.errors.add(:picture,  "Photo doesn't seem to be JPG, GIF, or PNG. please ensure it is a valid image file.")
      render :action => 'new'
      return
    end
  end

  if @classified.save
    redirect_to :action => 'list'
  else 
    render :action => 'new'
  end
end


# Update Method So user can change the image associated with their classified
def update
  image_types = ["image/jpeg", "image/pjpeg", "image/gif", "image/png", "image/x-png"]
  @classified = Classified.find(params[:id])
  @categories = Category.find(:all)
  
  if @classified.update_attributes(params[:classified])
    unless params[:classified][:picture].blank?
      if (image_types.include?params[:classified][:picture].content_type.chomp)
        @classified.picture = params[:classified][:picture].read
      else
        @classified.errors.add(:picture, " doesn't seem to be JPG, GIF, or PNG. please ensure it is a valid image file.")
        render :action => 'edit'
        return
      end
    end
    
    flash[:notice] = 'Classified was successfully updated.'
    redirect_to :action => 'show', :id => @classified
  else
    render :action => 'edit'
  end
end

Change password in Sybase

// description of your code here

#check last pwd change

1> use master
2> go
1> select name, pwdate from syslogins where name = 'wat'
2> go
 name                           pwdate                     
 ------------------------------ -------------------------- 
 wat                              Jul  3 2006 12:54PM 

(1 row affected)

#change pwd

sp_password   caller_passwd, new_passwd   [,   login_name  ] 
 - caller_passwd: old pwd
 - new_passwd
 - login_name: use only if you want to change other users pwd

Creating a table with migrations when using has_and_belongs_to_many

You can find the information at http://api.rubyonrails.org under the "create_table" method. The short answer is this though,

create_table :table_name, :id => false do |t|

end


:id is the part you're looking for, it defaults to true, which creates the :id as an autoincrementing primary key, if you use migrations and want to create the join table, you don't want an "id" column, so use :id => false to override the create_table method and tell it you don't want that primary key.
« Newer Snippets
Older Snippets »
8 total  XML / RSS feed