Using MySQL connector


In [10]:
import mysql.connector

myu = open(r"Z:\abs_path\mysql_emanresu.txt", "r").read()
mypw = open(r"Z:\abs_path\mysql_drowssap.txt", "r").read()

cnx = mysql.connector.connect(user=myu, password=mypw,
                              host='127.0.0.1', buffered=True)
cursor = cnx.cursor()

In [27]:
# show database
cursor.execute('SHOW DATABASES')
print([x[0] for x in cursor])


['information_schema', 'mysql', 'performance_schema', 'shakespeare_complete_works', 'twitter2015', 'will_play_text']

In [26]:
# remove database
cursor.execute("DROP DATABASE test1")

In [29]:
# use database
cursor.execute("USE twitter2015")

In [31]:
# show tables
cursor.execute('SHOW TABLES')
tables = [x[0] for x in cursor]
print(tables)


['addict', 'addiction', 'atheist', 'atheists', 'athiest', 'athiests', 'booze', 'canada', 'drugs', 'drunk', 'faggot', 'feminazi', 'feminazis', 'feminism', 'feminist', 'feminists', 'fox news', 'fox_news', 'hate', 'hebdo', 'islam', 'love', 'marijuana', 'mulsim', 'mulsims', 'muslim', 'muslims', 'narcotics', 'nigger', 'obama', 'pope', 'putin', 'racism', 'racist', 'rape', 'rehab', 'russia', 'russian', 'selma', 'stoned', 'syria', 'terrorism', 'terrorist', 'terrorists', 'test', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7']

In [40]:
cursor.execute('SELECT * FROM love')
records = [x for x in cursor]
print(len(records))


0

In [ ]:
# create database
"CREATE DATABASE name DEFAULT CHARACTER SET 'utf8'"

# create table
cmd = ("CREATE TABLE `example` ("
    " `title` varchar(255) NOT NULL," 
    " `text` MEDIUMTEXT,"
    " `bool` tinyint(1) NOT NULL," 
    " `intro_or_body` enum('Intro', 'Body')," 
    " `line_num` smallint(4) NOT NULL," 
    " PRIMARY KEY (`title`, `bool`, `line_num`),"
    " FOREIGN KEY (`title`) REFERENCES plays(`title`)"
    ") ENGINE=InnoDB")
cursor.execute(cmd)

In [ ]:

SQLAlchemy


In [33]:
dbname = 'twitter2015'

from sqlalchemy import create_engine

myu = open(r"Z:\abs_path\mysql_emanresu.txt", "r").read()
mypw = open(r"Z:\abs_path\mysql_drowssap.txt", "r").read()

engine = create_engine('mysql+mysqlconnector://'+myu+':'+mypw+\
                       '@localhost:3306/{}'.format(dbname))

In [41]:
import pandas as pd

for table in tables:
    assert engine.has_table(table)
    df = pd.read_sql(table, engine)
    df.to_csv(r'Z:\\'+table+'.csv', index=False)
    print(table, len(df))


addict 0
addiction 0
atheist 0
atheists 0
athiest 0
athiests 0
booze 0
canada 0
drugs 0
drunk 0
faggot 0
feminazi 0
feminazis 0
feminism 0
feminist 0
feminists 0
fox news 0
fox_news 0
hate 0
hebdo 0
islam 0
love 0
marijuana 0
mulsim 0
mulsims 0
muslim 0
muslims 0
narcotics 0
nigger 0
obama 0
pope 0
putin 0
racism 0
racist 0
rape 0
rehab 0
russia 0
russian 0
selma 0
stoned 0
syria 0
terrorism 0
terrorist 0
terrorists 0
test 0
test2 0
test3 0
test4 0
test5 0
test6 0
test7 0

In [ ]: