<commit_before>---
layout: posts
title:  "Mysql with Python"
date:   2016-11-25 10:03:40 +0530
categories: jekyll update
---
Reauirements are: PyMySql(for connection and interaction with database), pandas(importing data to data frame for analysis purpose)

Install PyMySql from [PyMySql][link1]


{% highlight python linenos%}

import pymysql as mysql
import pandas as pd

host = 'hostname' 	
user = 'username'
passwd = 'password'
db = 'dbname'

# create database connection 
conn = mysql.connect(host=host, user=user, passwd=passwd, db=db)

# cursor is required for query execution and data fetching
cur = conn.cursor()

# make a query, execute it and fetch the results
query = "SELECT * FROM users WHERE Id=1"

cur.execute(query)

result = cur.fetchall()

# write the result in a pandas dataframe
df = pd.read_sql_query(query, con=mydb)
df.to_csv('result.csv')

{% endhighlight %}

[link1]: https://github.com/PyMySQL/PyMySQL/<commit_msg>typos<commit_after>---
layout: posts
title:  "Mysql with Python"
date:   2016-11-25 10:03:40 +0530
categories: jekyll update
---
Reauirements are: PyMySql(for connection and interaction with database), pandas(importing data to data frame for analysis purpose)

Install PyMySql from [PyMySql][link1]


{% highlight python linenos%}
import pymysql as mysql
import pandas as pd

host = 'hostname' 	
user = 'username'
passwd = 'password'
db = 'dbname'

# create database connection 
conn = mysql.connect(host=host, user=user, passwd=passwd, db=db)

# cursor is required for query execution and data fetching
cur = conn.cursor()

# make a query, execute it and fetch the results
query = "SELECT * FROM users WHERE Id=1"

cur.execute(query)

result = cur.fetchall()

# write the result in a pandas dataframe
df = pd.read_sql_query(query, con=mydb)
df.to_csv('result.csv')
{% endhighlight %}

[link1]: https://github.com/PyMySQL/PyMySQL/
{'fraction_non_alphanumeric': 0.11122994652406418, 'fraction_numerical': 0.022459893048128343, 'mean_word_length': 4.8286604361370715, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2138700', 'n_tokens_mistral': 656, 'n_tokens_neox': 615, 'n_words': 207}