How to Connect MySQL database with Python

Hi…to all today i’m going to tell how to connect python with Mysql

we need a package named MySQLdb

install that package inorder to connect mysql and python.

$ sudo apt-get install python-mysqldb

Here is the code to connect to mysql

# !usr/bin/python
import MySQLdb
#creating connection using Mysqldb and mqsql dbname,username and password
con = MySQLdb.connect(host = “localhost”, user = “root”, passwd = “”, db = “balaji”)
#creating currsor to access to db
cursor=con.cursor()
#Executing query using cursor
cursor.execute(“SELECT VERSION()”)
result=cursor.fetchone()
#printing the result
print result
#closing the db connection .
con.close()

Save this file with .py extension

now run the file by $ python filename.py

Thats it..enjoy the coding with python…happy times!!!

Advertisement

Bootstrap Twitter in Rails

Hi…to all to day i learned Bootstrap from Twitter in rails application…

Before getting into session first ,let we  know about twitterbootstrap..It helps to build beautiful web applications..It provides variety of CSS and Javascripts for  making layouts,navigations.lot more etc..

Now, we ll create a simple application to store the products and price in a shop and then add TwitterBootstrap..

we ll call the app as store and create scaffold named Product model to do some work

$ rails new store

$ cd store

$ rails g scaffold product name:string price:decimal –skip-stylesheets

$ rake db:migrate

Note: we skipped stylesheets because we are going to use Bootstrap’s stylesheets,css,etc..

now you can see the page as like this..

now.we have to add Bootstrap to this application..

open Gem file

  1. # Gems used only for assets and not required  
  2. # in production environments by default.  
  3. group :assets do  
  4.   gem ‘sass-rails’,   ‘~> 3.2.3’  
  5.   gem ‘coffee-rails’, ‘~> 3.2.1’  
  6.   
  7.   # See https://github.com/sstephenson/execjs#readme for more supported runtimes  
  8.   # gem ‘therubyracer’  
  9.   
  10.   gem ‘uglifier’, ‘>= 1.0.3’  
  11.   gem ‘twitter-bootstrap-rails’  
  12. end

now bundle it and install Bootstrap Twitter

$ bundle install

$ rails g bootstrap:install

this file will be installed in assests/bootstrap_and_overwrites_css.less

now if yuo run the server u can feel difference ..Now our page looks like this…

 

now we can improve our layout by bootstrap..

app/views/layout/application.html

add <div class=”container”>  in body of the file.

now we are going to add sidebar to our page

/app/views/layouts/application.html.erb

<div>
  <div>
    <div><%= yield %></div>
    <div>
      <h2>About Us</h2>
      <p>This is a product based company.Here we are generating a very good products for pepole in a quality manner. Our products are most popular in the market.</p>	
    </div>
  </div>
</div>

now you can see the about us as sidebar in our app like this

Finally we are going to add the navigation bar such as somestore,contact us..etc

add these lines on top of the body of the file /app/views/layouts/application.html.erb

  • <div class=”navbar navbar-fixed-top”>  
  •   <div class=”navbar-inner”>  
  •     <div class=”container”>  
  •       <a class=”btn btn-navbar” data-toggle=”collapse” data-target=”.collapse”>  
  •         <span class=”icon-bar”></span>  
  •         <span class=”icon-bar”></span>  
  •         <span class=”icon-bar”></span>  
  •       </a>  
  •       <a class=”brand” href=”#”>Some Store</a>  
  •       <div class=”nav-collapse”>  
  •         <ul class=”nav”>  
  •           <li><%= link_to “Browse Products”, products_path %></li>  
  •           <li><%= link_to “Price List” %></li>  
  •           <li><%= link_to “Contact Us” %></li>  
  •           <li><%= link_to “Cart” %></li>  
  •         </ul>  
  •       </div>  
  •     </div>  
  •   </div>  
  • </div>  

now if you run the server you can see the navigations bar in our application…

Final Tweaks to The Header

Our application’s layout file is pretty much complete now but there are a couple of thing we need to add in the head section to ensure that it works everywhere.

/app/views/layouts/application.html.erb

<head>
  <title>Store</title>
  <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode/svn/trunk/html5.js" type="text/javascript"></script>
  <![endif]-->
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

the above code for HTML5 support and mobile behaviour .

Next we’ll use Twitter Bootstrap to improve the look of this page. Instead of walking through each change manually we’ll use one of the generators provided by the gem.

$ rails g bootstrap:themed products -f

now run server by $ rails s

after adding products and price to our store apllication ..Our application looks like this…

Thats it…Thanks to all….happy times!!!!!

Sending emails using gmail in Rails

Hi…to all today i learned how to send emails using gmail in rails

here are the steps

Create application using

$ rails new mailit

change the directory using      $ cd   /mailit

next create the scaffolder name User with name and email

$ rails g scaffold user name:string email:string

then run the database migrations

$ rake db:migrate

next we have to do configuration settings for gmail

create a file in  config/initializers named setup_mail.rb  

ActionMailer::Base.smtp_settings = {
:address => “smtp.gmail.com”,
:port => 587,
:domain => “mail.google.com”,
:user_name => “username@gmail.com”,
:password => “yourpassword”,
:authentication => “plain”,
:enable_starttls_auto => true
}

now we completed the configuration ,now generate the mailer using

$ rails g mailer user_mailer

this will create the user_mail.rb in  /apps/mailer

now add the following code in to that file

  1. class UserMailer < ActionMailer::Base  
  2.   default :from => “user@gmail.com”  
  3.   
  4.   def registration_confirmation(user)  
  5.     mail(:to => user.email, :subject => “Registered” , :body =>”Thanks for registering”)  
  6.   end  
  7. end

create textfile named regisetration_confirm.text/erb in apps/views/user_mailer

Thanks for registering!!!

Add the following line to the  /app/controllers/users_controller.rb

UserMailer.registration_confirmation(@user).deliver

def create
@user = User.new(params[:user])

respond_to do |format|
if @user.save
UserMailer.registration_confirmation(@user).deliver # added line 
format.html { redirect_to @user, :notice => ‘User was successfully created.’ }
format.json { render :json => @user, :status => :created, :location => @user }
else
format.html { render :action => “new” }
format.json { render :json => @user.errors, :status => :unprocessable_entity }
end
end
end

Thats it ..now rin the server by $ rails server 

then give name and emailid to which you have to send email..

then click create button ,now you will see like this

now go to your mail and check your inbox

Thanks to all…happy times!!!!