Skip to content

Instantly share code, notes, and snippets.

@Anas-jaf
Created July 22, 2024 14:16
Show Gist options
  • Save Anas-jaf/59e560632cdf2954bd42705516969cac to your computer and use it in GitHub Desktop.
Save Anas-jaf/59e560632cdf2954bd42705516969cac to your computer and use it in GitHub Desktop.
import pandas as pd
import mysql.connector
# Replace 'your_file.xlsx' with the path to your Excel file
file_path = '07_محافظة_الكرك.xlsx'
df = pd.read_excel(file_path, skiprows=9)
data_dict = df.to_dict(orient='records')
db = mysql.connector.connect(
port='3306',
host='127.0.0.1',
user='root',
password='root',
database='voters_db',
)
mycursor = db.cursor()
mycursor.execute('CREATE SCHEMA `voters_db` ;')
mycursor.execute('DROP TABLE `voters_db`.`voters`;')
mycursor.execute('''CREATE TABLE `voters_db`.`voters` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL,
`gender` VARCHAR(255) NULL,
`place_od_residence_in_id` VARCHAR(255) NULL,
`actual_place_of_residence` VARCHAR(255) NULL,
`election_area_id` VARCHAR(255) NULL,
PRIMARY KEY (`id`));''')
for voter_data_dict in data_dict:
mycursor.execute('INSERT INTO voters (name , gender , actual_place_of_residence) VALUES (%s,%s,%s)' ,(
voter_data_dict['اسم الناخب'],
voter_data_dict['الجنس'],
voter_data_dict['مكان الاقامة']
))
db.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment