What does self.env.cr.copy_from do?
The copy_from function takes almost four parameters.Recommend:
- CSV File: Takes content that need to be stored into the database table
- Table Name: Name of the table, where to stores the CSV content
- Separator: Symbol that distinguish between columns e.g. (|,;,etc)
- Column: Column of the table in which CSV data to be inserted
Recommend:
What we are going to do?
We are going to save or insert CSV data into the table using "self.env.cr.copy_from". To do this we need to do following activity.- Create table using "self.env.cr.execute"
- Open CSV file
- Create list of column (fourth parameter of "copy_from" function)
- Copy CSV data into table using "self.env.cr.copy_from"
- Close CSV file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.env.cr.execute(""" CREATE TABLE IF NOT EXISTS your_table( | |
count decimal, mean decimal, std decimal, min decimal, twentyfive decimal, | |
fifty decimal, seventyfive decimal, max decimal, cv decimal, | |
uid integer, report_type text,name text)""") | |
self.env.cr.commit() | |
csv_file= open(filepath,"r") | |
columns=['count','mean','std','min','twentyfive','fifty','seventyfive','max','cv','uid','report_type','name'] | |
self.env.cr.copy_from(csv_file, 'your_table', sep='|', columns=columns) | |
self.env.cr.commit() | |
csv_file.close() |
Don,t forgot to share, keep sharing keep learning
2 Comments
HI,what if my table exit what should do ? How i can run this function ?
ReplyDeletecan u please explain?
Delete