I am importing data from a .csv file with headers into a SQLite table. This is easy to do of course and I could simply do the following:
sqlite> .separator ","
sqlite> .import 2015.csv tennisresults2015
However if I do this then all the columns default to the 'TEXT' data type. Instead I want my numeric field to default to either the 'INTEGER' or 'REAL' data types, which ever is appropriate.
Now I realize I can define each column explicitly in Python when I create a table via the following lines of code:
conn = sqlite3.connect('tennisresults.db') #connection
c = conn.cursor() #get a cursor object, all SQL commands are processed by it
c.execute('CREATE TABLE tennisresults2015(ATP INTEGER, Location TEXT, Tournament TEXT)') #etc
However this .csv file has a lot of columns, as you can see from the first two lines of the file shown below:
ATP,Location,Tournament,Date,Series,Court,Surface,Round,Best of,Winner,Loser,WRank,LRank,WPts,LPts,W1,L1,W2,L2,W3,L3,W4,L4,W5,L5,Wsets,Lsets,Comment,B365W,B365L,EXW,EXL,LBW,LBL,PSW,PSL,MaxW,MaxL,AvgW,AvgL
1,Brisbane,Brisbane International,01/05/2015,ATP250,Outdoor,Hard,1st Round,3,Duckworth J.,Simon G.,125,21,430,1730,6,2,6,2,,,,,,,2,0,Completed,4.5,1.18,4.3,1.2,4.33,1.2,4.67,1.23,4.73,1.23,4.31,1.2
I was wondering if there is a more efficient way of doing this instead of me manually specifying each field name and data type in Python?
Sadly I think the answer is simply no, there is no built-in way to do what you're asking. You need to either:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.