How to Insert Values into Table through MySQLdb Python

How to Insert Values into Table through MySQLdb Python

So here I was trying to store the values of a csv file 'employee_reviews.csv' into my employees table. I'm reading through the file, storing it as a string, then splitting the elements all the way down to a 2D list which is then looped through each row. I'm pretty sure there is a more simple way of doing this. When I run my code, I get an Error ->

So here I was trying to store the values of a csv file 'employee_reviews.csv' into my employees table. I'm reading through the file, storing it as a string, then splitting the elements all the way down to a 2D list which is then looped through each row. I'm pretty sure there is a more simple way of doing this. When I run my code, I get an Error ->

cur.execute("INSERT INTO (id, company, location, dates, job_title, summary, pros, cons, advice_to_management, overall_ratings, work_balance_stars, culture_value_stars, career_opportunities_stars, comp_benefit_stars, senior_management_stars, helpful_count, link) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(int(row[0]), row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], int(row[9]), row[10], row[11], row[12], row[13], row[14], int(row[15]), row[16] )) ValueError: invalid literal for int() with base 10: 'none'


import csv
import MySQLdb

mydb = MySQL.connect(host="host",user="user",passwd="passwd",db="Employee_Review") cur = mydb.cursor() #cur.execute("CREATE TABLE employees(id INT, company VARCHAR(20), location VARCHAR(40), dates DATE, job_title VARCHAR(20), summary VARCHAR(50), pros VARCHAR(1000), cons VARCHAR(1000), advice_to_management VARCHAR(50), overall_ratings INT, work_balance_stars VARCHAR(10), culture_value_stars VARCHAR(10), career_opportunities_stars VARCHAR(10), comp_benefit_stars VARCHAR(10), senior_management_stars VARCHAR(10), helpful_count INT, link VARCHAR(75) )")

f = open("employee_reviews.csv","r") fString = f.read()

fList = [] for line in fString.split('\n'): fList.append(line.split(','))

for row in fList: if row == fList[0]: continue #skip the first row for it contains the name of the columns

cur.execute("INSERT INTO (id, company, location, dates, job_title, summary, pros, cons, advice_to_management, overall_ratings, work_balance_stars, culture_value_stars, career_opportunities_stars, comp_benefit_stars, senior_management_stars, helpful_count, link) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",(int(row[0]), row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], int(row[9]), row[10], row[11], row[12], row[13], row[14], int(row[15]), row[16] )) mydb.commit()

mydb.close()

The csv file can be found here: https://inclass.kaggle.com/petersunga/google-amazon-facebook-employee-reviews/discussion

python mysql sql

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Best MySQL DigitalOcean Performance – ScaleGrid vs. DigitalOcean Managed Databases

Compare ScaleGrid MySQL vs. DigitalOcean Managed Databases - See which offers the best MySQL throughput, latency, and pricing on DigitalOcean across workloads.

Welcome Back the T-SQL Debugger with SQL Complete – SQL Debugger

Debug SQL stored procedures and develop your SQL database project with dbForge SQL Complete, a new add-in for Visual Studio and SSMS. When you develop large chunks of T-SQL code with the help of the SQL Server Management Studio tool, it is essential to test the “Live” behavior of your code by making sure that each small piece of code works fine and being able to allocate any error message that may cause a failure within that code.

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.