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])
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)
In [40]:
cursor.execute('SELECT * FROM love')
records = [x for x in cursor]
print(len(records))
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 [ ]:
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))
In [ ]: