0
$\begingroup$

I am new using pandas. I am asking about how to insert specific columns using to_sql.

This is my explanation.

I have a database with a table datasiswa with columns:

  • id: as int, with autoincrement, as primary key
  • name: string
  • age: string

And I have an excel file with header name and age.

How to use to_sql to insert in fields name and age only, without ids.

enter image description here

$\endgroup$
3
  • $\begingroup$ Is hiding your entire path really necessary? $\endgroup$ Apr 11 '19 at 0:41
  • $\begingroup$ If I understood correctly, you want to fill a SQL Table entries from a Excel file, and this table should create IDs automatically. $\endgroup$ Apr 11 '19 at 0:42
  • $\begingroup$ @PedroHenriqueMonforte Hide because credential our server, yes i just want insert name and age, whitout ids, because id automatically generate by autoincrement. $\endgroup$ Apr 11 '19 at 2:27
0
$\begingroup$

You'll need to perform two steps:

  1. You get ride of the id column in the pandas dataframe:

    df=df.drop(columns=['id'])
    
  2. Add the index=False param: to the .to_sql() method:

    selected.to_sql('dataiswa', engine, index=False)
    
$\endgroup$
1
  • $\begingroup$ 'id' column is not in the dataframe, it's in the database $\endgroup$ Jul 23 '20 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.