Create sample dataframe
import pandas as pd
from io import StringIO
SAMPLEDATA = StringIO('''
tweet_id,sentiment,author,content
1956967341,"empty","xoshayzers","@tiffanylue i know i was listenin to bad habit earlier and i started freakin at his part =["
1956967666,"sadness","wannamama","Layin n bed with a headache ughhhh...waitin on your call..."
1956967696,"sadness","coolfunky","Funeral ceremony...gloomy friday..."
1956967789,"enthusiasm","czareaquino","wants to hang out with friends SOON!"
''')
df = pd.read_csv(SAMPLEDATA, sep=",")
df
POSTGRESQL
#install db adapter for postgresql
!pip install psycopg2
# import db adapter
import psycopg2
# connect
psconn = psycopg2.connect(host="xxxxx.db.elephantsql.com", port = 5432, database="rudmtwwq", user="rudmtwwq", password="aWXozigD3Jk2wLy7ayngTeHFp5i60j3d")
# create cursor
cursor = psconn.cursor()
# Create Table
cursor.execute('''
CREATE TABLE "tb_emotion" (
"index" INTEGER,
"tweet_id" INTEGER,
"sentiment" TEXT,
"author" TEXT,
"content" TEXT
)
''')
psconn.commit()
# close cursor
cursor.close()
# connect
psconn = psycopg2.connect(host="xxxxx.db.elephantsql.com", port = 5432, database="rudmtwwq", user="rudmtwwq", password="aWXozigD3Jk2wLy7ayngTeHFp5i60j3d")
# create cursor
cursor = psconn.cursor()
# insert dataframe into db server
for index, row in df.iterrows():
cursor.execute('''
INSERT INTO tb_emotion (index,tweet_id,sentiment,author,content) values(%s,%s,%s,%s,%s);
''',
(index, row.tweet_id, row.sentiment, row.author, row.content)
)
psconn.commit()
cursor.close()
# connect
psconn = psycopg2.connect(host="xxxxx.db.elephantsql.com", port = 5432, database="rudmtwwq", user="rudmtwwq", password="aWXozigD3Jk2wLy7ayngTeHFp5i60j3d")
# select data into dataframe
df = pd.read_sql_query('''
SELECT * FROM tb_emotion
'''
,psconn
)
psconn.commit()
df
see colab example
MYSQL/MARIADB
# install db connector
!pip install mysql-connector-python
# import connector
import mysql.connector as mysql
# connect
myconn = mysql.connect(host='82.180.175.153',database='u654086639_xxx', user='u654086639_xxx', password='xxxxxxxxxx')
# create cursor
cursor = myconn.cursor()
# Create Table
cursor.execute('''
CREATE TABLE `tb_emotion` (
`index` INT,
`tweet_id` INT,
`sentiment` TEXT,
`author` TEXT,
`content` TEXT
)
''')
myconn.commit()
# close cursor
cursor.close()
# connect
myconn = mysql.connect(host='82.180.175.153',database='u654086639_xxx', user='u654086639_xxx', password='xxxxxxxxxx')
# create cursor
cursor = myconn.cursor()
# insert dataframe into db server:
for index, row in df.iterrows():
cursor.execute('''
INSERT INTO `tb_emotion` (`index`,`tweet_id`,`sentiment`,`author`,`content`) values(%s,%s,%s,%s,%s);
''',
(index, row.tweet_id, row.sentiment, row.author, row.content)
)
myconn.commit()
cursor.close()
# connect
myconn = mysql.connect(host='82.180.175.153',database='u654086639_xxx', user='u654086639_xxx', password='xxxxxxxxxx')
# select data into dataframe
df = pd.read_sql_query('''
SELECT * FROM `tb_emotion`
'''
,myconn
)
myconn.commit()
df
see colab example
MS SQL SERVER
# install msodbcsql
%%sh
curl https:
curl https:
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get -q -y install msodbcsql17
# install pyodbc
!pip install pyodbc
# connect
msconn = pyodbc.connect(driver="ODBC Driver 17 for SQL Server",server="smacodbs.mssql.somee.com",database="smacodbs",UID="smarcompu_SQLLogin_1",PWD="ci6uu6zpx6",)
# create cursor
cursor = msconn.cursor()
# Create Table
cursor.execute('''
CREATE TABLE "tb_emotion" (
"index" INTEGER,
"tweet_id" INTEGER,
"sentiment" TEXT,
"author" TEXT,
"content" TEXT
)
''')
msconn.commit()
# close cursor
cursor.close()
# connect
msconn = pyodbc.connect(driver="ODBC Driver 17 for SQL Server",server="smacodbs.mssql.somee.com",database="smacodbs",UID="smarcompu_SQLLogin_1",PWD="ci6uu6zpx6",)
# create cursor
cursor = msconn.cursor()
# insert dataframe into db server
for index, row in df.iterrows():
# in MS SQL index is a reserved word, therefore custom field name must be enclosed in [...]
cursor.execute('''
INSERT INTO tb_emotion ([index],tweet_id,sentiment,author,content) values(?,?,?,?,?)
''',
index, row.tweet_id, row.sentiment, row.author, row.content
)
msconn.commit()
cursor.close()
# connect
msconn = pyodbc.connect(driver="ODBC Driver 17 for SQL Server",server="smacodbs.mssql.somee.com",database="smacodbs",UID="smarcompu_SQLLogin_1",PWD="ci6uu6zpx6",)
# select data into dataframe
df = pd.read_sql_query('''
SELECT * FROM tb_emotion
'''
,msconn
)
msconn.commit()
df
see colab example