0

I am trying to push my XLSM file to MySQL phpadmin using pandas. Here is my full code of what i have attempted so far.

 from sqlalchemy import create_engine
 import pandas as pd
 import os
 import MySQLdb

 engine = create_engine('mysql+mysqldb://root:@localhost/myDB? 
 charset=utf8mb4&binary_prefix=true', echo=False)

 mydir = (os.getcwd()).replace('\\', '/') + '/'
 data = pd.read_excel(r'' + mydir + 'Governance_Tracker - Copy - Copy.xlsm'
 ,header = 1).drop(['#'], axis=1)
 data.replace('\n','', regex=True)
 df1 = data.where((pd.notnull(data)), None)
 print(df1)

 df1.to_sql('govtracker', con=engine,if_exists='append',index=False)
 rows = engine.execute("SELECT * FROM govtracker").fetchall()
 print(rows)

But when I execute it throws error

MySQLdb._exceptions.OperationalError: (1054, "Unknown column 'index' in 
'field list'")

Here is the full stack trace for your reference:

Traceback (most recent call last):
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\sqlalchemy\engine\base.py", line 1224, in _execute_context
cursor, statement, parameters, context
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\sqlalchemy\dialects\mysql\mysqldb.py", line 132, in 
do_executemany
rowcount = cursor.executemany(statement, parameters)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\MySQLdb\cursors.py", line 234, in executemany
self._get_db().encoding)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\MySQLdb\cursors.py", line 256, in _do_execute_many
rows += self.execute(sql + postfix)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\MySQLdb\cursors.py", line 206, in execute
res = self._query(query)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\MySQLdb\cursors.py", line 312, in _query
db.query(q)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\MySQLdb\connections.py", line 224, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (1054, "Unknown column 'index' in 
'field list'")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm 
2019.1.2\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars)  # execute the 
script
File "C:\Program Files\JetBrains\PyCharm 
2019.1.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in 
execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/DELL/PycharmProjects/GUIRef/PyGUI.py", line 15, in <module>
df1.to_sql('govtracker', con=engine,if_exists='append')
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\pandas\core\generic.py", line 2531, in to_sql
dtype=dtype, method=method)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\pandas\io\sql.py", line 460, in to_sql
chunksize=chunksize, dtype=dtype, method=method)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\pandas\io\sql.py", line 1174, in to_sql
table.insert(chunksize, method=method)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\pandas\io\sql.py", line 686, in insert
exec_insert(conn, keys, chunk_iter)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\pandas\io\sql.py", line 599, in _execute_insert
conn.execute(self.table.insert(), data)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\sqlalchemy\engine\base.py", line 988, in execute
return meth(self, multiparams, params)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\sqlalchemy\sql\elements.py", line 287, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\sqlalchemy\engine\base.py", line 1107, in _execute_clauseelement
distilled_params,
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\sqlalchemy\engine\base.py", line 1248, in _execute_context
e, statement, parameters, cursor, context
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\sqlalchemy\engine\base.py", line 1466, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\sqlalchemy\util\compat.py", line 383, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\sqlalchemy\util\compat.py", line 128, in reraise
raise value.with_traceback(tb)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\sqlalchemy\engine\base.py", line 1224, in _execute_context
cursor, statement, parameters, context
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\sqlalchemy\dialects\mysql\mysqldb.py", line 132, in 
do_executemany
rowcount = cursor.executemany(statement, parameters)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\MySQLdb\cursors.py", line 234, in executemany
self._get_db().encoding)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\MySQLdb\cursors.py", line 256, in _do_execute_many
rows += self.execute(sql + postfix)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\MySQLdb\cursors.py", line 206, in execute
res = self._query(query)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\MySQLdb\cursors.py", line 312, in _query
db.query(q)
File "C:\Users\DELL\PycharmProjects\GUIRef\venv\lib\site- 
packages\MySQLdb\connections.py", line 224, in query
_mysql.connection.query(self, query)
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) 
(1054, "Unknown column 'index' in 'field list'")
[SQL: INSERT INTO govtracker (`index`, `Site Name`, `Region`, `Site Type`, 
`SiteCode`, `TAC Name`, `DT
Readiness`, `RFS`, `RFS Date`, `Huawei 1st submission date `, `TE 1st 
Response date `, `Huawei 2nd submission date `, `TE 2nd Response date `, 
`Huawei 3rd submission date `, `TE 3rd Response date `, `Acceptance 
Date(Optimization)`, `Acceptance Date(Planning)`, `signed sites`, `As Built 
Date`, `AS built status`, `Date DT`, `DT Status`, `SHR Status`, `DT 
Planned`, `Integeration Status`, `Comments/snags`, `Cluster name`, 
`Type(Standalone/colocated)`, `Installed type (Standalone/colocated)`, ` 
 Status `, `Pending  `, `Pending Status`, `problematic details`, `ETS TAC 
 `, `Region.1`, `SF6
 Signed date`, `SF6
 Signed Comment`, ` Comment History`, `On air Owner`, `PP 
 Owner`, `Report 
 Comment`, `HU Opt.
 Area Owner`, `Planning Owner`, `PO Number`, `Trigger date `, `As built 
 status`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 
 %s, 
 %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 
 %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)]
 [parameters: ((0, 'Manshyet Naser', 'Cairo', 'Existing', 'LCAIE10002', 
 'Manshyet nasser', datetime.datetime(2017, 7, 20, 0, 0), 'yes', 
 datetime.datetime(2017, 7, 23, 0, 0), datetime.datetime(2017, 9, 12, 0, 0), 
 None, None, None, None, None, datetime.datetime(2017, 9, 17, 0, 0), None, 
 'PAC Signed', None, 'Done', None, None, None, None, None, None, 'Mid 
 Cairo', 'L700+L1800', None, 'PAC Signed', None, None, None, 41181, 'R2', 
 datetime.datetime(2017, 1, 11, 0, 0), None, None, None, None, None, 'Nour', 
 'Ahmed Ibrahim', 'PO1', 2018, None), (1, 'Khanka LE', 'Cairo', 'Existing', 
 'LCAIN30531', 'Khanka Cluster', datetime.datetime(2017, 7, 20, 0, 0), 
 'yes', datetime.datetime(2017, 7, 23, 0, 0), datetime.datetime(2017, 9, 12, 
 0, 0), None, None, None, None, None, datetime.datetime(2017, 9, 17, 0, 0), 
 None, 'PAC Signed', None, 'Done', None, None, None, None, None, None, '10th 
 ramadan+Sherouk+Badr+Obor + Khanka + El Salam', 'L700+L1800', None, 'PAC 
 Signed', None, None, None, 43043, 'R3', datetime.datetime(2017, 2, 5, 0, 
 0), None, None, None, None, None, 'Ahmed Samir', 'Amr Fahmy', 'PO1', 2018, 
 None), (2, 'Kalyob', 'Cairo', 'Existing', 'LCAIW10254', 'Qalyub', 
 datetime.datetime(2017, 7, 20, 0, 0), 'yes', datetime.datetime(2017, 7, 23, 
 0, 0), datetime.datetime(2017, 10, 9, 0, 0), None, None, None, None, None, 
 datetime.datetime(2017, 11, 3, 0, 0), None, 'PAC Signed', None, 'Done', 
 None, None, None, None, None, None, 'Shobra el Khiema+Benha', 'L700+L1800', 
 None, 'PAC Signed', None, None, None, 41112, 'R2', datetime.datetime(2016, 
 11, 1, 0, 0), None, None, None, 'Rafiq Magdy', None, 'Ahmed Elsayed', 'Amr 
 Othman', 'PO1', 2018, None), (3, 'HELWAN LE', 'Cairo', 'Existing', 
 'LCAIW30316', 'Helwan', datetime.datetime(2017, 7, 20, 0, 0), 'yes', 
 datetime.datetime(2017, 7, 23, 0, 0), datetime.datetime(2017, 9, 12, 0, 0), 
 None, None, None, None, None, datetime.datetime(2017, 9, 17, 0, 0), None, 
 'PAC Signed', None, 'Done', None, None, None, None, None, None, 'Helwan + 
 Maasara', 'L700+L1800', None, 'PAC Signed', None, None, None, 41101, 'R1', 
 datetime.datetime(2016, 12, 5, 0, 0), None, None, None, 'Khaled Kashaba', 
 None, 'Ahmed Elsayed', 'Amr Othman', 'PO1', 2018, None), (4, 'HAFEER', 
 'Cairo', 'Existing', 'LCAIN30083', 'El khosous', datetime.datetime(2017, 7, 
 20, 0, 0), 'yes', datetime.datetime(2017, 7, 23, 0, 0), 
 datetime.datetime(2017, 10, 28, 0, 0), None, None, None, None, None, 
 datetime.datetime(2017, 11, 3, 0, 0), None, 'PAC Signed', None, 'Done', 
 None, None, None, None, None, None, 'Marg el Khosos', 'L700+L1800', None, 
 'PAC Signed', None, None, None, 41121, 'R2', datetime.datetime(2016, 11, 1, 
 0, 0), None, None, None, 'Mohamed Ramadan', None, 'Rahaf', 'Amr Fahmy', 
 'PO1', 2018, None), (5, 'EL-Sharabia', 'Cairo', 'Existing', 'LCAIW10212', 
 'Abbaseya', datetime.datetime(2017, 7, 20, 0, 0), 'yes', 
 datetime.datetime(2017, 7, 23, 0, 0), datetime.datetime(2017, 9, 18, 0, 0), 
 None, None, None, None, None, datetime.datetime(2017, 10, 2, 0, 0), None, 
 'PAC Signed', None, 'Done', None, None, None, None, None, None, 'Waili', 
 'L700+L1800', None, 'PAC Signed', None, None, None, 41131, 'R2', 
 datetime.datetime(2016, 11, 1, 0, 0), None, None, None, None, None, 'Ahmed 
 Elsayed', 'Amr Othman', 'PO1', 2018, None), (6, 'El-Salam', 'Cairo', 
 'Existing', 'LCAIN30173', 'Madinet El Salam', datetime.datetime(2017, 7, 
 20, 0, 0), 'yes', datetime.datetime(2017, 7, 23, 0, 0), datetime.datetime(2017, 9, 12, 0, 0), None, None, None, None, None, datetime.datetime(2017, 9, 17, 0, 0), None, 'PAC Signed', None, 'Done', None, None, None, None, None, None, '10th ramadan+Sherouk+Badr+Obor + Khanka + El Salam', 'L700+L1800', None, 'PAC Signed', None, None, None, 43043, 'R3', datetime.datetime(2016, 11, 1, 0, 0), None, None, None, None, None, 'Ahmed Samir', 'Amr Fahmy', 'PO1', 2018, None), (7, 'El-Roda', 'Cairo', 'Existing', 'LCAIW20271', 'Manyal', datetime.datetime(2017, 7, 20, 0, 0), 'yes', datetime.datetime(2017, 7, 23, 0, 0), datetime.datetime(2017, 9, 12, 0, 0), None, None, None, None, None, datetime.datetime(2017, 9, 17, 0, 0), None, 'PAC Signed', None, 'Done', None, None, None, None, None, None, 'Downtown + Masr EL Kadima', 'L700+L1800', None, 'PAC Signed', None, None, None, 41171, 'R1', datetime.datetime(2016, 11, 1, 0, 0), None, None, None, None, None, 'Rahaf', 'Amr Othman', 'PO1', 2018, None)  ... displaying 10 of 861 total bound parameter sets ...  (859, 'Site_0475', 'Cairo', 'New', 'LCAIE30475', 'Ain Shams 2', datetime.datetime(2019, 5, 23, 0, 0), None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 'Heliopolis', '1800 Stand Alone', None, 'Ready DT', 'DT', 'planned', None, 41081, 'R2', None, None, None, None, None, None, 'Passant', 'Ahmed Ibrahim', 'PO3', None, None), (860, 0, 'Cairo', 'New', 'LCAIN22318', 0, datetime.datetime(2019, 5, 23, 0, 0), None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 'ElHarafyen', '1800 Stand Alone', None, 'Ready DT', 'DT', 'planned', None, 41082, 'R2', None, None, None, None, None, None, 'Moataz Ahmed', 'Amr Fahmy', 
'PO3', None, None))]
(Background on this error at: http://sqlalche.me/e/e3q8)

and this is my SQL Insert

CREATE TABLE `mydb`.`govtracker` (
`id` DOUBLE(255, 0) NOT NULL,
`site_name` VARCHAR(255) NOT NULL,
`region` VARCHAR(255) NOT NULL,
`site_type` VARCHAR(255) NOT NULL,
`site_code` VARCHAR(255) NOT NULL,
`tac_name` VARCHAR(255) NOT NULL,
`dt_readiness` DATE NOT NULL,
`rfs` BOOLEAN NOT NULL,
`rfs_date` DATE NOT NULL,
`huawei_1st_submission_date` DATE NOT NULL,
`te_1st_submission_date` DATE NOT NULL,
`huawei_2nd_submission_date` DATE NOT NULL,
`te_2nd_submission_date` DATE NOT NULL,
`huawei_3rd_submission_date` DATE NOT NULL,
`te_3rd_submission_date` DATE NOT NULL,
`acceptance_date_opt` DATE NOT NULL,
`acceptance_date_plan` DATE NOT NULL,
`signed_sites` VARCHAR(255) NOT NULL,
`as_built_date` DATE NOT NULL,
`as_built_status` VARCHAR(255) NOT NULL,
`date_dt` DATE NOT NULL,
`dt_status` VARCHAR(255) NOT NULL,
`shr_status` VARCHAR(255) NOT NULL,
`dt_planned` INT(255) NOT NULL,
`integeration_status` VARCHAR(255) NOT NULL,
`comments_snags` LONGTEXT NOT NULL,
`cluster_name` LONGTEXT NOT NULL,
`type_standalone_colocated` VARCHAR(255) NOT NULL,
`installed_type_standalone_colocated` VARCHAR(255) NOT NULL,
`status` VARCHAR(255) NOT NULL,
`pending` VARCHAR(255) NOT NULL,
`pending_status` LONGTEXT NOT NULL,
`problematic_details` LONGTEXT NOT NULL,
`ets_tac` INT(255) NOT NULL,
`region_r` VARCHAR(255) NOT NULL,
`sf6_signed_date` DATE NOT NULL,
`sf6_signed_comment` LONGTEXT NOT NULL,
`comment_history` LONGTEXT NOT NULL,
`on_air_owner` VARCHAR(255) NOT NULL,
`pp_owner` VARCHAR(255) NOT NULL,
`report_comment` LONGTEXT NOT NULL,
`hu_opt_area_owner` VARCHAR(255) NOT NULL,
`planning_owner` VARCHAR(255) NOT NULL,
`po_number` VARCHAR(255) NOT NULL,
`trigger_date` DATE NOT NULL,
`as_built_status_tr` VARCHAR(255) NOT NULL
) ENGINE = InnoDB;

Note: the columns name In excel is different in sql query for example

 Site Type (in Excel Sheet) and site_type (in sql) DT\nReadiness(in excel sheet) and dt_readiness (in sql)

0 Answers0