Store Hangman User Records in a MYSQL Database from user-record text file

Storing Data from Hangman in a MYSQL database is usually done by converting the text file(where the user records are stored into csv using pandas module and then using the csv file to write data into MYSQL server .

To learn how to create Hangman and save user records in a text file ! Click Here

Modules Used :

  • csv : pre-installed
  • pandas : pip install pandas
  • mysql-connector : pip install mysql-connector-python

Additional Information about Modules :

The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the format preferred by Excel,” or “read data from this file which was generated by Excel,” without knowing the precise details of the CSV format used by Excel.

Pandas is an open source library in Python. It provides ready to use high-performance data structures and data analysis tools. Pandas module runs on top of NumPy and it is popularly used for data science and data analytics.

MySQL Connector/Python enables Python programs to access MySQL databases, using an API that is compliant with the Python Database API Specification v2.0 (PEP 249). It is written in pure Python and does not have any dependencies except for the Python Standard Library.

Source Code :

Creating a csv file using the data of record.txt to write data to MYSQL database :

Data.py :

import csv
import pandas as pd

f=pd.read_csv('record.txt')
f.columns = ['Name','Status']
f.to_csv('RECORDCSV.csv')

import mysql.connector

db = mysql.connector.connect(host="localhost", user="root", passwd="root",db="#")
cursor = db.cursor()

f=open('RECORDCSV.csv','r')
reader=csv.reader(f)
for row in reader:
    cursor.execute("INSERT INTO RECORD(SNO,USER,STATUS) VALUE(%s,%s,%s)",row)

db.commit()
cursor.close()
print("Written successfully ! ")

Note : Running this more than once would cause errors as the data is already written once after executing this code and would try to override in MYSQL database

Creating a csv file using the data of record.txt to write data to MYSQL database :

ViewMYSQL.py :

import mysql.connector
try:
connection = mysql.connector.connect(host='localhost',
database='#',
user='root',
password='root')
sql_select_Query = "select distinct * from Record"
cursor = connection.cursor()
cursor.execute(sql_select_Query)

records = cursor.fetchall()
print("Total number of rows in table: ", cursor.rowcount)

print("\nPrinting each row")
for row in records:
    print("SNO = ", row[0], )
    print("USER = ", row[1])
    print("STATUS  = ", row[2])
    print()
except mysql.connector.Error as e:
print("Error reading data from MySQL table", e)
finally:
if connection.is_connected():
connection.close()
cursor.close()
print("MySQL connection is closed")

One thought on “Store Hangman User Records in a MYSQL Database from user-record text file

Leave a Reply

Your email address will not be published. Required fields are marked *