1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
import sqlite3
def convert(value): if value.startswith('~'): return value.strip('~') if not value: value = 0 return (value)
conn = sqlite3.connect('USA_food.db') curs = conn.cursor()
curs.execute(''' CREATE TABLE food( id TEXT, desc TEXT, water TEXT, kcal TEXT, protein TEXT, fat TEXT, ash TEXT, carbs TEXT, fiber TEXT, sugar TEXT, ddd TEXT, sss TEXT, huanhang TEXT )''')
query = 'INSERT INTO food VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)' field_count = 13
for line in open('./sr28asc/FOOD_DES.txt'): fields = line.split('^') vals = [convert(f) for f in fields[:field_count]] curs.execute(query,vals)
conn.commit() conn.close()
|