Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

python 3.x - how to set properly the TYPEs for columns while importing excel data into sqlite database with python3?

I am importing excel data into sqlite database with python3 with the below code:

import sqlite3
import pandas as pd
con = sqlite3.connect("test.db")
wb = pd.read_excel("for_DB_data.xlsx", sheet_name=None)
for sheet in wb:
    wb.[sheet].to_sql(sheet, con, if_exists="replace", index=False)
con.commit()
con.close()

As result for ID column I get such values:

1.0
2.0
'''
99.0

But I want to see as:

1
2
'''
99

Without .0, whereas my excel ID column filled as:

1
2
'''
99

When I open database with sqlitebrowser in linux I see ID Type as REAL, but I want for ID TYPE to be set as INTEGER. The same I want for other columns which are set to REAL, but I want to be set as INTEGER OR TEXT(some). How can I ask python to set TYPE for ID or some other columns as INTEGER or TEXT while exporting the data into sqlite3 database? Thank you in advance for the help.

question from:https://stackoverflow.com/questions/65640730/how-to-set-properly-the-types-for-columns-while-importing-excel-data-into-sqlite

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...